Skip to content
This repository was archived by the owner on Jul 21, 2021. It is now read-only.

Commit 02c1051

Browse files
carlosalbertoJoe
authored andcommitted
v4.0.0 (lightstep#49)
Adds support for OpenTracing 2.0 Python APIs! This supports ScopeManager to make in-process context propagation simpler.
1 parent 9a20034 commit 02c1051

File tree

10 files changed

+167
-123
lines changed

10 files changed

+167
-123
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<a name="4.0.0"></a>
2+
## [4.0.0](https://github.com/lightstep/lightstep-tracer-python/compare/4.0.0...3.0.11)
3+
### BREAKING CHANGES
4+
* Integrate the OpenTracing 2.0.0 updates which implements the ScopeManager for in-process propagation (OT changelog can be found [here](https://medium.com/opentracing/announcing-python-opentracing-2-0-0-fa4e4c9395a))

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ if __name__ == "__main__":
7777
component_name='your_microservice_name',
7878
access_token='{your_access_token}')
7979

80-
with opentracing.tracer.start_span('TestSpan') as span:
81-
span.log_event('test message', payload={'life': 42})
80+
with opentracing.tracer.start_active_span('TestSpan') as scope:
81+
scope.span.log_event('test message', payload={'life': 42})
8282

8383
opentracing.tracer.flush()
8484
```

examples/http/context_in_headers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class RemoteHandler(BaseHTTPRequestHandler):
3636
"""
3737

3838
def do_GET(self):
39-
with before_answering_request(self, opentracing.tracer) as server_span:
39+
server_span = before_answering_request(self, opentracing.tracer)
40+
with opentracing.tracer.scope_manager.activate(server_span, True):
4041
server_span.log_event('request received', self.path)
4142

4243
self.send_response(200)
@@ -160,7 +161,8 @@ def lightstep_tracer_from_args():
160161
# Prepare request in the client
161162
url = 'http://localhost:{}'.format(port_number)
162163
request = Request(url)
163-
with before_sending_request(request) as client_span:
164+
client_span = before_sending_request(request)
165+
with opentracing.tracer.scope_manager.activate(client_span, True):
164166
client_span.log_event('sending request', url)
165167

166168
# Send request to server

examples/nontrivial/main.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def sleep_dot():
2727
def add_spans():
2828
"""Calls the opentracing API, doesn't use any LightStep-specific code.
2929
"""
30-
with opentracing.tracer.start_span(operation_name='trivial/initial_request') as parent_span:
31-
parent_span.set_tag('url', 'localhost')
32-
parent_span.log_event('All good here!', payload={'N': 42, 'pi': 3.14, 'abc': 'xyz'})
33-
parent_span.set_tag('span_type', 'parent')
34-
parent_span.set_baggage_item('checked', 'baggage')
30+
with opentracing.tracer.start_active_span('trivial/initial_request') as parent_scope:
31+
parent_scope.span.set_tag('url', 'localhost')
32+
parent_scope.span.log_event('All good here!', payload={'N': 42, 'pi': 3.14, 'abc': 'xyz'})
33+
parent_scope.span.set_tag('span_type', 'parent')
34+
parent_scope.span.set_baggage_item('checked', 'baggage')
3535

3636
rng = random.SystemRandom()
3737
for i in range(50):
@@ -40,21 +40,21 @@ def add_spans():
4040
sys.stdout.flush()
4141

4242
# This is how you would represent starting work locally.
43-
with opentracing.start_child_span(parent_span, operation_name='trivial/child_request') as child_span:
44-
child_span.log_event('Uh Oh!', payload={'error': True})
45-
child_span.set_tag('span_type', 'child')
43+
with opentracing.tracer.start_active_span('trivial/child_request') as child_scope:
44+
child_scope.span.log_event('Uh Oh!', payload={'error': True})
45+
child_scope.span.set_tag('span_type', 'child')
4646

4747
# Play with the propagation APIs... this is not IPC and thus not
4848
# where they're intended to be used.
4949
text_carrier = {}
50-
opentracing.tracer.inject(child_span.context, opentracing.Format.TEXT_MAP, text_carrier)
50+
opentracing.tracer.inject(child_scope.span.context, opentracing.Format.TEXT_MAP, text_carrier)
5151

5252
span_context = opentracing.tracer.extract(opentracing.Format.TEXT_MAP, text_carrier)
53-
with opentracing.tracer.start_span(
53+
with opentracing.tracer.start_active_span(
5454
'nontrivial/remote_span',
55-
child_of=span_context) as remote_span:
56-
remote_span.log_event('Remote!')
57-
remote_span.set_tag('span_type', 'remote')
55+
child_of=span_context) as remote_scope:
56+
remote_scope.span.log_event('Remote!')
57+
remote_scope.span.set_tag('span_type', 'remote')
5858
time.sleep(rng.random() * 0.1)
5959

6060
opentracing.tracer.flush()

examples/trivial/main.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,37 @@ def sleep_dot():
2020
def add_spans():
2121
"""Calls the opentracing API, doesn't use any LightStep-specific code.
2222
"""
23-
with opentracing.tracer.start_span(operation_name='trivial/initial_request') as parent_span:
24-
parent_span.set_tag('url', 'localhost')
23+
with opentracing.tracer.start_active_span('trivial/initial_request') as parent_scope:
24+
parent_scope.span.set_tag('url', 'localhost')
2525
sleep_dot()
26-
parent_span.log_event('All good here!', payload={'N': 42, 'pi': 3.14, 'abc': 'xyz'})
27-
parent_span.log_kv({'foo': 'bar', 'int': 42, 'float': 4.2, 'bool': True, 'obj': {'blargh': 'hmm', 'whee': 4324}})
28-
parent_span.set_tag('span_type', 'parent')
29-
parent_span.set_tag('int_tag', 5)
30-
parent_span.set_tag('unicode_val', u'non-ascii: \u200b')
31-
parent_span.set_tag('bool_tag', True)
32-
parent_span.set_baggage_item('checked', 'baggage')
26+
parent_scope.span.log_event('All good here!', payload={'N': 42, 'pi': 3.14, 'abc': 'xyz'})
27+
parent_scope.span.log_kv({'foo': 'bar', 'int': 42, 'float': 4.2, 'bool': True, 'obj': {'blargh': 'hmm', 'whee': 4324}})
28+
parent_scope.span.set_tag('span_type', 'parent')
29+
parent_scope.span.set_tag('int_tag', 5)
30+
parent_scope.span.set_tag('unicode_val', u'non-ascii: \u200b')
31+
parent_scope.span.set_tag('bool_tag', True)
32+
parent_scope.span.set_baggage_item('checked', 'baggage')
3333
sleep_dot()
3434

3535
# This is how you would represent starting work locally.
36-
with opentracing.start_child_span(parent_span, operation_name='trivial/child_request') as child_span:
37-
child_span.set_tag('span_type', 'child')
36+
with opentracing.tracer.start_active_span('trivial/child_request') as child_scope:
37+
child_scope.span.set_tag('span_type', 'child')
3838
# Pretend there was an error
39-
child_span.set_tag('error', True)
40-
child_span.log_event('Uh Oh!', payload={'stacktrace': traceback.extract_stack()})
39+
child_scope.span.set_tag('error', True)
40+
child_scope.span.log_event('Uh Oh!', payload={'stacktrace': traceback.extract_stack()})
4141
sleep_dot()
4242

4343
# Play with the propagation APIs... this is not IPC and thus not
4444
# where they're intended to be used.
4545
text_carrier = {}
46-
opentracing.tracer.inject(child_span.context, opentracing.Format.TEXT_MAP, text_carrier)
46+
opentracing.tracer.inject(child_scope.span.context, opentracing.Format.TEXT_MAP, text_carrier)
4747

4848
span_context = opentracing.tracer.extract(opentracing.Format.TEXT_MAP, text_carrier)
49-
with opentracing.tracer.start_span(
49+
with opentracing.tracer.start_active_span(
5050
'trivial/remote_span',
51-
child_of=span_context) as remote_span:
52-
remote_span.log_event('Remote!')
53-
remote_span.set_tag('span_type', 'remote')
51+
child_of=span_context) as remote_scope:
52+
remote_scope.span.log_event('Remote!')
53+
remote_scope.span.set_tag('span_type', 'remote')
5454
sleep_dot()
5555

5656
def lightstep_tracer_from_args():

lightstep/tracer.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def Tracer(**kwargs):
4747
inject/extract format (which relies on protobufs and may cause problems
4848
if other versions of protobufs are active in the same packaging
4949
configuration). Defaults to False (i.e., binary format is enabled).
50+
:param ScopeManager scope_manager: the ScopeManager responsible for
51+
Span activation. Defaults to the implementation provided by the
52+
basictracer package, which uses thread-local storage.
5053
:param bool use_thrift: Forces the use of Thrift as the transport protocol.
5154
:param bool use_http: Forces the use of Proto over http.
5255
:param float timeout_seconds: Number of seconds allowed for the HTTP report transaction (fractions are permitted)
@@ -55,13 +58,21 @@ def Tracer(**kwargs):
5558
if 'disable_binary_format' in kwargs:
5659
enable_binary_format = not kwargs['disable_binary_format']
5760
del kwargs['disable_binary_format']
58-
return _LightstepTracer(enable_binary_format, Recorder(**kwargs))
61+
62+
scope_manager = None
63+
if 'scope_manager' in kwargs:
64+
scope_manager = kwargs['scope_manager']
65+
del kwargs['scope_manager']
66+
67+
return _LightstepTracer(enable_binary_format,
68+
Recorder(**kwargs),
69+
scope_manager)
5970

6071

6172
class _LightstepTracer(BasicTracer):
62-
def __init__(self, enable_binary_format, recorder):
73+
def __init__(self, enable_binary_format, recorder, scope_manager):
6374
"""Initialize the LightStep Tracer, deferring to BasicTracer."""
64-
super(_LightstepTracer, self).__init__(recorder)
75+
super(_LightstepTracer, self).__init__(recorder, scope_manager=scope_manager)
6576
self.register_propagator(Format.TEXT_MAP, TextPropagator())
6677
self.register_propagator(Format.HTTP_HEADERS, TextPropagator())
6778
if enable_binary_format:

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
setup(
44
name='lightstep',
5-
version='3.0.11',
5+
version='4.0.0',
66
description='LightStep Python OpenTracing Implementation',
77
long_description='',
88
author='LightStep',
99
license='',
1010
install_requires=['thrift==0.10.0',
1111
'jsonpickle',
1212
'six',
13-
'basictracer>=2.2,<2.3',
13+
'basictracer>=3.0,<3.1',
1414
'googleapis-common-protos==1.5.3',
1515
'requests>=2.19,<3.0'],
1616
tests_require=['pytest',

tests/opentracing_compatibility_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ def setUp(self):
1818
def tracer(self):
1919
return self._tracer
2020

21+
def is_parent(self, parent, span):
22+
if span is None:
23+
return False
24+
25+
if parent is None:
26+
return span.parent_id is None
27+
28+
return parent.context.span_id == span.parent_id
29+
2130
def tearDown(self):
2231
self._tracer.flush()
2332

0 commit comments

Comments
 (0)