Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 660
ref(am): Introduce start_transaction#715
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
2e37add2756487153b5a9bdfc4177ce76f0a488d1edc8ffc380275ef63a4681e8349ea5036f28429c842af2b1e3986c9f1a32e2d0a845863File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,7 +8,7 @@ | ||
| from sentry_sdk._compat import with_metaclass | ||
| from sentry_sdk.scope import Scope | ||
| from sentry_sdk.client import Client | ||
| from sentry_sdk.tracing import Span | ||
| from sentry_sdk.tracing import Span, Transaction | ||
| from sentry_sdk.sessions import Session | ||
| from sentry_sdk.utils import ( | ||
| exc_info_from_error, | ||
| @@ -445,10 +445,9 @@ def start_span( | ||
| span, if any. The return value is the span object that can | ||
| be used as a context manager to start and stop timing. | ||
| Note that you will not see any span that is not contained | ||
| within a transaction. Create a transaction with | ||
| ``start_span(transaction="my transaction")`` if an | ||
| integration doesn't already do this for you. | ||
| Note that you will not see any span that is not contained within a | ||
| transaction. Most integrations already do this for you, but create a | ||
| transaction with `start_transaction` otherwise. | ||
| """ | ||
| client, scope = self._stack[-1] | ||
| @@ -462,17 +461,63 @@ def start_span( | ||
| else: | ||
| span = Span(**kwargs) | ||
| if span.sampled is None and span.transaction is not None: | ||
| elif isinstance(span, Transaction): | ||
| raise ValueError("Pass transactions to start_transaction instead") | ||
| return span | ||
| def start_transaction( | ||
| self, | ||
| transaction=None, # type: Optional[Transaction] | ||
| **kwargs # type: Any | ||
| ): | ||
| # type: (...) -> Transaction | ||
| """ | ||
| Start and return a transaction. | ||
| Start an existing transaction if given, otherwise create and start a new | ||
| transaction with kwargs. | ||
| """ | ||
| # XXX: should we always set transaction.hub = self? | ||
| # In a multi-hub program, what does it mean to write | ||
| # hub1.start_transaction(Transaction(hub=hub2)) | ||
| # ? Should the transaction be on hub1 or hub2? | ||
| # XXX: it is strange that both start_span and start_transaction take | ||
| # kwargs, but those are ignored if span/transaction are not None. | ||
| # Code such as: | ||
| # hub.start_transaction(Transaction(name="foo"), name="bar") | ||
| # | ||
| # is clearly weird, but not so weird if we intentionally want to rename | ||
| # a transaction we got from somewhere else: | ||
| # hub.start_transaction(transaction, name="new_name") | ||
| # | ||
| # Would be clearer if Hub was not involved: | ||
| # transaction.name = "new_name" | ||
| # with transaction: # __enter__ => start, __exit__ => finish | ||
| # with transaction.start_child(...): | ||
| # pass | ||
| # # alternatively, rely on transaction being in the current scope | ||
| # with Span(...): | ||
| # pass | ||
| if transaction is None: | ||
| kwargs.setdefault("hub", self) | ||
| transaction = Transaction(**kwargs) | ||
| client, scope = self._stack[-1] | ||
| if transaction.sampled is None: | ||
| sample_rate = client and client.options["traces_sample_rate"] or 0 | ||
| span.sampled = random.random() < sample_rate | ||
| transaction.sampled = random.random() < sample_rate | ||
rhcarvalho marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if span.sampled: | ||
| if transaction.sampled: | ||
| max_spans = ( | ||
| client and client.options["_experiments"].get("max_spans") or 1000 | ||
| ) | ||
| span.init_finished_spans(maxlen=max_spans) | ||
| transaction.init_span_recorder(maxlen=max_spans) | ||
| return span | ||
| return transaction | ||
| @overload # noqa | ||
| def push_scope( | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,7 +4,7 @@ | ||
| from sentry_sdk.hub import Hub | ||
| from sentry_sdk.utils import capture_internal_exceptions, event_from_exception | ||
| from sentry_sdk.tracing import Span | ||
| from sentry_sdk.tracing import Transaction | ||
| from sentry_sdk._compat import reraise | ||
| from sentry_sdk.integrations import Integration, DidNotEnable | ||
| from sentry_sdk.integrations.logging import ignore_logger | ||
| @@ -130,19 +130,21 @@ def _inner(*args, **kwargs): | ||
| scope.clear_breadcrumbs() | ||
| scope.add_event_processor(_make_event_processor(task, *args, **kwargs)) | ||
| span = Span.continue_from_headers(args[3].get("headers") or {}) | ||
| span.op = "celery.task" | ||
| span.transaction = "unknown celery task" | ||
| transaction = Transaction.continue_from_headers( | ||
| args[3].get("headers") or {}, | ||
Contributor 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. I see this was already there. What is | ||
| op="celery.task", | ||
| name="unknown celery task", | ||
| ) | ||
| # Could possibly use a better hook than this one | ||
| span.set_status("ok") | ||
| transaction.set_status("ok") | ||
| with capture_internal_exceptions(): | ||
| # Celery task objects are not a thing to be trusted. Even | ||
| # something such as attribute access can fail. | ||
| span.transaction = task.name | ||
| transaction.name = task.name | ||
| with hub.start_span(span): | ||
| with hub.start_transaction(transaction): | ||
| return f(*args, **kwargs) | ||
| return _inner # type: ignore | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5,6 +5,7 @@ | ||
| from sentry_sdk._functools import wraps | ||
| from sentry_sdk._types import MYPY | ||
| from sentry_sdk.utils import logger, capture_internal_exceptions | ||
| from sentry_sdk.tracing import Transaction | ||
| if MYPY: | ||
| from typing import Any | ||
| @@ -140,8 +141,8 @@ def transaction(self, value): | ||
| """When set this forces a specific transaction name to be set.""" | ||
| self._transaction = value | ||
| span = self._span | ||
| if span: | ||
| span.transaction = value | ||
| if span and isinstance(span, Transaction) and value: | ||
| span.name = value | ||
Comment on lines
+144
to
+145
Contributor 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. Would it make sense to store the whole transaction in Code reading it that expects a string (e.g. to set Contributor 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. The current state at JS is that we have a single slot for a "span" that can be a transaction or a span. Let's leave this for later. | ||
| @_attr_setter | ||
| def user(self, value): | ||
| @@ -166,8 +167,8 @@ def span(self): | ||
| def span(self, span): | ||
| # type: (Optional[Span]) -> None | ||
| self._span = span | ||
| if span is not None: | ||
| span_transaction = span.transaction | ||
| if isinstance(span, Transaction): | ||
| span_transaction = span.name | ||
| if span_transaction: | ||
| self._transaction = span_transaction | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In JS there is a fallback that calls
startTransactionin this case, to ease transition of existing code.https://github.com/getsentry/sentry-javascript/blob/d66e2d7f76492b2e403f64b047ffaf7bdddabb0a/packages/apm/src/hubextensions.ts#L52-L71