Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 662
Add instrumenter config to switch between Otel and Sentry instrumentation.#1766
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
Merged
antonpirker
merged 13 commits into
master
from
antonpirker/1687-basic-otel-support-2-instrumenterDec 1, 2022
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8f2f3bb
updated vscode config
antonpirker a7963ae
Moved code around for better readability
antonpirker 76ae08d
cleanup
antonpirker ca99978
Introduced SENTRY_TRACE_HEADER_NAME variable
antonpirker 376d087
Introduced +BAGGAGE_HEADER_NAME variable
antonpirker 9d3047e
Add instrumenter config to switch between Sentry and OTel instrumenta…
antonpirker 7a8654e
Add API to set arbitrary context in Transaction. (#1769)
antonpirker 4cb573b
Add API to set custom Span timestamps (#1770)
antonpirker 758680f
Moved new parameter to last position
antonpirker 0ad7518
Made instrumenter parameter explicit.
antonpirker 3ab9425
Merge branch 'master' into antonpirker/1687-basic-otel-support-2-inst…
antonpirker 3ed3077
Fixed typo
antonpirker 7dc0338
Merge branch 'antonpirker/1687-basic-otel-support-2-instrumenter' of …
antonpirker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,6 +6,7 @@ | ||
| from datetime import datetime, timedelta | ||
| import sentry_sdk | ||
| from sentry_sdk.consts import INSTRUMENTER | ||
| from sentry_sdk.utils import logger | ||
| from sentry_sdk._types import MYPY | ||
| @@ -125,6 +126,7 @@ def __init__( | ||
| status=None, # type: Optional[str] | ||
| transaction=None, # type: Optional[str] # deprecated | ||
| containing_transaction=None, # type: Optional[Transaction] | ||
| start_timestamp=None, # type: Optional[datetime] | ||
| ): | ||
| # type: (...) -> None | ||
| self.trace_id = trace_id or uuid.uuid4().hex | ||
| @@ -139,7 +141,7 @@ def __init__( | ||
| self._tags = {} # type: Dict[str, str] | ||
| self._data = {} # type: Dict[str, Any] | ||
| self._containing_transaction = containing_transaction | ||
| self.start_timestamp = datetime.utcnow() | ||
| self.start_timestamp = start_timestamp or datetime.utcnow() | ||
| try: | ||
| # TODO: For Python 3.7+, we could use a clock with ns resolution: | ||
| # self._start_timestamp_monotonic = time.perf_counter_ns() | ||
| @@ -206,15 +208,22 @@ def containing_transaction(self): | ||
| # referencing themselves) | ||
| return self._containing_transaction | ||
| def start_child(self, **kwargs): | ||
| # type: (**Any) -> Span | ||
| def start_child(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs): | ||
| # type: (str, **Any) -> Span | ||
| """ | ||
| Start a sub-span from the current span or transaction. | ||
| Takes the same arguments as the initializer of :py:class:`Span`. The | ||
| trace id, sampling decision, transaction pointer, and span recorder are | ||
| inherited from the current span/transaction. | ||
| """ | ||
| hub = self.hub or sentry_sdk.Hub.current | ||
| client = hub.client | ||
| configuration_instrumenter = client and client.options["instrumenter"] | ||
| if instrumenter != configuration_instrumenter: | ||
antonpirker marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return NoOpSpan() | ||
| kwargs.setdefault("sampled", self.sampled) | ||
| child = Span( | ||
| @@ -461,8 +470,8 @@ def is_success(self): | ||
| # type: () -> bool | ||
| return self.status == "ok" | ||
| def finish(self, hub=None): | ||
| # type: (Optional[sentry_sdk.Hub]) -> Optional[str] | ||
| def finish(self, hub=None, end_timestamp=None): | ||
| # type: (Optional[sentry_sdk.Hub], Optional[datetime]) -> Optional[str] | ||
| # XXX: would be type: (Optional[sentry_sdk.Hub]) -> None, but that leads | ||
| # to incompatible return types for Span.finish and Transaction.finish. | ||
| if self.timestamp is not None: | ||
| @@ -472,8 +481,13 @@ def finish(self, hub=None): | ||
| hub = hub or self.hub or sentry_sdk.Hub.current | ||
| try: | ||
| duration_seconds = time.perf_counter() - self._start_timestamp_monotonic | ||
| self.timestamp = self.start_timestamp + timedelta(seconds=duration_seconds) | ||
| if end_timestamp: | ||
| self.timestamp = end_timestamp | ||
| else: | ||
| duration_seconds = time.perf_counter() - self._start_timestamp_monotonic | ||
| self.timestamp = self.start_timestamp + timedelta( | ||
| seconds=duration_seconds | ||
| ) | ||
| except AttributeError: | ||
| self.timestamp = datetime.utcnow() | ||
| @@ -550,6 +564,7 @@ class Transaction(Span): | ||
| # tracestate data from other vendors, of the form `dogs=yes,cats=maybe` | ||
| "_third_party_tracestate", | ||
| "_measurements", | ||
| "_contexts", | ||
| "_profile", | ||
| "_baggage", | ||
| "_active_thread_id", | ||
| @@ -575,7 +590,9 @@ def __init__( | ||
| "instead of Span(transaction=...)." | ||
| ) | ||
| name = kwargs.pop("transaction") | ||
| Span.__init__(self, **kwargs) | ||
| self.name = name | ||
| self.source = source | ||
| self.sample_rate = None # type: Optional[float] | ||
| @@ -586,6 +603,7 @@ def __init__( | ||
| self._sentry_tracestate = sentry_tracestate | ||
| self._third_party_tracestate = third_party_tracestate | ||
| self._measurements = {} # type: Dict[str, Any] | ||
| self._contexts = {} # type: Dict[str, Any] | ||
| self._profile = None # type: Optional[sentry_sdk.profiler.Profile] | ||
| self._baggage = baggage | ||
| # for profiling, we want to know on which thread a transaction is started | ||
| @@ -619,8 +637,8 @@ def containing_transaction(self): | ||
| # reference. | ||
| return self | ||
| def finish(self, hub=None): | ||
| # type: (Optional[sentry_sdk.Hub]) -> Optional[str] | ||
| def finish(self, hub=None, end_timestamp=None): | ||
| # type: (Optional[sentry_sdk.Hub], Optional[datetime]) -> Optional[str] | ||
| if self.timestamp is not None: | ||
| # This transaction is already finished, ignore. | ||
| return None | ||
| @@ -652,7 +670,7 @@ def finish(self, hub=None): | ||
| ) | ||
| self.name = "<unlabeled transaction>" | ||
| Span.finish(self, hub) | ||
| Span.finish(self, hub, end_timestamp) | ||
| if not self.sampled: | ||
| # At this point a `sampled = None` should have already been resolved | ||
| @@ -674,11 +692,15 @@ def finish(self, hub=None): | ||
| # to be garbage collected | ||
| self._span_recorder = None | ||
| contexts = {} | ||
| contexts.update(self._contexts) | ||
| contexts.update({"trace": self.get_trace_context()}) | ||
| event = { | ||
| "type": "transaction", | ||
| "transaction": self.name, | ||
| "transaction_info": {"source": self.source}, | ||
| "contexts": {"trace": self.get_trace_context()}, | ||
| "contexts": contexts, | ||
| "tags": self._tags, | ||
| "timestamp": self.timestamp, | ||
| "start_timestamp": self.start_timestamp, | ||
| @@ -703,6 +725,10 @@ def set_measurement(self, name, value, unit=""): | ||
| self._measurements[name] = {"value": value, "unit": unit} | ||
| def set_context(self, key, value): | ||
| # type: (str, Any) -> None | ||
| self._contexts[key] = value | ||
| def to_json(self): | ||
| # type: () -> Dict[str, Any] | ||
| rv = super(Transaction, self).to_json() | ||
| @@ -828,6 +854,48 @@ def _set_initial_sampling_decision(self, sampling_context): | ||
| ) | ||
| class NoOpSpan(Span): | ||
| def __repr__(self): | ||
| # type: () -> Any | ||
| return self.__class__.__name__ | ||
| def __enter__(self): | ||
| # type: () -> Any | ||
| return self | ||
| def __exit__(self, ty, value, tb): | ||
| # type: (Any, Any, Any) -> Any | ||
| pass | ||
| def start_child(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs): | ||
| # type: (str, **Any) -> Any | ||
| pass | ||
| def new_span(self, **kwargs): | ||
| # type: (**Any) -> Any | ||
| pass | ||
| def set_tag(self, key, value): | ||
| # type: (Any, Any) -> Any | ||
| pass | ||
| def set_data(self, key, value): | ||
| # type: (Any, Any) -> Any | ||
| pass | ||
| def set_status(self, value): | ||
| # type: (Any) -> Any | ||
| pass | ||
| def set_http_status(self, http_status): | ||
| # type: (Any) -> Any | ||
| pass | ||
| def finish(self, hub=None, end_timestamp=None): | ||
| # type: (Any, Any) -> Any | ||
| pass | ||
| # Circular imports | ||
| from sentry_sdk.tracing_utils import ( | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.