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()
Beispiel #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()