-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
executable file
·98 lines (81 loc) · 3.34 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
Synthetic example with high concurrency. Used primarily to stress test the
library.
"""
import argparse
import contextlib
import sys
import time
import threading
import random
# Comment out to test against the published copy
import os
sys.path.insert(1, os.path.dirname(os.path.realpath(__file__)) + '/../..')
import opentracing
import zipkin_ot
def sleep_dot():
"""Short sleep and writes a dot to the STDOUT.
"""
time.sleep(0.05)
sys.stdout.write('.')
sys.stdout.flush()
def add_spans():
"""Calls the opentracing API, doesn't use any OpenZipkin-specific code.
"""
with opentracing.tracer.start_span(operation_name='trivial/initial_request') as parent_span:
parent_span.set_tag('url', 'localhost')
parent_span.log_event('All good here!', payload={'N': 42, 'pi': 3.14, 'abc': 'xyz'})
parent_span.set_tag('span_type', 'parent')
parent_span.set_baggage_item('checked', 'baggage')
rng = random.SystemRandom()
for i in range(50):
time.sleep(rng.random() * 0.2)
sys.stdout.write('.')
sys.stdout.flush()
# This is how you would represent starting work locally.
with opentracing.start_child_span(parent_span, operation_name='trivial/child_request') as child_span:
child_span.log_event('Uh Oh!', payload={'error': True})
child_span.set_tag('span_type', 'child')
# Play with the propagation APIs... this is not IPC and thus not
# where they're intended to be used.
text_carrier = {}
opentracing.tracer.inject(child_span.context, opentracing.Format.TEXT_MAP, text_carrier)
span_context = opentracing.tracer.extract(opentracing.Format.TEXT_MAP, text_carrier)
with opentracing.tracer.start_span(
'nontrivial/remote_span',
child_of=span_context) as remote_span:
remote_span.log_event('Remote!')
remote_span.set_tag('span_type', 'remote')
time.sleep(rng.random() * 0.1)
opentracing.tracer.flush()
def zipkin_ot_tracer_from_args():
"""Initializes OpenZipkin from the commandline args.
"""
parser = argparse.ArgumentParser()
parser.add_argument('--host', help='The OpenZipkin reporting service host to contact.',
default='localhost')
parser.add_argument('--port', help='The OpenZipkin reporting service port.',
type=int, default=9411)
parser.add_argument('--service_name', help='The OpenZipkin component name',
default='TrivialExample')
args = parser.parse_args()
return zipkin_ot.Tracer(
service_name=args.service_name,
collector_host=args.host,
collector_port=args.port,
verbosity=1)
if __name__ == '__main__':
print 'Hello ',
# Use OpenZipkin's opentracing implementation
with zipkin_ot_tracer_from_args() as tracer:
opentracing.tracer = tracer
for j in range(20):
threads = []
for i in range(64):
t = threading.Thread(target=add_spans)
threads.append(t)
t.start()
for t in threads:
t.join()
print '\n'
print ' World!'