This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
tracer.py
348 lines (268 loc) · 12.7 KB
/
tracer.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# Copyright The OpenTracing Authors
# Copyright Uber Technologies, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from collections import namedtuple
from .span import Span
from .span import SpanContext
from .scope import Scope
from .scope_manager import ScopeManager
from .propagation import Format, UnsupportedFormatException
class Tracer(object):
"""Tracer is the entry point API between instrumentation code and the
tracing implementation.
This implementation both defines the public Tracer API, and provides
a default no-op behavior.
"""
_supported_formats = [Format.TEXT_MAP, Format.BINARY, Format.HTTP_HEADERS]
def __init__(self, scope_manager=None):
self._scope_manager = ScopeManager() if scope_manager is None \
else scope_manager
self._noop_span_context = SpanContext()
self._noop_span = Span(tracer=self, context=self._noop_span_context)
self._noop_scope = Scope(self._scope_manager, self._noop_span)
@property
def scope_manager(self):
"""Provides access to the current :class:`~opentracing.ScopeManager`.
:rtype: :class:`~opentracing.ScopeManager`
"""
return self._scope_manager
@property
def active_span(self):
"""Provides access to the the active :class:`Span`. This is a shorthand for
:attr:`Tracer.scope_manager.active.span`, and ``None`` will be
returned if :attr:`Scope.span` is ``None``.
:rtype: :class:`~opentracing.Span`
:return: the active :class:`Span`.
"""
scope = self._scope_manager.active
return None if scope is None else scope.span
def start_active_span(self,
operation_name,
child_of=None,
references=None,
tags=None,
start_time=None,
ignore_active_span=False,
finish_on_close=True):
"""Returns a newly started and activated :class:`Scope`.
The returned :class:`Scope` supports with-statement contexts. For
example::
with tracer.start_active_span('...') as scope:
scope.span.set_tag('http.method', 'GET')
do_some_work()
# Span.finish() is called as part of scope deactivation through
# the with statement.
It's also possible to not finish the :class:`Span` when the
:class:`Scope` context expires::
with tracer.start_active_span('...',
finish_on_close=False) as scope:
scope.span.set_tag('http.method', 'GET')
do_some_work()
# Span.finish() is not called as part of Scope deactivation as
# `finish_on_close` is `False`.
:param operation_name: name of the operation represented by the new
:class:`Span` from the perspective of the current service.
:type operation_name: str
:param child_of: (optional) a :class:`Span` or :class:`SpanContext`
instance representing the parent in a REFERENCE_CHILD_OF reference.
If specified, the `references` parameter must be omitted.
:type child_of: Span or SpanContext
:param references: (optional) references that identify one or more
parent :class:`SpanContext`\\ s. (See the Reference documentation
for detail).
:type references: :obj:`list` of :class:`Reference`
:param tags: an optional dictionary of :class:`Span` tags. The caller
gives up ownership of that dictionary, because the :class:`Tracer`
may use it as-is to avoid extra data copying.
:type tags: dict
:param start_time: an explicit :class:`Span` start time as a unix
timestamp per :meth:`time.time()`.
:type start_time: float
:param ignore_active_span: (optional) an explicit flag that ignores
the current active :class:`Scope` and creates a root :class:`Span`.
:type ignore_active_span: bool
:param finish_on_close: whether :class:`Span` should automatically be
finished when :meth:`Scope.close()` is called.
:type finish_on_close: bool
:rtype: Scope
:return: a :class:`Scope`, already registered via the
:class:`ScopeManager`.
"""
return self._noop_scope
def start_span(self,
operation_name=None,
child_of=None,
references=None,
tags=None,
start_time=None,
ignore_active_span=False):
"""Starts and returns a new :class:`Span` representing a unit of work.
Starting a root :class:`Span` (a :class:`Span` with no causal
references)::
tracer.start_span('...')
Starting a child :class:`Span` (see also :meth:`start_child_span()`)::
tracer.start_span(
'...',
child_of=parent_span)
Starting a child :class:`Span` in a more verbose way::
tracer.start_span(
'...',
references=[opentracing.child_of(parent_span)])
:param operation_name: name of the operation represented by the new
:class:`Span` from the perspective of the current service.
:type operation_name: str
:param child_of: (optional) a :class:`Span` or :class:`SpanContext`
representing the parent in a REFERENCE_CHILD_OF reference. If
specified, the `references` parameter must be omitted.
:type child_of: Span or SpanContext
:param references: (optional) references that identify one or more
parent :class:`SpanContext`\\ s. (See the Reference documentation
for detail).
:type references: :obj:`list` of :class:`Reference`
:param tags: an optional dictionary of :class:`Span` tags. The caller
gives up ownership of that dictionary, because the :class:`Tracer`
may use it as-is to avoid extra data copying.
:type tags: dict
:param start_time: an explicit Span start time as a unix timestamp per
:meth:`time.time()`
:type start_time: float
:param ignore_active_span: an explicit flag that ignores the current
active :class:`Scope` and creates a root :class:`Span`.
:type ignore_active_span: bool
:rtype: Span
:return: an already-started :class:`Span` instance.
"""
return self._noop_span
def inject(self, span_context, format, carrier):
"""Injects `span_context` into `carrier`.
The type of `carrier` is determined by `format`. See the
:class:`Format` class/namespace for the built-in OpenTracing formats.
Implementations *must* raise :exc:`UnsupportedFormatException` if
`format` is unknown or disallowed.
:param span_context: the :class:`SpanContext` instance to inject
:type span_context: SpanContext
:param format: a python object instance that represents a given
carrier format. `format` may be of any type, and `format` equality
is defined by python ``==`` equality.
:type format: Format
:param carrier: the format-specific carrier object to inject into
"""
if format in Tracer._supported_formats:
return
raise UnsupportedFormatException(format)
def extract(self, format, carrier):
"""Returns a :class:`SpanContext` instance extracted from a `carrier` of the
given `format`, or ``None`` if no such :class:`SpanContext` could be
found.
The type of `carrier` is determined by `format`. See the
:class:`Format` class/namespace for the built-in OpenTracing formats.
Implementations *must* raise :exc:`UnsupportedFormatException` if
`format` is unknown or disallowed.
Implementations may raise :exc:`InvalidCarrierException`,
:exc:`SpanContextCorruptedException`, or implementation-specific errors
if there are problems with `carrier`.
:param format: a python object instance that represents a given
carrier format. `format` may be of any type, and `format` equality
is defined by python ``==`` equality.
:param carrier: the format-specific carrier object to extract from
:rtype: SpanContext
:return: a :class:`SpanContext` extracted from `carrier` or ``None`` if
no such :class:`SpanContext` could be found.
"""
if format in Tracer._supported_formats:
return self._noop_span_context
raise UnsupportedFormatException(format)
class ReferenceType(object):
"""A namespace for OpenTracing reference types.
See http://opentracing.io/spec for more detail about references,
reference types, and CHILD_OF and FOLLOWS_FROM in particular.
"""
CHILD_OF = 'child_of'
FOLLOWS_FROM = 'follows_from'
# We use namedtuple since references are meant to be immutable.
# We subclass it to expose a standard docstring.
class Reference(namedtuple('Reference', ['type', 'referenced_context'])):
"""A Reference pairs a reference type with a referenced :class:`SpanContext`.
References are used by :meth:`Tracer.start_span()` to describe the
relationships between :class:`Span`\\ s.
:class:`Tracer` implementations must ignore references where
referenced_context is ``None``. This behavior allows for simpler code when
an inbound RPC request contains no tracing information and as a result
:meth:`Tracer.extract()` returns ``None``::
parent_ref = tracer.extract(opentracing.HTTP_HEADERS, request.headers)
span = tracer.start_span(
'operation', references=child_of(parent_ref)
)
See :meth:`child_of` and :meth:`follows_from` helpers for creating these
references.
"""
pass
def child_of(referenced_context=None):
"""child_of is a helper that creates CHILD_OF References.
:param referenced_context: the (causal parent) :class:`SpanContext` to
reference. If ``None`` is passed, this reference must be ignored by
the :class:`Tracer`.
:type referenced_context: SpanContext
:rtype: Reference
:return: A reference suitable for ``Tracer.start_span(...,
references=...)``
"""
return Reference(
type=ReferenceType.CHILD_OF,
referenced_context=referenced_context)
def follows_from(referenced_context=None):
"""follows_from is a helper that creates FOLLOWS_FROM References.
:param referenced_context: the (causal parent) :class:`SpanContext` to
reference. If ``None`` is passed, this reference must be ignored by the
:class:`Tracer`.
:type referenced_context: SpanContext
:rtype: Reference
:return: A Reference suitable for ``Tracer.start_span(...,
references=...)``
"""
return Reference(
type=ReferenceType.FOLLOWS_FROM,
referenced_context=referenced_context)
def start_child_span(parent_span, operation_name, tags=None, start_time=None):
"""A shorthand method that starts a `child_of` :class:`Span` for a given
parent :class:`Span`.
Equivalent to calling::
parent_span.tracer().start_span(
operation_name,
references=opentracing.child_of(parent_span.context),
tags=tags,
start_time=start_time)
:param parent_span: the :class:`Span` which will act as the parent in the
returned :class:`Span`\\ s child_of reference.
:type parent_span: Span
:param operation_name: the operation name for the child :class:`Span`
instance
:type operation_name: str
:param tags: optional dict of :class:`Span` tags. The caller gives up
ownership of that dict, because the :class:`Tracer` may use it as-is to
avoid extra data copying.
:type tags: dict
:param start_time: an explicit :class:`Span` start time as a unix timestamp
per :meth:`time.time()`.
:type start_time: float
:rtype: Span
:return: an already-started :class:`Span` instance.
"""
return parent_span.tracer.start_span(
operation_name=operation_name,
child_of=parent_span,
tags=tags,
start_time=start_time
)