|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import time |
| 4 | +import random |
| 5 | + |
| 6 | +import grpc |
| 7 | +import stan_pb2 |
| 8 | +import stan_pb2_grpc |
| 9 | + |
| 10 | +from instana.singletons import tracer |
| 11 | + |
| 12 | +testenv = dict() |
| 13 | +testenv["grpc_port"] = 10814 |
| 14 | +testenv["grpc_host"] = "127.0.0.1" |
| 15 | +testenv["grpc_server"] = testenv["grpc_host"] + ":" + str(testenv["grpc_port"]) |
| 16 | + |
| 17 | + |
| 18 | +def generate_questions(): |
| 19 | + """ Used in the streaming grpc tests """ |
| 20 | + questions = [ |
| 21 | + stan_pb2.QuestionRequest(question="Are you there?"), |
| 22 | + stan_pb2.QuestionRequest(question="What time is it?"), |
| 23 | + stan_pb2.QuestionRequest(question="Where in the world is Waldo?"), |
| 24 | + stan_pb2.QuestionRequest(question="What did one campfire say to the other?"), |
| 25 | + stan_pb2.QuestionRequest(question="Is cereal soup?"), |
| 26 | + stan_pb2.QuestionRequest(question="What is always coming, but never arrives?") |
| 27 | + ] |
| 28 | + for q in questions: |
| 29 | + yield q |
| 30 | + time.sleep(random.uniform(0.2, 0.5)) |
| 31 | + |
| 32 | + |
| 33 | +channel = grpc.insecure_channel(testenv["grpc_server"]) |
| 34 | +server_stub = stan_pb2_grpc.StanStub(channel) |
| 35 | +# The grpc client apparently needs a second to connect and initialize |
| 36 | +time.sleep(1) |
| 37 | + |
| 38 | +with tracer.start_active_span('http-server') as scope: |
| 39 | + scope.span.set_tag('http.url', 'https://localhost:8080/grpc-client') |
| 40 | + scope.span.set_tag('http.method', 'GET') |
| 41 | + scope.span.set_tag('span.kind', 'entry') |
| 42 | + response = server_stub.OneQuestionOneResponse(stan_pb2.QuestionRequest(question="Are you there?")) |
| 43 | + |
| 44 | +with tracer.start_active_span('http-server') as scope: |
| 45 | + scope.span.set_tag('http.url', 'https://localhost:8080/grpc-server-streaming') |
| 46 | + scope.span.set_tag('http.method', 'GET') |
| 47 | + scope.span.set_tag('span.kind', 'entry') |
| 48 | + responses = server_stub.OneQuestionManyResponses(stan_pb2.QuestionRequest(question="Are you there?")) |
| 49 | + |
| 50 | +with tracer.start_active_span('http-server') as scope: |
| 51 | + scope.span.set_tag('http.url', 'https://localhost:8080/grpc-client-streaming') |
| 52 | + scope.span.set_tag('http.method', 'GET') |
| 53 | + scope.span.set_tag('span.kind', 'entry') |
| 54 | + response = server_stub.ManyQuestionsOneResponse(generate_questions()) |
0 commit comments