1
1
import uuid
2
2
import random
3
+ import warnings
3
4
from datetime import datetime , timedelta , timezone
4
5
5
6
import sentry_sdk
@@ -286,13 +287,23 @@ def __init__(
286
287
self .op = op
287
288
self .description = description
288
289
self .status = status
289
- self .hub = hub
290
+ self .hub = hub # backwards compatibility
290
291
self .scope = scope
291
292
self .origin = origin
292
293
self ._measurements = {} # type: Dict[str, MeasurementValue]
293
294
self ._tags = {} # type: MutableMapping[str, str]
294
295
self ._data = {} # type: Dict[str, Any]
295
296
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
+
296
307
if start_timestamp is None :
297
308
start_timestamp = datetime .now (timezone .utc )
298
309
elif isinstance (start_timestamp , float ):
@@ -823,15 +834,57 @@ def containing_transaction(self):
823
834
# reference.
824
835
return self
825
836
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]
828
878
"""Finishes the transaction and sends it to Sentry.
829
879
All finished spans in the transaction will also be sent to Sentry.
830
880
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.
833
883
:param end_timestamp: Optional timestamp that should
834
884
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.
835
888
836
889
:return: The event ID if the transaction was sent to Sentry,
837
890
otherwise None.
@@ -840,7 +893,13 @@ def finish(self, hub=None, end_timestamp=None):
840
893
# This transaction is already finished, ignore.
841
894
return None
842
895
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 ()
844
903
client = sentry_sdk .Scope .get_client ()
845
904
846
905
if not client .is_active ():
@@ -877,7 +936,7 @@ def finish(self, hub=None, end_timestamp=None):
877
936
)
878
937
self .name = "<unlabeled transaction>"
879
938
880
- super ().finish (hub , end_timestamp )
939
+ super ().finish (scope , end_timestamp )
881
940
882
941
if not self .sampled :
883
942
# At this point a `sampled = None` should have already been resolved
@@ -930,7 +989,7 @@ def finish(self, hub=None, end_timestamp=None):
930
989
if metrics_summary :
931
990
event ["_metrics_summary" ] = metrics_summary
932
991
933
- return hub .capture_event (event )
992
+ return scope .capture_event (event )
934
993
935
994
def set_measurement (self , name , value , unit = "" ):
936
995
# type: (str, float, MeasurementUnit) -> None
0 commit comments