4646 from typing import Sequence
4747 from typing import Tuple
4848 from typing_extensions import TypedDict
49- import sentry_sdk .scope
5049 import sentry_sdk .tracing
5150
5251 ThreadId = str
@@ -329,10 +328,13 @@ def __init__(
329328 self ,
330329 scheduler , # type: Scheduler
331330 transaction , # type: sentry_sdk.tracing.Transaction
331+ hub = None , # type: Optional[sentry_sdk.Hub]
332332 ):
333333 # type: (...) -> None
334334 self .scheduler = scheduler
335335 self .transaction = transaction
336+ self .hub = hub
337+ self .active_thread_id = None # type: Optional[int]
336338 self .start_ns = 0 # type: int
337339 self .stop_ns = 0 # type: int
338340 self .active = False # type: bool
@@ -347,6 +349,14 @@ def __init__(
347349
348350 def __enter__ (self ):
349351 # type: () -> None
352+ hub = self .hub or sentry_sdk .Hub .current
353+
354+ _ , scope = hub ._stack [- 1 ]
355+ old_profile = scope .profile
356+ scope .profile = self
357+
358+ self ._context_manager_state = (hub , scope , old_profile )
359+
350360 self .start_ns = nanosecond_time ()
351361 self .scheduler .start_profiling (self )
352362
@@ -355,6 +365,11 @@ def __exit__(self, ty, value, tb):
355365 self .scheduler .stop_profiling (self )
356366 self .stop_ns = nanosecond_time ()
357367
368+ _ , scope , old_profile = self ._context_manager_state
369+ del self ._context_manager_state
370+
371+ scope .profile = old_profile
372+
358373 def write (self , ts , sample ):
359374 # type: (int, RawSample) -> None
360375 if ts < self .start_ns :
@@ -414,18 +429,14 @@ def process(self):
414429 "thread_metadata" : thread_metadata ,
415430 }
416431
417- def to_json (self , event_opt , options , scope ):
418- # type: (Any, Dict[str, Any], Optional[sentry_sdk.scope.Scope]) -> Dict[str, Any]
419-
432+ def to_json (self , event_opt , options ):
433+ # type: (Any, Dict[str, Any]) -> Dict[str, Any]
420434 profile = self .process ()
421435
422436 handle_in_app_impl (
423437 profile ["frames" ], options ["in_app_exclude" ], options ["in_app_include" ]
424438 )
425439
426- # the active thread id from the scope always take priorty if it exists
427- active_thread_id = None if scope is None else scope .active_thread_id
428-
429440 return {
430441 "environment" : event_opt .get ("environment" ),
431442 "event_id" : uuid .uuid4 ().hex ,
@@ -459,8 +470,8 @@ def to_json(self, event_opt, options, scope):
459470 "trace_id" : self .transaction .trace_id ,
460471 "active_thread_id" : str (
461472 self .transaction ._active_thread_id
462- if active_thread_id is None
463- else active_thread_id
473+ if self . active_thread_id is None
474+ else self . active_thread_id
464475 ),
465476 }
466477 ],
@@ -739,7 +750,7 @@ def start_profiling(transaction, hub=None):
739750 # if profiling was not enabled, this should be a noop
740751 if _should_profile (transaction , hub ):
741752 assert _scheduler is not None
742- with Profile (_scheduler , transaction ):
753+ with Profile (_scheduler , transaction , hub ):
743754 yield
744755 else :
745756 yield
0 commit comments