-
Notifications
You must be signed in to change notification settings - Fork 568
Add external_propagation_context support #5051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,6 +107,10 @@ | |
|
|
||
| global_event_processors = [] # type: List[EventProcessor] | ||
|
|
||
| # A function returning a (trace_id, span_id) tuple | ||
| # from an external tracing source (such as otel) | ||
| _external_propagation_context_fn = None # type: Optional[Callable[[], Optional[Tuple[str, str]]]] | ||
|
|
||
|
|
||
| class ScopeType(Enum): | ||
| CURRENT = "current" | ||
|
|
@@ -142,6 +146,25 @@ def add_global_event_processor(processor): | |
| global_event_processors.append(processor) | ||
|
|
||
|
|
||
| def register_external_propagation_context(fn): | ||
| # type: (Callable[[], Optional[Tuple[str, str]]]) -> None | ||
| global _external_propagation_context_fn | ||
| _external_propagation_context_fn = fn | ||
|
|
||
|
|
||
| def remove_external_propagation_context(): | ||
| # type: () -> None | ||
| global _external_propagation_context_fn | ||
| _external_propagation_context_fn = None | ||
|
|
||
|
|
||
| def get_external_propagation_context(): | ||
| # type: () -> Optional[Tuple[str, str]] | ||
| return ( | ||
| _external_propagation_context_fn() if _external_propagation_context_fn else None | ||
| ) | ||
|
|
||
|
|
||
| def _attr_setter(fn): | ||
| # type: (Any) -> Any | ||
| return property(fset=fn, doc=fn.__doc__) | ||
|
|
@@ -562,21 +585,29 @@ def get_baggage(self, *args, **kwargs): | |
| return self.get_isolation_scope().get_baggage() | ||
|
|
||
| def get_trace_context(self): | ||
| # type: () -> Any | ||
| # type: () -> Dict[str, Any] | ||
| """ | ||
| Returns the Sentry "trace" context from the Propagation Context. | ||
| """ | ||
| if self._propagation_context is None: | ||
| return None | ||
| if has_tracing_enabled(self.get_client().options) and self._span is not None: | ||
| return self._span.get_trace_context() | ||
|
|
||
| trace_context = { | ||
| "trace_id": self._propagation_context.trace_id, | ||
| "span_id": self._propagation_context.span_id, | ||
| "parent_span_id": self._propagation_context.parent_span_id, | ||
| "dynamic_sampling_context": self.get_dynamic_sampling_context(), | ||
| } # type: Dict[str, Any] | ||
| # if we are tracing externally (otel), those values take precedence | ||
| external_propagation_context = get_external_propagation_context() | ||
| if external_propagation_context: | ||
| trace_id, span_id = external_propagation_context | ||
| return {"trace_id": trace_id, "span_id": span_id} | ||
sl0thentr0py marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return trace_context | ||
| propagation_context = self.get_active_propagation_context() | ||
| if propagation_context is None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. propagation_context is actually never None, but this implementation is whack so we need this redundant check... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for some future cleanup, if you see |
||
| return {} | ||
|
|
||
| return { | ||
| "trace_id": propagation_context.trace_id, | ||
| "span_id": propagation_context.span_id, | ||
| "parent_span_id": propagation_context.parent_span_id, | ||
| "dynamic_sampling_context": self.get_dynamic_sampling_context(), | ||
| } | ||
|
|
||
| def trace_propagation_meta(self, *args, **kwargs): | ||
| # type: (*Any, **Any) -> str | ||
|
|
@@ -1438,10 +1469,7 @@ def _apply_contexts_to_event(self, event, hint, options): | |
|
|
||
| # Add "trace" context | ||
| if contexts.get("trace") is None: | ||
| if has_tracing_enabled(options) and self._span is not None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved these into |
||
| contexts["trace"] = self._span.get_trace_context() | ||
| else: | ||
| contexts["trace"] = self.get_trace_context() | ||
| contexts["trace"] = self.get_trace_context() | ||
|
|
||
| def _apply_flags_to_event(self, event, hint, options): | ||
| # type: (Event, Hint, Optional[Dict[str, Any]]) -> None | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.