|
107 | 107 |
|
108 | 108 | global_event_processors = [] # type: List[EventProcessor]
|
109 | 109 |
|
| 110 | +# A function returning a (trace_id, span_id) tuple |
| 111 | +# from an external tracing source (such as otel) |
| 112 | +_external_propagation_context_fn = None # type: Optional[Callable[[], Optional[Tuple[str, str]]]] |
| 113 | + |
110 | 114 |
|
111 | 115 | class ScopeType(Enum):
|
112 | 116 | CURRENT = "current"
|
@@ -142,6 +146,25 @@ def add_global_event_processor(processor):
|
142 | 146 | global_event_processors.append(processor)
|
143 | 147 |
|
144 | 148 |
|
| 149 | +def register_external_propagation_context(fn): |
| 150 | + # type: (Callable[[], Optional[Tuple[str, str]]]) -> None |
| 151 | + global _external_propagation_context_fn |
| 152 | + _external_propagation_context_fn = fn |
| 153 | + |
| 154 | + |
| 155 | +def remove_external_propagation_context(): |
| 156 | + # type: () -> None |
| 157 | + global _external_propagation_context_fn |
| 158 | + _external_propagation_context_fn = None |
| 159 | + |
| 160 | + |
| 161 | +def get_external_propagation_context(): |
| 162 | + # type: () -> Optional[Tuple[str, str]] |
| 163 | + return ( |
| 164 | + _external_propagation_context_fn() if _external_propagation_context_fn else None |
| 165 | + ) |
| 166 | + |
| 167 | + |
145 | 168 | def _attr_setter(fn):
|
146 | 169 | # type: (Any) -> Any
|
147 | 170 | return property(fset=fn, doc=fn.__doc__)
|
@@ -562,21 +585,29 @@ def get_baggage(self, *args, **kwargs):
|
562 | 585 | return self.get_isolation_scope().get_baggage()
|
563 | 586 |
|
564 | 587 | def get_trace_context(self):
|
565 | | - # type: () -> Any |
| 588 | + # type: () -> Dict[str, Any] |
566 | 589 | """
|
567 | 590 | Returns the Sentry "trace" context from the Propagation Context.
|
568 | 591 | """
|
569 | | - if self._propagation_contextis None: |
570 | | - return None |
| 592 | + if has_tracing_enabled(self.get_client().options) andself._spanisnot None: |
| 593 | + return self._span.get_trace_context() |
571 | 594 |
|
572 | | - trace_context = { |
573 | | - "trace_id": self._propagation_context.trace_id, |
574 | | - "span_id": self._propagation_context.span_id, |
575 | | - "parent_span_id": self._propagation_context.parent_span_id, |
576 | | - "dynamic_sampling_context": self.get_dynamic_sampling_context(), |
577 | | - } # type: Dict[str, Any] |
| 595 | + # if we are tracing externally (otel), those values take precedence |
| 596 | + external_propagation_context = get_external_propagation_context() |
| 597 | + if external_propagation_context: |
| 598 | + trace_id, span_id = external_propagation_context |
| 599 | + return {"trace_id": trace_id, "span_id": span_id} |
578 | 600 |
|
579 | | - return trace_context |
| 601 | + propagation_context = self.get_active_propagation_context() |
| 602 | + if propagation_context is None: |
| 603 | + return {} |
| 604 | + |
| 605 | + return { |
| 606 | + "trace_id": propagation_context.trace_id, |
| 607 | + "span_id": propagation_context.span_id, |
| 608 | + "parent_span_id": propagation_context.parent_span_id, |
| 609 | + "dynamic_sampling_context": self.get_dynamic_sampling_context(), |
| 610 | + } |
580 | 611 |
|
581 | 612 | def trace_propagation_meta(self, *args, **kwargs):
|
582 | 613 | # type: (*Any, **Any) -> str
|
@@ -1438,10 +1469,7 @@ def _apply_contexts_to_event(self, event, hint, options):
|
1438 | 1469 |
|
1439 | 1470 | # Add "trace" context
|
1440 | 1471 | if contexts.get("trace") is None:
|
1441 | | - if has_tracing_enabled(options) and self._span is not None: |
1442 | | - contexts["trace"] = self._span.get_trace_context() |
1443 | | - else: |
1444 | | - contexts["trace"] = self.get_trace_context() |
| 1472 | + contexts["trace"] = self.get_trace_context() |
1445 | 1473 |
|
1446 | 1474 | def _apply_flags_to_event(self, event, hint, options):
|
1447 | 1475 | # type: (Event, Hint, Optional[Dict[str, Any]]) -> None
|
|
0 commit comments