diff --git a/instana/instrumentation/grpcio.py b/instana/instrumentation/grpcio.py index d5f898dd..e3df4f0c 100644 --- a/instana/instrumentation/grpcio.py +++ b/instana/instrumentation/grpcio.py @@ -254,5 +254,6 @@ def call_behavior_with_instana(wrapped, instance, argv, kwargs): else: return rv + logger.debug("Instrumenting grpcio") except ImportError: pass diff --git a/tests/apps/grpc_server/stan_client.py b/tests/apps/grpc_server/stan_client.py new file mode 100644 index 00000000..700e6e35 --- /dev/null +++ b/tests/apps/grpc_server/stan_client.py @@ -0,0 +1,54 @@ +from __future__ import absolute_import + +import time +import random + +import grpc +import stan_pb2 +import stan_pb2_grpc + +from instana.singletons import tracer + +testenv = dict() +testenv["grpc_port"] = 10814 +testenv["grpc_host"] = "127.0.0.1" +testenv["grpc_server"] = testenv["grpc_host"] + ":" + str(testenv["grpc_port"]) + + +def generate_questions(): + """ Used in the streaming grpc tests """ + questions = [ + stan_pb2.QuestionRequest(question="Are you there?"), + stan_pb2.QuestionRequest(question="What time is it?"), + stan_pb2.QuestionRequest(question="Where in the world is Waldo?"), + stan_pb2.QuestionRequest(question="What did one campfire say to the other?"), + stan_pb2.QuestionRequest(question="Is cereal soup?"), + stan_pb2.QuestionRequest(question="What is always coming, but never arrives?") + ] + for q in questions: + yield q + time.sleep(random.uniform(0.2, 0.5)) + + +channel = grpc.insecure_channel(testenv["grpc_server"]) +server_stub = stan_pb2_grpc.StanStub(channel) +# The grpc client apparently needs a second to connect and initialize +time.sleep(1) + +with tracer.start_active_span('http-server') as scope: + scope.span.set_tag('http.url', 'https://localhost:8080/grpc-client') + scope.span.set_tag('http.method', 'GET') + scope.span.set_tag('span.kind', 'entry') + response = server_stub.OneQuestionOneResponse(stan_pb2.QuestionRequest(question="Are you there?")) + +with tracer.start_active_span('http-server') as scope: + scope.span.set_tag('http.url', 'https://localhost:8080/grpc-server-streaming') + scope.span.set_tag('http.method', 'GET') + scope.span.set_tag('span.kind', 'entry') + responses = server_stub.OneQuestionManyResponses(stan_pb2.QuestionRequest(question="Are you there?")) + +with tracer.start_active_span('http-server') as scope: + scope.span.set_tag('http.url', 'https://localhost:8080/grpc-client-streaming') + scope.span.set_tag('http.method', 'GET') + scope.span.set_tag('span.kind', 'entry') + response = server_stub.ManyQuestionsOneResponse(generate_questions()) diff --git a/tests/apps/grpc_server/stan_server.py b/tests/apps/grpc_server/stan_server.py index cada6673..d701b79e 100644 --- a/tests/apps/grpc_server/stan_server.py +++ b/tests/apps/grpc_server/stan_server.py @@ -1,10 +1,16 @@ +import os +import sys import grpc import time import tests.apps.grpc_server.stan_pb2 as stan_pb2 import tests.apps.grpc_server.stan_pb2_grpc as stan_pb2_grpc from concurrent import futures -from ...helpers import testenv +try: + from ...helpers import testenv +except ValueError: + # We must be running from the command line... + testenv = {} testenv["grpc_port"] = 10814 testenv["grpc_host"] = "127.0.0.1" @@ -80,3 +86,12 @@ def start_server(self): rpc_server.stop(0) print('Stan as a Service RPC Server Stopped ...') + +if __name__ == "__main__": + print ("Booting foreground GRPC application...") + # os.environ["INSTANA_TEST"] = "true" + + if sys.version_info >= (3, 5, 3): + StanServicer().start_server() + else: + print("Python v3.5.3 or higher only")