Skip to content

Commit 14970d8

Browse files
ref(tracing): Remove Hub in Transaction.finish
Rename `Transaction.finish` method's `hub` parameter to `scope` (in a backwards-compatible manner), and update the method so that it is using `Scope` API under the hood as much as possible. Prerequisite for #3265
1 parent 2cb2dca commit 14970d8

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed

sentry_sdk/tracing.py

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import uuid
22
import random
3+
import warnings
34
from datetime import datetime, timedelta, timezone
45

56
import sentry_sdk
@@ -286,13 +287,23 @@ def __init__(
286287
self.op = op
287288
self.description = description
288289
self.status = status
289-
self.hub = hub
290+
self.hub = hub # backwards compatibility
290291
self.scope = scope
291292
self.origin = origin
292293
self._measurements = {} # type: Dict[str, MeasurementValue]
293294
self._tags = {} # type: MutableMapping[str, str]
294295
self._data = {} # type: Dict[str, Any]
295296
self._containing_transaction = containing_transaction
297+
298+
if hub is not None:
299+
warnings.warn(
300+
"The `hub` parameter is deprecated. Please use `scope` instead.",
301+
DeprecationWarning,
302+
stacklevel=2,
303+
)
304+
305+
self.scope = self.scope or hub.scope
306+
296307
if start_timestamp is None:
297308
start_timestamp = datetime.now(timezone.utc)
298309
elif isinstance(start_timestamp, float):
@@ -823,15 +834,57 @@ def containing_transaction(self):
823834
# reference.
824835
return self
825836

826-
def finish(self, hub=None, end_timestamp=None):
827-
# type: (Optional[Union[sentry_sdk.Hub, sentry_sdk.Scope]], Optional[Union[float, datetime]]) -> Optional[str]
837+
def _get_scope_from_finish_args(
838+
self,
839+
scope_arg, # type: Optional[Union[sentry_sdk.Scope, sentry_sdk.Hub]]
840+
hub_arg, # type: Optional[Union[sentry_sdk.Scope, sentry_sdk.Hub]]
841+
):
842+
# type: (...) -> Optional[sentry_sdk.Scope]
843+
"""
844+
Logic to get the scope from the arguments passed to finish. This
845+
function exists for backwards compatibility with the old finish.
846+
847+
TODO: Remove this function in the next major version.
848+
"""
849+
scope_or_hub = scope_arg
850+
if hub_arg is not None:
851+
warnings.warn(
852+
"The `hub` parameter is deprecated. Please use the `scope` parameter, instead.",
853+
DeprecationWarning,
854+
stacklevel=3,
855+
)
856+
857+
scope_or_hub = hub_arg
858+
859+
if isinstance(scope_or_hub, sentry_sdk.Hub):
860+
warnings.warn(
861+
"Passing a Hub to finish is deprecated. Please pass a Scope, instead.",
862+
DeprecationWarning,
863+
stacklevel=3,
864+
)
865+
866+
return scope_or_hub.scope
867+
868+
return scope_or_hub
869+
870+
def finish(
871+
self,
872+
scope=None, # type: Optional[sentry_sdk.Scope]
873+
end_timestamp=None, # type: Optional[Union[float, datetime]]
874+
*,
875+
hub=None, # type: Optional[sentry_sdk.Hub]
876+
):
877+
# type: (...) -> Optional[str]
828878
"""Finishes the transaction and sends it to Sentry.
829879
All finished spans in the transaction will also be sent to Sentry.
830880
831-
:param hub: The hub to use for this transaction.
832-
If not provided, the current hub will be used.
881+
:param scope: The Scope to use for this transaction.
882+
If not provided, the current Scope will be used.
833883
:param end_timestamp: Optional timestamp that should
834884
be used as timestamp instead of the current time.
885+
:param hub: The hub to use for this transaction.
886+
This argument is DEPRECATED. Please use the `scope`
887+
parameter, instead.
835888
836889
:return: The event ID if the transaction was sent to Sentry,
837890
otherwise None.
@@ -840,7 +893,13 @@ def finish(self, hub=None, end_timestamp=None):
840893
# This transaction is already finished, ignore.
841894
return None
842895

843-
hub = hub or self.hub or sentry_sdk.Hub.current
896+
# For backwards compatibility, we must handle the case where `scope`
897+
# or `hub` could both either be a `Scope` or a `Hub`.
898+
scope = self._get_scope_from_finish_args(
899+
scope, hub
900+
) # type: Optional[sentry_sdk.Scope]
901+
902+
scope = scope or self.scope or sentry_sdk.Scope.get_current_scope()
844903
client = sentry_sdk.Scope.get_client()
845904

846905
if not client.is_active():
@@ -877,7 +936,7 @@ def finish(self, hub=None, end_timestamp=None):
877936
)
878937
self.name = "<unlabeled transaction>"
879938

880-
super().finish(hub, end_timestamp)
939+
super().finish(scope, end_timestamp)
881940

882941
if not self.sampled:
883942
# At this point a `sampled = None` should have already been resolved
@@ -930,7 +989,7 @@ def finish(self, hub=None, end_timestamp=None):
930989
if metrics_summary:
931990
event["_metrics_summary"] = metrics_summary
932991

933-
return hub.capture_event(event)
992+
return scope.capture_event(event)
934993

935994
def set_measurement(self, name, value, unit=""):
936995
# type: (str, float, MeasurementUnit) -> None

0 commit comments

Comments
 (0)