Skip to content

Commit

Permalink
writing tests to test grpc integration
Browse files Browse the repository at this point in the history
  • Loading branch information
aravinsiva committed Jun 15, 2020
1 parent eebd8fd commit 7785468
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions ext/opentelemetry-ext-grpc/tests/test_server_interceptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from opentelemetry.ext.grpc.grpcext import intercept_server
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.test.test_base import TestBase
from opentelemetry.ext.grpc import GrpcInstrumentorServer, GrpcInstrumentorClient



class UnaryUnaryMethodHandler(grpc.RpcMethodHandler):
Expand All @@ -49,6 +51,51 @@ def service(self, handler_call_details):


class TestOpenTelemetryServerInterceptor(TestBase):

def test_instrumentor(self):
# Intercept gRPC calls...
#interceptor = server_interceptor()

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

#server = grpc.server(
# futures.ThreadPoolExecutor(max_workers=1),
# options=(("grpc.so_reuseport", 0),),
#)
# FIXME: grpcext interceptor doesn't apply to handlers passed to server
# init, should use intercept_service API instead.
#server = intercept_server(server, interceptor)

grpc_server_instrumentor = GrpcInstrumentorServer()
grpc_server_instrumentor.instrument()

server = grpc_server_instrumentor.server

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

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

try:
server.start()
channel.unary_unary("")(b"")
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, "")
self.assertIs(span.kind, trace.SpanKind.SERVER)

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


def test_create_span(self):
"""Check that the interceptor wraps calls with spans server-side."""

Expand Down

0 comments on commit 7785468

Please sign in to comment.