def test_uninstrument(self):
        def handler(request, context):
            return b""

        grpc_server_instrumentor = GrpcInstrumentorServer()
        grpc_server_instrumentor.instrument()
        grpc_server_instrumentor.uninstrument()
        server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=1),
            options=(("grpc.so_reuseport", 0), ),
        )

        server.add_generic_rpc_handlers((UnaryUnaryRpcHandler(handler), ))

        port = server.add_insecure_port("[::]:0")
        channel = grpc.insecure_channel("localhost:{:d}".format(port))

        rpc_call = "TestServicer/test"
        try:
            server.start()
            channel.unary_unary(rpc_call)(b"test")
        finally:
            server.stop(None)

        spans_list = self.memory_exporter.get_finished_spans()
        self.assertEqual(len(spans_list), 0)
    def test_instrumentor(self):
        def handler(request, context):
            return b""

        grpc_server_instrumentor = GrpcInstrumentorServer()
        grpc_server_instrumentor.instrument()
        server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=1),
            options=(("grpc.so_reuseport", 0), ),
        )

        server.add_generic_rpc_handlers((UnaryUnaryRpcHandler(handler), ))

        port = server.add_insecure_port("[::]:0")
        channel = grpc.insecure_channel("localhost:{:d}".format(port))

        try:
            server.start()
            channel.unary_unary("test")(b"test")
        finally:
            server.stop(None)

        spans_list = self.memory_exporter.get_finished_spans()
        self.assertEqual(len(spans_list), 1)
        span = spans_list[0]
        self.assertEqual(span.name, "test")
        self.assertIs(span.kind, trace.SpanKind.SERVER)
        self.check_span_instrumentation_info(
            span, opentelemetry.instrumentation.grpc)
        grpc_server_instrumentor.uninstrument()
Пример #3
0
    def test_instrumentor(self):
        def handler(request, context):
            return b""

        grpc_server_instrumentor = GrpcInstrumentorServer()
        grpc_server_instrumentor.instrument()
        with futures.ThreadPoolExecutor(max_workers=1) as executor:
            server = grpc.server(
                executor,
                options=(("grpc.so_reuseport", 0), ),
            )

            server.add_generic_rpc_handlers((UnaryUnaryRpcHandler(handler), ))

            port = server.add_insecure_port("[::]:0")
            channel = grpc.insecure_channel("localhost:{:d}".format(port))

            rpc_call = "TestServicer/handler"
            try:
                server.start()
                channel.unary_unary(rpc_call)(b"test")
            finally:
                server.stop(None)

            spans_list = self.memory_exporter.get_finished_spans()
            self.assertEqual(len(spans_list), 1)
            span = spans_list[0]
            self.assertEqual(span.name, rpc_call)
            self.assertIs(span.kind, trace.SpanKind.SERVER)

            # Check version and name in span's instrumentation info
            self.check_span_instrumentation_info(
                span, opentelemetry.instrumentation.grpc)

            # Check attributes
            self.assert_span_has_attributes(
                span,
                {
                    SpanAttributes.NET_PEER_IP:
                    "[::1]",
                    SpanAttributes.NET_PEER_NAME:
                    "localhost",
                    SpanAttributes.RPC_METHOD:
                    "handler",
                    SpanAttributes.RPC_SERVICE:
                    "TestServicer",
                    SpanAttributes.RPC_SYSTEM:
                    "grpc",
                    SpanAttributes.RPC_GRPC_STATUS_CODE:
                    grpc.StatusCode.OK.value[0],
                },
            )

            grpc_server_instrumentor.uninstrument()
logger = getJSONLogger('recommendationservice-server')

zipkin_exporter = zipkin.ZipkinSpanExporter(
    service_name="recommendationservice",
    url=os.environ['SIGNALFX_ENDPOINT_URL']
)
span_processor = BatchExportSpanProcessor(zipkin_exporter)

propagators.set_global_textmap(FixedB3Format())
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(span_processor)
tracer = trace.get_tracer(__name__)

instrumentor = GrpcInstrumentorClient()
instrumentor.instrument()
grpc_server_instrumentor = GrpcInstrumentorServer()
grpc_server_instrumentor.instrument()


class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer):
    def ListRecommendations(self, request, context):
        max_responses = 5
        # fetch list of products from product catalog stub
        cat_response = product_catalog_stub.ListProducts(demo_pb2.Empty())
        product_ids = [x.id for x in cat_response.products]
        filtered_products = list(set(product_ids)-set(request.product_ids))
        num_products = len(filtered_products)
        num_return = min(max_responses, num_products)
        # sample list of indicies to return
        indices = random.sample(range(num_products), num_return)
        # fetch product ids from indices