-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.py
98 lines (83 loc) · 3.79 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
"""Simple example showing several generations of spans in a trace.
"""
import argparse
import sys
import time
import traceback
import opentracing
import lightstep.tracer
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_active_span('trivial/initial_request') as parent_scope:
parent_scope.span.set_tag('url', 'localhost')
sleep_dot()
parent_scope.span.log_event('All good here!', payload={'N': 42, 'pi': 3.14, 'abc': 'xyz'})
parent_scope.span.log_kv({'foo': 'bar', 'int': 42, 'float': 4.2, 'bool': True, 'obj': {'blargh': 'hmm', 'whee': 4324}})
parent_scope.span.set_tag('span_type', 'parent')
parent_scope.span.set_tag('int_tag', 5)
parent_scope.span.set_tag('unicode_val', u'non-ascii: \u200b')
parent_scope.span.set_tag('bool_tag', True)
parent_scope.span.set_baggage_item('checked', 'baggage')
sleep_dot()
# This is how you would represent starting work locally.
with opentracing.tracer.start_active_span('trivial/child_request') as child_scope:
child_scope.span.set_tag('span_type', 'child')
# Pretend there was an error
child_scope.span.set_tag('error', True)
child_scope.span.log_event('Uh Oh!', payload={'stacktrace': traceback.extract_stack()})
sleep_dot()
# 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_scope.span.context, opentracing.Format.TEXT_MAP, text_carrier)
span_context = opentracing.tracer.extract(opentracing.Format.TEXT_MAP, text_carrier)
with opentracing.tracer.start_active_span(
'trivial/remote_span',
child_of=span_context) as remote_scope:
remote_scope.span.log_event('Remote!')
remote_scope.span.set_tag('span_type', 'remote')
sleep_dot()
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('--no_tls', help='Disable TLS for reporting',
dest="no_tls", action='store_true')
parser.add_argument('--component_name', help='The LightStep component name',
default='TrivialExample')
parser.add_argument('--use_thrift', help='Use Thrift over http',
dest="use_thrift", action='store_true')
args = parser.parse_args()
if args.no_tls:
collector_encryption = 'none'
else:
collector_encryption = 'tls'
return lightstep.Tracer(
component_name=args.component_name,
access_token=args.token,
collector_host=args.host,
collector_port=args.port,
verbosity=1,
collector_encryption=collector_encryption,
use_thrift=args.use_thrift,
use_http=not args.use_thrift)
if __name__ == '__main__':
print('Hello ')
# Use LightStep's opentracing implementation
with lightstep_tracer_from_args() as tracer:
opentracing.tracer = tracer
add_spans()
print(' World!')