Skip to content

Commit 89911a1

Browse files
authored
GRPC minor fixes; GRPC test app & client (#180)
* Add message on instrumentation boot * Add support for running the test server manually * Add sample client code
1 parent 10c5b33 commit 89911a1

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

instana/instrumentation/grpcio.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,5 +254,6 @@ def call_behavior_with_instana(wrapped, instance, argv, kwargs):
254254
else:
255255
return rv
256256

257+
logger.debug("Instrumenting grpcio")
257258
except ImportError:
258259
pass
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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())

tests/apps/grpc_server/stan_server.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import os
2+
import sys
13
import grpc
24
import time
35
import tests.apps.grpc_server.stan_pb2 as stan_pb2
46
import tests.apps.grpc_server.stan_pb2_grpc as stan_pb2_grpc
57
from concurrent import futures
68

7-
from ...helpers import testenv
9+
try:
10+
from ...helpers import testenv
11+
except ValueError:
12+
# We must be running from the command line...
13+
testenv = {}
814

915
testenv["grpc_port"] = 10814
1016
testenv["grpc_host"] = "127.0.0.1"
@@ -80,3 +86,12 @@ def start_server(self):
8086
rpc_server.stop(0)
8187
print('Stan as a Service RPC Server Stopped ...')
8288

89+
90+
if __name__ == "__main__":
91+
print ("Booting foreground GRPC application...")
92+
# os.environ["INSTANA_TEST"] = "true"
93+
94+
if sys.version_info >= (3, 5, 3):
95+
StanServicer().start_server()
96+
else:
97+
print("Python v3.5.3 or higher only")

0 commit comments

Comments
 (0)