@@ -111,6 +111,11 @@ class Span(object):
111
111
112
112
def __new__ (cls , ** kwargs ):
113
113
# type: (**Any) -> Any
114
+ """
115
+ Backwards-compatible implementation of Span and Transaction
116
+ creation.
117
+ """
118
+
114
119
# TODO: consider removing this in a future release.
115
120
# This is for backwards compatibility with releases before Transaction
116
121
# existed, to allow for a smoother transition.
@@ -166,8 +171,10 @@ def init_span_recorder(self, maxlen):
166
171
167
172
def __repr__ (self ):
168
173
# type: () -> str
169
- return "<%s(trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r)>" % (
174
+ return "<%s(op=%r, description:%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r)>" % (
170
175
self .__class__ .__name__ ,
176
+ self .op ,
177
+ self .description ,
171
178
self .trace_id ,
172
179
self .span_id ,
173
180
self .parent_span_id ,
@@ -200,8 +207,9 @@ def start_child(self, **kwargs):
200
207
"""
201
208
Start a sub-span from the current span or transaction.
202
209
203
- Takes the same arguments as the initializer of :py:class:`Span`. No
204
- attributes other than the sample rate are inherited.
210
+ Takes the same arguments as the initializer of :py:class:`Span`. The
211
+ trace id, sampling decision, and span recorder are inherited from the
212
+ current span/transaction.
205
213
"""
206
214
kwargs .setdefault ("sampled" , self .sampled )
207
215
@@ -227,6 +235,14 @@ def continue_from_environ(
227
235
** kwargs # type: Any
228
236
):
229
237
# type: (...) -> Transaction
238
+ """
239
+ Create a Transaction with the given params, then add in data pulled from
240
+ the 'sentry-trace' header in the environ (if any) before returning the
241
+ Transaction.
242
+
243
+ If the 'sentry-trace' header is malformed or missing, just create and
244
+ return a Transaction instance with the given params.
245
+ """
230
246
if cls is Span :
231
247
logger .warning (
232
248
"Deprecated: use Transaction.continue_from_environ "
@@ -241,16 +257,25 @@ def continue_from_headers(
241
257
** kwargs # type: Any
242
258
):
243
259
# type: (...) -> Transaction
260
+ """
261
+ Create a Transaction with the given params, then add in data pulled from
262
+ the 'sentry-trace' header (if any) before returning the Transaction.
263
+
264
+ If the 'sentry-trace' header is malformed or missing, just create and
265
+ return a Transaction instance with the given params.
266
+ """
244
267
if cls is Span :
245
268
logger .warning (
246
269
"Deprecated: use Transaction.continue_from_headers "
247
270
"instead of Span.continue_from_headers."
248
271
)
249
- parent = Transaction .from_traceparent (headers .get ("sentry-trace" ), ** kwargs )
250
- if parent is None :
251
- parent = Transaction (** kwargs )
252
- parent .same_process_as_parent = False
253
- return parent
272
+ transaction = Transaction .from_traceparent (
273
+ headers .get ("sentry-trace" ), ** kwargs
274
+ )
275
+ if transaction is None :
276
+ transaction = Transaction (** kwargs )
277
+ transaction .same_process_as_parent = False
278
+ return transaction
254
279
255
280
def iter_headers (self ):
256
281
# type: () -> Generator[Tuple[str, str], None, None]
@@ -263,6 +288,13 @@ def from_traceparent(
263
288
** kwargs # type: Any
264
289
):
265
290
# type: (...) -> Optional[Transaction]
291
+ """
292
+ Create a Transaction with the given params, then add in data pulled from
293
+ the given 'sentry-trace' header value before returning the Transaction.
294
+
295
+ If the header value is malformed or missing, just create and return a
296
+ Transaction instance with the given params.
297
+ """
266
298
if cls is Span :
267
299
logger .warning (
268
300
"Deprecated: use Transaction.from_traceparent "
@@ -279,20 +311,23 @@ def from_traceparent(
279
311
if match is None :
280
312
return None
281
313
282
- trace_id , span_id , sampled_str = match .groups ()
314
+ trace_id , parent_span_id , sampled_str = match .groups ()
283
315
284
316
if trace_id is not None :
285
317
trace_id = "{:032x}" .format (int (trace_id , 16 ))
286
- if span_id is not None :
287
- span_id = "{:016x}" .format (int (span_id , 16 ))
318
+ if parent_span_id is not None :
319
+ parent_span_id = "{:016x}" .format (int (parent_span_id , 16 ))
288
320
289
321
if sampled_str :
290
- sampled = sampled_str != "0" # type: Optional[bool]
322
+ parent_sampled = sampled_str != "0" # type: Optional[bool]
291
323
else :
292
- sampled = None
324
+ parent_sampled = None
293
325
294
326
return Transaction (
295
- trace_id = trace_id , parent_span_id = span_id , sampled = sampled , ** kwargs
327
+ trace_id = trace_id ,
328
+ parent_span_id = parent_span_id ,
329
+ sampled = parent_sampled ,
330
+ ** kwargs
296
331
)
297
332
298
333
def to_traceparent (self ):
@@ -436,16 +471,14 @@ def __init__(
436
471
437
472
def __repr__ (self ):
438
473
# type: () -> str
439
- return (
440
- "<%s(name=%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r)>"
441
- % (
442
- self .__class__ .__name__ ,
443
- self .name ,
444
- self .trace_id ,
445
- self .span_id ,
446
- self .parent_span_id ,
447
- self .sampled ,
448
- )
474
+ return "<%s(name=%r, op=%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r)>" % (
475
+ self .__class__ .__name__ ,
476
+ self .name ,
477
+ self .op ,
478
+ self .trace_id ,
479
+ self .span_id ,
480
+ self .parent_span_id ,
481
+ self .sampled ,
449
482
)
450
483
451
484
def finish (self , hub = None ):
@@ -454,7 +487,9 @@ def finish(self, hub=None):
454
487
# This transaction is already finished, ignore.
455
488
return None
456
489
490
+ # This is a de facto proxy for checking if sampled = False
457
491
if self ._span_recorder is None :
492
+ logger .debug ("Discarding transaction because sampled = False" )
458
493
return None
459
494
460
495
hub = hub or self .hub or sentry_sdk .Hub .current
0 commit comments