|
4 | 4 | from datetime import datetime
|
5 | 5 | from contextlib import contextmanager
|
6 | 6 |
|
7 |
| -from sentry_sdk.api import get_traceparent, get_baggage |
8 | 7 | from sentry_sdk._compat import with_metaclass
|
9 | 8 | from sentry_sdk.consts import INSTRUMENTER
|
10 | 9 | from sentry_sdk.scope import Scope
|
|
18 | 17 | SENTRY_TRACE_HEADER_NAME,
|
19 | 18 | )
|
20 | 19 | from sentry_sdk.session import Session
|
21 |
| -from sentry_sdk.tracing_utils import has_tracing_enabled |
| 20 | +from sentry_sdk.tracing_utils import ( |
| 21 | + has_tracing_enabled, |
| 22 | + normalize_incoming_data, |
| 23 | +) |
| 24 | + |
22 | 25 | from sentry_sdk.utils import (
|
23 | 26 | exc_info_from_error,
|
24 | 27 | event_from_exception,
|
@@ -540,6 +543,22 @@ def start_transaction(
|
540 | 543 |
|
541 | 544 | return transaction
|
542 | 545 |
|
| 546 | + def continue_trace(self, environ_or_headers, op=None, name=None, source=None): |
| 547 | + # type: (Dict[str, Any], Optional[str], Optional[str], Optional[str]) -> Transaction |
| 548 | + """ |
| 549 | + Sets the propagation context from environment or headers and returns a transaction. |
| 550 | + """ |
| 551 | + with self.configure_scope() as scope: |
| 552 | + scope.generate_propagation_context(environ_or_headers) |
| 553 | + |
| 554 | + transaction = Transaction.continue_from_headers( |
| 555 | + normalize_incoming_data(environ_or_headers), |
| 556 | + op=op, |
| 557 | + name=name, |
| 558 | + source=source, |
| 559 | + ) |
| 560 | + return transaction |
| 561 | + |
543 | 562 | @overload
|
544 | 563 | def push_scope(
|
545 | 564 | self, callback=None # type: Optional[None]
|
@@ -706,6 +725,36 @@ def flush(
|
706 | 725 | if client is not None:
|
707 | 726 | return client.flush(timeout=timeout, callback=callback)
|
708 | 727 |
|
| 728 | + def get_traceparent(self): |
| 729 | + # type: () -> Optional[str] |
| 730 | + """ |
| 731 | + Returns the traceparent either from the active span or from the scope. |
| 732 | + """ |
| 733 | + if self.client is not None: |
| 734 | + if has_tracing_enabled(self.client.options) and self.scope.span is not None: |
| 735 | + return self.scope.span.to_traceparent() |
| 736 | + |
| 737 | + return self.scope.get_traceparent() |
| 738 | + |
| 739 | + def get_baggage(self): |
| 740 | + # type: () -> Optional[str] |
| 741 | + """ |
| 742 | + Returns Baggage either from the active span or from the scope. |
| 743 | + """ |
| 744 | + if ( |
| 745 | + self.client is not None |
| 746 | + and has_tracing_enabled(self.client.options) |
| 747 | + and self.scope.span is not None |
| 748 | + ): |
| 749 | + baggage = self.scope.span.to_baggage() |
| 750 | + else: |
| 751 | + baggage = self.scope.get_baggage() |
| 752 | + |
| 753 | + if baggage is not None: |
| 754 | + return baggage.serialize() |
| 755 | + |
| 756 | + return None |
| 757 | + |
709 | 758 | def iter_trace_propagation_headers(self, span=None):
|
710 | 759 | # type: (Optional[Span]) -> Generator[Tuple[str, str], None, None]
|
711 | 760 | """
|
@@ -740,14 +789,14 @@ def trace_propagation_meta(self, span=None):
|
740 | 789 |
|
741 | 790 | meta = ""
|
742 | 791 |
|
743 |
| - sentry_trace = get_traceparent() |
| 792 | + sentry_trace = self.get_traceparent() |
744 | 793 | if sentry_trace is not None:
|
745 | 794 | meta += '<meta name="%s" content="%s">' % (
|
746 | 795 | SENTRY_TRACE_HEADER_NAME,
|
747 | 796 | sentry_trace,
|
748 | 797 | )
|
749 | 798 |
|
750 |
| - baggage = get_baggage() |
| 799 | + baggage = self.get_baggage() |
751 | 800 | if baggage is not None:
|
752 | 801 | meta += '<meta name="%s" content="%s">' % (BAGGAGE_HEADER_NAME, baggage)
|
753 | 802 |
|
|
0 commit comments