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

Commit 5a237ce

Browse files
mihauyurishkuro
authored andcommitted
Support OpenTracing API v2.0 (#206)
* Use opentracing 2.0 Signed-off-by: Michał Szymański <[email protected]> * remove redundant assignment Signed-off-by: Michał Szymański <[email protected]> * use opentracing in any 2.x version Signed-off-by: Michał Szymański <[email protected]> * allow specifying scope manager in config Signed-off-by: Michał Szymański <[email protected]> * Apply suggestions from code review: formatting fixes Co-Authored-By: mihau <[email protected]> Signed-off-by: Michał Szymański <[email protected]> * remove unnecessary new line Signed-off-by: Michał Szymański <[email protected]> * override test_start_activ_span Signed-off-by: Michał Szymański <[email protected]> * fix linter issues Signed-off-by: Michał Szymański <[email protected]> * pass an instance of a scope manager instead of a class Signed-off-by: Michał Szymański <[email protected]> * add a TODO to remove api tests override Signed-off-by: Michał Szymański <[email protected]> * remove trailing whitespace Signed-off-by: Michał Szymański <[email protected]> * reference opentracing-python release in TODO message Signed-off-by: Michał Szymański <[email protected]> * use named arguments Signed-off-by: Michał Szymański <[email protected]> * don't use active span if parent is passed explicitly Signed-off-by: Michał Szymański <[email protected]> * Fix lint Signed-off-by: Yuri Shkuro <[email protected]> * Bump OT to 2.1 and remove test override Signed-off-by: Yuri Shkuro <[email protected]>
1 parent 9771cc9 commit 5a237ce

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

jaeger_client/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,20 @@ class Config(object):
8484
_initialized_lock = threading.Lock()
8585

8686
def __init__(self, config, metrics=None, service_name=None, metrics_factory=None,
87-
validate=False):
87+
validate=False, scope_manager=None):
8888
"""
8989
:param metrics: an instance of Metrics class, or None. This parameter
9090
has been deprecated, please use metrics_factory instead.
9191
:param service_name: default service name.
9292
Can be overwritten by config['service_name'].
9393
:param metrics_factory: an instance of MetricsFactory class, or None.
94+
:param scope_manager: an instance of a scope manager, or None for
95+
default (ThreadLocalScopeManager).
9496
"""
9597
if validate:
9698
self._validate_config(config)
9799
self.config = config
100+
self.scope_manager = scope_manager
98101
if get_boolean(self.config.get('metrics', True), True):
99102
self._metrics_factory = metrics_factory or LegacyMetricsFactory(metrics or Metrics())
100103
else:
@@ -413,6 +416,7 @@ def create_tracer(self, reporter, sampler, throttler=None):
413416
max_tag_value_length=self.max_tag_value_length,
414417
extra_codecs=self.propagation,
415418
throttler=throttler,
419+
scope_manager=self.scope_manager,
416420
)
417421

418422
def _initialize_global_tracer(self, tracer):

jaeger_client/tracer.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import opentracing
2626
from opentracing import Format, UnsupportedFormatException
2727
from opentracing.ext import tags as ext_tags
28+
from opentracing.scope_managers import ThreadLocalScopeManager
2829

2930
from . import constants
3031
from .codecs import TextCodec, ZipkinCodec, ZipkinSpanFormat, BinaryCodec
@@ -51,6 +52,7 @@ def __init__(
5152
tags=None,
5253
max_tag_value_length=constants.MAX_TAG_VALUE_LENGTH,
5354
throttler=None,
55+
scope_manager=None,
5456
):
5557
self.service_name = service_name
5658
self.reporter = reporter
@@ -109,12 +111,18 @@ def __init__(
109111
max_length=self.max_tag_value_length,
110112
)
111113

114+
super(Tracer, self).__init__(
115+
scope_manager=scope_manager or ThreadLocalScopeManager()
116+
)
117+
112118
def start_span(self,
113119
operation_name=None,
114120
child_of=None,
115121
references=None,
116122
tags=None,
117-
start_time=None):
123+
start_time=None,
124+
ignore_active_span=False,
125+
):
118126
"""
119127
Start and return a new Span representing a unit of work.
120128
@@ -129,18 +137,22 @@ def start_span(self,
129137
to avoid extra data copying.
130138
:param start_time: an explicit Span start time as a unix timestamp per
131139
time.time()
140+
:param ignore_active_span: an explicit flag that ignores the current
141+
active :class:`Scope` and creates a root :class:`Span`
132142
133143
:return: Returns an already-started Span instance.
134144
"""
135145
parent = child_of
136146

147+
if self.active_span is not None \
148+
and not ignore_active_span \
149+
and not parent:
150+
parent = self.active_span
151+
137152
# allow Span to be passed as reference, not just SpanContext
138153
if isinstance(parent, Span):
139154
parent = parent.context
140155

141-
rpc_server = tags and \
142-
tags.get(ext_tags.SPAN_KIND) == ext_tags.SPAN_KIND_RPC_SERVER
143-
144156
valid_references = None
145157
if references:
146158
valid_references = list()
@@ -154,6 +166,9 @@ def start_span(self,
154166
if valid_references and (parent is None or not parent.has_trace):
155167
parent = valid_references[0].referenced_context
156168

169+
rpc_server = tags and \
170+
tags.get(ext_tags.SPAN_KIND) == ext_tags.SPAN_KIND_RPC_SERVER
171+
157172
if parent is None or not parent.has_trace:
158173
trace_id = self._random_id(self.max_trace_id_bits)
159174
span_id = self._random_id(constants._max_id_bits)
@@ -197,6 +212,46 @@ def start_span(self,
197212

198213
return span
199214

215+
def start_active_span(self,
216+
operation_name=None,
217+
child_of=None,
218+
references=None,
219+
tags=None,
220+
start_time=None,
221+
ignore_active_span=False,
222+
finish_on_close=True,
223+
):
224+
"""
225+
Returns a newly started and activated :class:`Scope`
226+
227+
:param operation_name: name of the operation represented by the new
228+
span from the perspective of the current service.
229+
:param child_of: shortcut for 'child_of' reference
230+
:param references: (optional) either a single Reference object or a
231+
list of Reference objects that identify one or more parent
232+
SpanContexts. (See the Reference documentation for detail)
233+
:param tags: optional dictionary of Span Tags. The caller gives up
234+
ownership of that dictionary, because the Tracer may use it as-is
235+
to avoid extra data copying.
236+
:param start_time: an explicit Span start time as a unix timestamp per
237+
time.time()
238+
:param ignore_active_span: an explicit flag that ignores the current
239+
active :class:`Scope` and creates a root :class:`Span`
240+
:param finish_on_close: whether :class:`Span` should automatically be
241+
finished when :meth:`Scope.close()` is called.
242+
243+
:return: a :class:`Scope`, already registered via the :class:`ScopeManager`.
244+
"""
245+
span = self.start_span(
246+
operation_name=operation_name,
247+
child_of=child_of,
248+
references=references,
249+
tags=tags,
250+
start_time=start_time,
251+
ignore_active_span=ignore_active_span,
252+
)
253+
return self.scope_manager.activate(span, finish_on_close)
254+
200255
def inject(self, span_context, format, carrier):
201256
codec = self.codecs.get(format, None)
202257
if codec is None:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
'threadloop>=1,<2',
4141
'thrift',
4242
'tornado>=4.3,<5',
43-
'opentracing>=1.2.2,<2',
43+
'opentracing>=2.1,<3.0',
4444
],
4545
# Uncomment below if need to test with unreleased version of opentracing
4646
# dependency_links=[

tests/test_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ def tracer(self):
3434
def test_binary_propagation(self):
3535
# TODO binary codecs are not implemented at the moment
3636
pass
37+
38+
def is_parent(self, parent, span):
39+
return span.parent_id == getattr(parent, 'span_id', None)

0 commit comments

Comments
 (0)