@@ -80,6 +80,9 @@ def add(self, span):
80
80
81
81
82
82
class Span (object ):
83
+ """A span holds timing information of a block of code.
84
+ Spans can have multiple child spans thus forming a span tree."""
85
+
83
86
__slots__ = (
84
87
"trace_id" ,
85
88
"span_id" ,
@@ -201,6 +204,9 @@ def __exit__(self, ty, value, tb):
201
204
@property
202
205
def containing_transaction (self ):
203
206
# type: () -> Optional[Transaction]
207
+ """The ``Transaction`` that this span belongs to.
208
+ The ``Transaction`` is the root of the span tree,
209
+ so one could also think of this ``Transaction`` as the "root span"."""
204
210
205
211
# this is a getter rather than a regular attribute so that transactions
206
212
# can return `self` here instead (as a way to prevent them circularly
@@ -237,12 +243,15 @@ def start_child(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
237
243
)
238
244
if span_recorder :
239
245
span_recorder .add (child )
246
+
240
247
return child
241
248
242
249
def new_span (self , ** kwargs ):
243
250
# type: (**Any) -> Span
244
- """Deprecated: use :py:meth:`sentry_sdk.tracing.Span.start_child` instead."""
245
- logger .warning ("Deprecated: use Span.start_child instead of Span.new_span." )
251
+ """DEPRECATED: use :py:meth:`sentry_sdk.tracing.Span.start_child` instead."""
252
+ logger .warning (
253
+ "Deprecated: use Span.start_child instead of Span.new_span. This will be removed in the future."
254
+ )
246
255
return self .start_child (** kwargs )
247
256
248
257
@classmethod
@@ -254,12 +263,15 @@ def continue_from_environ(
254
263
# type: (...) -> Transaction
255
264
"""
256
265
Create a Transaction with the given params, then add in data pulled from
257
- the ' sentry-trace' and ' baggage' headers from the environ (if any)
266
+ the `` sentry-trace`` and `` baggage`` headers from the environ (if any)
258
267
before returning the Transaction.
259
268
260
- This is different from `continue_from_headers` in that it assumes header
261
- names in the form "HTTP_HEADER_NAME" - such as you would get from a wsgi
262
- environ - rather than the form "header-name".
269
+ This is different from :py:meth:`~sentry_sdk.tracing.Span.continue_from_headers`
270
+ in that it assumes header names in the form ``HTTP_HEADER_NAME`` -
271
+ such as you would get from a WSGI/ASGI environ -
272
+ rather than the form ``header-name``.
273
+
274
+ :param environ: The ASGI/WSGI environ to pull information from.
263
275
"""
264
276
if cls is Span :
265
277
logger .warning (
@@ -277,7 +289,9 @@ def continue_from_headers(
277
289
# type: (...) -> Transaction
278
290
"""
279
291
Create a transaction with the given params (including any data pulled from
280
- the 'sentry-trace' and 'baggage' headers).
292
+ the ``sentry-trace`` and ``baggage`` headers).
293
+
294
+ :param headers: The dictionary with the HTTP headers to pull information from.
281
295
"""
282
296
# TODO move this to the Transaction class
283
297
if cls is Span :
@@ -311,8 +325,8 @@ def continue_from_headers(
311
325
def iter_headers (self ):
312
326
# type: () -> Iterator[Tuple[str, str]]
313
327
"""
314
- Creates a generator which returns the span's `sentry-trace` and `baggage` headers.
315
- If the span's containing transaction doesn't yet have a `baggage` value,
328
+ Creates a generator which returns the span's `` sentry-trace`` and `` baggage` ` headers.
329
+ If the span's containing transaction doesn't yet have a `` baggage` ` value,
316
330
this will cause one to be generated and stored.
317
331
"""
318
332
yield SENTRY_TRACE_HEADER_NAME , self .to_traceparent ()
@@ -330,10 +344,10 @@ def from_traceparent(
330
344
):
331
345
# type: (...) -> Optional[Transaction]
332
346
"""
333
- DEPRECATED: Use :py:meth:`sentry_sdk.tracing.Transaction .continue_from_headers`.
347
+ DEPRECATED: Use :py:meth:`sentry_sdk.tracing.Span .continue_from_headers`.
334
348
335
- Create a `Transaction` with the given params, then add in data pulled from
336
- the given ' sentry-trace' header value before returning the `Transaction`.
349
+ Create a `` Transaction` ` with the given params, then add in data pulled from
350
+ the given `` sentry-trace`` header value before returning the `` Transaction` `.
337
351
"""
338
352
logger .warning (
339
353
"Deprecated: Use Transaction.continue_from_headers(headers, **kwargs) "
@@ -364,6 +378,9 @@ def to_traceparent(self):
364
378
365
379
def to_baggage (self ):
366
380
# type: () -> Optional[Baggage]
381
+ """Returns the :py:class:`~sentry_sdk.tracing_utils.Baggage`
382
+ associated with this ``Span``, if any. (Taken from the root of the span tree.)
383
+ """
367
384
if self .containing_transaction :
368
385
return self .containing_transaction .get_baggage ()
369
386
return None
@@ -422,8 +439,21 @@ def is_success(self):
422
439
423
440
def finish (self , hub = None , end_timestamp = None ):
424
441
# type: (Optional[sentry_sdk.Hub], Optional[datetime]) -> Optional[str]
425
- # XXX : would be type: (Optional[sentry_sdk.Hub]) -> None, but that leads
442
+ # Note : would be type: (Optional[sentry_sdk.Hub]) -> None, but that leads
426
443
# to incompatible return types for Span.finish and Transaction.finish.
444
+ """Sets the end timestamp of the span.
445
+ Additionally it also creates a breadcrumb from the span,
446
+ if the span represents a database or HTTP request.
447
+
448
+ :param hub: The hub to use for this transaction.
449
+ If not provided, the current hub will be used.
450
+ :param end_timestamp: Optional timestamp that should
451
+ be used as timestamp instead of the current time.
452
+
453
+ :return: Always ``None``. The type is ``Optional[str]`` to match
454
+ the return value of :py:meth:`sentry_sdk.tracing.Transaction.finish`.
455
+ """
456
+
427
457
if self .timestamp is not None :
428
458
# This span is already finished, ignore.
429
459
return None
@@ -446,6 +476,8 @@ def finish(self, hub=None, end_timestamp=None):
446
476
447
477
def to_json (self ):
448
478
# type: () -> Dict[str, Any]
479
+ """Returns a JSON-compatible representation of the span."""
480
+
449
481
rv = {
450
482
"trace_id" : self .trace_id ,
451
483
"span_id" : self .span_id ,
@@ -491,6 +523,9 @@ def get_trace_context(self):
491
523
492
524
493
525
class Transaction (Span ):
526
+ """The Transaction is the root element that holds all the spans
527
+ for Sentry performance instrumentation."""
528
+
494
529
__slots__ = (
495
530
"name" ,
496
531
"source" ,
@@ -512,6 +547,19 @@ def __init__(
512
547
** kwargs # type: Any
513
548
):
514
549
# type: (...) -> None
550
+ """Constructs a new Transaction.
551
+
552
+ :param name: Identifier of the transaction.
553
+ Will show up in the Sentry UI.
554
+ :param parent_sampled: Whether the parent transaction was sampled.
555
+ If True this transaction will be kept, if False it will be discarded.
556
+ :param baggage: The W3C baggage header value.
557
+ (see https://www.w3.org/TR/baggage/)
558
+ :param source: A string describing the source of the transaction name.
559
+ This will be used to determine the transaction's type.
560
+ See https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations
561
+ for more information. Default "custom".
562
+ """
515
563
# TODO: consider removing this in a future release.
516
564
# This is for backwards compatibility with releases before Transaction
517
565
# existed, to allow for a smoother transition.
@@ -522,7 +570,7 @@ def __init__(
522
570
)
523
571
name = kwargs .pop ("transaction" )
524
572
525
- Span . __init__ ( self , ** kwargs )
573
+ super ( Transaction , self ). __init__ ( ** kwargs )
526
574
527
575
self .name = name
528
576
self .source = source
@@ -568,6 +616,9 @@ def __exit__(self, ty, value, tb):
568
616
@property
569
617
def containing_transaction (self ):
570
618
# type: () -> Transaction
619
+ """The root element of the span tree.
620
+ In the case of a transaction it is the transaction itself.
621
+ """
571
622
572
623
# Transactions (as spans) belong to themselves (as transactions). This
573
624
# is a getter rather than a regular attribute to avoid having a circular
@@ -576,6 +627,17 @@ def containing_transaction(self):
576
627
577
628
def finish (self , hub = None , end_timestamp = None ):
578
629
# type: (Optional[sentry_sdk.Hub], Optional[datetime]) -> Optional[str]
630
+ """Finishes the transaction and sends it to Sentry.
631
+ All finished spans in the transaction will also be sent to Sentry.
632
+
633
+ :param hub: The hub to use for this transaction.
634
+ If not provided, the current hub will be used.
635
+ :param end_timestamp: Optional timestamp that should
636
+ be used as timestamp instead of the current time.
637
+
638
+ :return: The event ID if the transaction was sent to Sentry,
639
+ otherwise None.
640
+ """
579
641
if self .timestamp is not None :
580
642
# This transaction is already finished, ignore.
581
643
return None
@@ -610,7 +672,7 @@ def finish(self, hub=None, end_timestamp=None):
610
672
)
611
673
self .name = "<unlabeled transaction>"
612
674
613
- Span . finish ( self , hub , end_timestamp )
675
+ super ( Transaction , self ). finish ( hub , end_timestamp )
614
676
615
677
if not self .sampled :
616
678
# At this point a `sampled = None` should have already been resolved
@@ -661,15 +723,26 @@ def set_measurement(self, name, value, unit=""):
661
723
662
724
def set_context (self , key , value ):
663
725
# type: (str, Any) -> None
726
+ """Sets a context. Transactions can have multiple contexts
727
+ and they should follow the format described in the "Contexts Interface"
728
+ documentation.
729
+
730
+ :param key: The name of the context.
731
+ :param value: The information about the context.
732
+ """
664
733
self ._contexts [key ] = value
665
734
666
735
def set_http_status (self , http_status ):
667
736
# type: (int) -> None
737
+ """Sets the status of the Transaction according to the given HTTP status.
738
+
739
+ :param http_status: The HTTP status code."""
668
740
super (Transaction , self ).set_http_status (http_status )
669
741
self .set_context ("response" , {"status_code" : http_status })
670
742
671
743
def to_json (self ):
672
744
# type: () -> Dict[str, Any]
745
+ """Returns a JSON-compatible representation of the transaction."""
673
746
rv = super (Transaction , self ).to_json ()
674
747
675
748
rv ["name" ] = self .name
@@ -680,10 +753,12 @@ def to_json(self):
680
753
681
754
def get_baggage (self ):
682
755
# type: () -> Baggage
683
- """
684
- The first time a new baggage with sentry items is made,
685
- it will be frozen.
686
- """
756
+ """Returns the :py:class:`~sentry_sdk.tracing_utils.Baggage`
757
+ associated with the Transaction.
758
+
759
+ The first time a new baggage with Sentry items is made,
760
+ it will be frozen."""
761
+
687
762
if not self ._baggage or self ._baggage .mutable :
688
763
self ._baggage = Baggage .populate_from_transaction (self )
689
764
0 commit comments