|
8 | 8 | from sentry_sdk._compat import with_metaclass |
9 | 9 | from sentry_sdk.scope import Scope |
10 | 10 | from sentry_sdk.client import Client |
11 | | -from sentry_sdk.tracing import Span |
| 11 | +from sentry_sdk.tracing import Span, Transaction |
12 | 12 | from sentry_sdk.sessions import Session |
13 | 13 | from sentry_sdk.utils import ( |
14 | 14 | exc_info_from_error, |
@@ -441,38 +441,88 @@ def start_span( |
441 | 441 | ): |
442 | 442 | # type: (...) -> Span |
443 | 443 | """ |
444 | | - Create a new span whose parent span is the currently active |
445 | | - span, if any. The return value is the span object that can |
446 | | - be used as a context manager to start and stop timing. |
447 | | -
|
448 | | - Note that you will not see any span that is not contained |
449 | | - within a transaction. Create a transaction with |
450 | | - ``start_span(transaction="my transaction")`` if an |
451 | | - integration doesn't already do this for you. |
| 444 | + Create and start timing a new span whose parent is the currently active |
| 445 | + span or transaction, if any. The return value is a span instance, |
| 446 | + typically used as a context manager to start and stop timing in a `with` |
| 447 | + block. |
| 448 | +
|
| 449 | + Only spans contained in a transaction are sent to Sentry. Most |
| 450 | + integrations start a transaction at the appropriate time, for example |
| 451 | + for every incoming HTTP request. Use `start_transaction` to start a new |
| 452 | + transaction when one is not already in progress. |
452 | 453 | """ |
| 454 | + # TODO: consider removing this in a future release. |
| 455 | + # This is for backwards compatibility with releases before |
| 456 | + # start_transaction existed, to allow for a smoother transition. |
| 457 | + if isinstance(span, Transaction) or "transaction" in kwargs: |
| 458 | + deprecation_msg = ( |
| 459 | + "Deprecated: use start_transaction to start transactions and " |
| 460 | + "Transaction.start_child to start spans." |
| 461 | + ) |
| 462 | + if isinstance(span, Transaction): |
| 463 | + logger.warning(deprecation_msg) |
| 464 | + return self.start_transaction(span) |
| 465 | + if "transaction" in kwargs: |
| 466 | + logger.warning(deprecation_msg) |
| 467 | + name = kwargs.pop("transaction") |
| 468 | + return self.start_transaction(name=name, **kwargs) |
453 | 469 |
|
454 | | - client, scope = self._stack[-1] |
| 470 | + if span is not None: |
| 471 | + return span |
455 | 472 |
|
456 | 473 | kwargs.setdefault("hub", self) |
457 | 474 |
|
458 | | - if span is None: |
459 | | - span = scope.span |
460 | | - if span is not None: |
461 | | - span = span.new_span(**kwargs) |
462 | | - else: |
463 | | - span = Span(**kwargs) |
| 475 | + span = self.scope.span |
| 476 | + if span is not None: |
| 477 | + return span.start_child(**kwargs) |
| 478 | + |
| 479 | + return Span(**kwargs) |
| 480 | + |
| 481 | + def start_transaction( |
| 482 | + self, |
| 483 | + transaction=None, # type: Optional[Transaction] |
| 484 | + **kwargs # type: Any |
| 485 | + ): |
| 486 | + # type: (...) -> Transaction |
| 487 | + """ |
| 488 | + Start and return a transaction. |
| 489 | +
|
| 490 | + Start an existing transaction if given, otherwise create and start a new |
| 491 | + transaction with kwargs. |
| 492 | +
|
| 493 | + This is the entry point to manual tracing instrumentation. |
| 494 | +
|
| 495 | + A tree structure can be built by adding child spans to the transaction, |
| 496 | + and child spans to other spans. To start a new child span within the |
| 497 | + transaction or any span, call the respective `.start_child()` method. |
| 498 | +
|
| 499 | + Every child span must be finished before the transaction is finished, |
| 500 | + otherwise the unfinished spans are discarded. |
| 501 | +
|
| 502 | + When used as context managers, spans and transactions are automatically |
| 503 | + finished at the end of the `with` block. If not using context managers, |
| 504 | + call the `.finish()` method. |
| 505 | +
|
| 506 | + When the transaction is finished, it will be sent to Sentry with all its |
| 507 | + finished child spans. |
| 508 | + """ |
| 509 | + if transaction is None: |
| 510 | + kwargs.setdefault("hub", self) |
| 511 | + transaction = Transaction(**kwargs) |
| 512 | + |
| 513 | + client, scope = self._stack[-1] |
464 | 514 |
|
465 | | - if span.sampled is None and span.transaction is not None: |
| 515 | + if transaction.sampled is None: |
466 | 516 | sample_rate = client and client.options["traces_sample_rate"] or 0 |
467 | | - span.sampled = random.random() < sample_rate |
| 517 | + transaction.sampled = random.random() < sample_rate |
468 | 518 |
|
469 | | - if span.sampled: |
| 519 | + if transaction.sampled: |
470 | 520 | max_spans = ( |
471 | 521 | client and client.options["_experiments"].get("max_spans") or 1000 |
472 | 522 | ) |
473 | | - span.init_finished_spans(maxlen=max_spans) |
| 523 | + transaction.init_span_recorder(maxlen=max_spans) |
474 | 524 |
|
475 | | - return span |
| 525 | + return transaction |
476 | 526 |
|
477 | 527 | @overload # noqa |
478 | 528 | def push_scope( |
|
0 commit comments