-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (86 loc) · 3.66 KB
/
Copy pathmain.py
File metadata and controls
103 lines (86 loc) · 3.66 KB
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
99
100
101
102
103
"""
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 lightstep
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 LightStep-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 lightstep_tracer_from_args():
"""Initializes lightstep from the commandline args.
"""
parser = argparse.ArgumentParser()
parser.add_argument('--token', help='Your LightStep access token.',
default='{your_access_token}')
parser.add_argument('--host', help='The LightStep reporting service host to contact.',
default='collector.lightstep.com')
parser.add_argument('--port', help='The LightStep reporting service port.',
type=int, default=443)
parser.add_argument('--use_tls', help='Add this argument if you want to use TLS.',
action='store_true')
parser.add_argument('--component_name', help='The LightStep component name',
default='NonTrivialExample')
args = parser.parse_args()
return lightstep.Tracer(
component_name=args.component_name,
access_token=args.token,
collector_host=args.host,
collector_port=args.port,
collector_encryption=('tls' if args.use_tls else 'none'))
if __name__ == '__main__':
print('Hello '),
# Use LightStep's opentracing implementation
with lightstep_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!')