This repository was archived by the owner on Apr 3, 2020. It is now read-only.
forked from opentracing-contrib/python-grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_server.py
69 lines (53 loc) · 1.76 KB
/
integration_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from __future__ import print_function
import time
import argparse
import grpc
from concurrent import futures
from jaeger_client import Config
from grpc_opentracing import open_tracing_server_interceptor
from grpc_opentracing.grpcext import intercept_server
import command_line_pb2
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class CommandLine(command_line_pb2.CommandLineServicer):
def __init__(self, tracer):
self._tracer = tracer
def Echo(self, request, context):
with self._tracer.start_span(
'command_line_server_span',
child_of=context.get_active_span().context):
return command_line_pb2.CommandResponse(text=request.text)
def serve():
parser = argparse.ArgumentParser()
parser.add_argument(
'--log_payloads',
action='store_true',
help='log request/response objects to open-tracing spans')
args = parser.parse_args()
config = Config(
config={
'sampler': {
'type': 'const',
'param': 1,
},
'logging': True,
},
service_name='integration-server')
tracer = config.initialize_tracer()
tracer_interceptor = open_tracing_server_interceptor(
tracer, log_payloads=args.log_payloads)
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
server = intercept_server(server, tracer_interceptor)
command_line_pb2.add_CommandLineServicer_to_server(
CommandLine(tracer), server)
server.add_insecure_port('[::]:50051')
server.start()
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
time.sleep(2)
tracer.close()
time.sleep(2)
if __name__ == '__main__':
serve()