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

Commit 186e2ce

Browse files
* Update our OT dependency to 2.0.0 * Implement Span propagation (opentracing#25)
1 parent 95f1c3a commit 186e2ce

File tree

8 files changed

+93
-18
lines changed

8 files changed

+93
-18
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
exclude =
3+
basictracer/wire_pb2.py

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
language: python
2-
python:
2+
python:
33
- "2.7"
44

55
install:
66
- make bootstrap
77

88
script:
9-
- make test
10-
9+
- make test lint

CHANGELOG.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
History
44
-------
55

6-
2.2.2 (unreleased)
7-
------------------
6+
3.0.0rc2 (2018-04-09)
7+
---------------------
88

9-
- Nothing changed yet.
9+
- Implement ScopeManager for in-process propagation.
1010

1111

1212
2.2.1 (2018-02-20)

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ clean-test:
5252
rm -fr htmlcov/
5353

5454
lint:
55-
flake8 $(project) tests
55+
flake8 --config=.flake8 $(project) tests
5656

5757
test:
5858
$(pytest) $(test_args)

basictracer/tracer.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import opentracing
44
from opentracing import Format, Tracer
55
from opentracing import UnsupportedFormatException
6+
from opentracing.scope_managers import ThreadLocalScopeManager
67
from .context import SpanContext
78
from .recorder import SpanRecorder, DefaultSampler
89
from .span import BasicSpan
@@ -11,7 +12,7 @@
1112

1213
class BasicTracer(Tracer):
1314

14-
def __init__(self, recorder=None, sampler=None):
15+
def __init__(self, recorder=None, sampler=None, scope_manager=None):
1516
"""Initialize a BasicTracer instance.
1617
1718
Note that the returned BasicTracer has *no* propagators registered. The
@@ -23,7 +24,10 @@ def __init__(self, recorder=None, sampler=None):
2324
with the binary carrier.
2425
"""
2526

26-
super(BasicTracer, self).__init__()
27+
scope_manager = ThreadLocalScopeManager() \
28+
if scope_manager is None else scope_manager
29+
super(BasicTracer, self).__init__(scope_manager)
30+
2731
self.recorder = NoopRecorder() if recorder is None else recorder
2832
self.sampler = DefaultSampler(1) if sampler is None else sampler
2933
self._propagators = {}
@@ -44,13 +48,34 @@ def register_required_propagators(self):
4448
self.register_propagator(Format.HTTP_HEADERS, TextPropagator())
4549
self.register_propagator(Format.BINARY, BinaryPropagator())
4650

47-
def start_span(
48-
self,
49-
operation_name=None,
50-
child_of=None,
51-
references=None,
52-
tags=None,
53-
start_time=None):
51+
def start_active_span(self,
52+
operation_name,
53+
child_of=None,
54+
references=None,
55+
tags=None,
56+
start_time=None,
57+
ignore_active_span=False,
58+
finish_on_close=True):
59+
60+
# create a new Span
61+
span = self.start_span(
62+
operation_name=operation_name,
63+
child_of=child_of,
64+
references=references,
65+
tags=tags,
66+
start_time=start_time,
67+
ignore_active_span=ignore_active_span,
68+
)
69+
70+
return self.scope_manager.activate(span, finish_on_close)
71+
72+
def start_span(self,
73+
operation_name=None,
74+
child_of=None,
75+
references=None,
76+
tags=None,
77+
start_time=None,
78+
ignore_active_span=False):
5479

5580
start_time = time.time() if start_time is None else start_time
5681

@@ -64,6 +89,12 @@ def start_span(
6489
# TODO only the first reference is currently used
6590
parent_ctx = references[0].referenced_context
6691

92+
# retrieve the active SpanContext
93+
if not ignore_active_span and parent_ctx is None:
94+
scope = self.scope_manager.active
95+
if scope is not None:
96+
parent_ctx = scope.span.context
97+
6798
# Assemble the child ctx
6899
ctx = SpanContext(span_id=generate_id())
69100
if parent_ctx is not None:

setup.py

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

33
setup(
44
name='basictracer',
5-
version='2.2.2.dev0',
5+
version='3.0.0rc2',
66
author='The OpenTracing Authors',
77
author_email='info@opentracing.io',
88
license='MIT',
@@ -22,7 +22,7 @@
2222
platforms='any',
2323
install_requires=[
2424
'protobuf>=3.0.0b2.post2',
25-
'opentracing>=1.2.1,<2.0',
25+
'opentracing==2.0.0',
2626
'six>=1.10.0,<2.0',
2727
],
2828
extras_require={

tests/test_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ def tracer(self):
2626

2727
def check_baggage_values(self):
2828
return True
29+
30+
def is_parent(self, parent, span):
31+
# use `Span` ids to check parenting
32+
if parent is None:
33+
return span.parent_id is None
34+
35+
return parent.context.span_id == span.parent_id

tests/utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (c) 2017 The OpenTracing Authors.
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
from __future__ import absolute_import
22+
23+
from unittest import TestCase
24+
25+
from basictracer import BasicTracer
26+
from basictracer.recorder import InMemoryRecorder
27+
28+
29+
class TracerTestCase(TestCase):
30+
"""Common TestCase to avoid duplication"""
31+
32+
def setUp(self):
33+
# initialize an in-memory tracer
34+
self.recorder = InMemoryRecorder()
35+
self.tracer = BasicTracer(recorder=self.recorder)

0 commit comments

Comments
 (0)