4242from sentry_sdk .utils import (
4343 capture_internal_exception ,
4444 filename_for_module ,
45+ get_current_thread_meta ,
46+ is_gevent ,
4547 is_valid_sample_rate ,
4648 logger ,
4749 nanosecond_time ,
126128
127129
128130try :
129- from gevent import get_hub as get_gevent_hub # type: ignore
130- from gevent .monkey import get_original , is_module_patched # type: ignore
131+ from gevent .monkey import get_original # type: ignore
131132 from gevent .threadpool import ThreadPool # type: ignore
132133
133134 thread_sleep = get_original ("time" , "sleep" )
134135except ImportError :
135-
136- def get_gevent_hub ():
137- # type: () -> Any
138- return None
139-
140136 thread_sleep = time .sleep
141137
142- def is_module_patched (* args , ** kwargs ):
143- # type: (*Any, **Any) -> bool
144- # unable to import from gevent means no modules have been patched
145- return False
146-
147138 ThreadPool = None
148139
149140
150- def is_gevent ():
151- # type: () -> bool
152- return is_module_patched ("threading" ) or is_module_patched ("_thread" )
153-
154-
155141_scheduler = None # type: Optional[Scheduler]
156142
157143# The default sampling frequency to use. This is set at 101 in order to
@@ -389,52 +375,6 @@ def get_frame_name(frame):
389375MAX_PROFILE_DURATION_NS = int (3e10 ) # 30 seconds
390376
391377
392- def get_current_thread_id (thread = None ):
393- # type: (Optional[threading.Thread]) -> Optional[int]
394- """
395- Try to get the id of the current thread, with various fall backs.
396- """
397-
398- # if a thread is specified, that takes priority
399- if thread is not None :
400- try :
401- thread_id = thread .ident
402- if thread_id is not None :
403- return thread_id
404- except AttributeError :
405- pass
406-
407- # if the app is using gevent, we should look at the gevent hub first
408- # as the id there differs from what the threading module reports
409- if is_gevent ():
410- gevent_hub = get_gevent_hub ()
411- if gevent_hub is not None :
412- try :
413- # this is undocumented, so wrap it in try except to be safe
414- return gevent_hub .thread_ident
415- except AttributeError :
416- pass
417-
418- # use the current thread's id if possible
419- try :
420- current_thread_id = threading .current_thread ().ident
421- if current_thread_id is not None :
422- return current_thread_id
423- except AttributeError :
424- pass
425-
426- # if we can't get the current thread id, fall back to the main thread id
427- try :
428- main_thread_id = threading .main_thread ().ident
429- if main_thread_id is not None :
430- return main_thread_id
431- except AttributeError :
432- pass
433-
434- # we've tried everything, time to give up
435- return None
436-
437-
438378class Profile (object ):
439379 def __init__ (
440380 self ,
@@ -456,7 +396,7 @@ def __init__(
456396
457397 # Various framework integrations are capable of overwriting the active thread id.
458398 # If it is set to `None` at the end of the profile, we fall back to the default.
459- self ._default_active_thread_id = get_current_thread_id () or 0 # type: int
399+ self ._default_active_thread_id = get_current_thread_meta ()[ 0 ] or 0 # type: int
460400 self .active_thread_id = None # type: Optional[int]
461401
462402 try :
@@ -479,7 +419,7 @@ def __init__(
479419
480420 def update_active_thread_id (self ):
481421 # type: () -> None
482- self .active_thread_id = get_current_thread_id ()
422+ self .active_thread_id = get_current_thread_meta ()[ 0 ]
483423 logger .debug (
484424 "[Profiling] updating active thread id to {tid}" .format (
485425 tid = self .active_thread_id
0 commit comments