42
42
from sentry_sdk .utils import (
43
43
capture_internal_exception ,
44
44
filename_for_module ,
45
+ get_current_thread_meta ,
46
+ is_gevent ,
45
47
is_valid_sample_rate ,
46
48
logger ,
47
49
nanosecond_time ,
126
128
127
129
128
130
try :
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
131
132
from gevent .threadpool import ThreadPool # type: ignore
132
133
133
134
thread_sleep = get_original ("time" , "sleep" )
134
135
except ImportError :
135
-
136
- def get_gevent_hub ():
137
- # type: () -> Any
138
- return None
139
-
140
136
thread_sleep = time .sleep
141
137
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
-
147
138
ThreadPool = None
148
139
149
140
150
- def is_gevent ():
151
- # type: () -> bool
152
- return is_module_patched ("threading" ) or is_module_patched ("_thread" )
153
-
154
-
155
141
_scheduler = None # type: Optional[Scheduler]
156
142
157
143
# The default sampling frequency to use. This is set at 101 in order to
@@ -389,52 +375,6 @@ def get_frame_name(frame):
389
375
MAX_PROFILE_DURATION_NS = int (3e10 ) # 30 seconds
390
376
391
377
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
-
438
378
class Profile (object ):
439
379
def __init__ (
440
380
self ,
@@ -456,7 +396,7 @@ def __init__(
456
396
457
397
# Various framework integrations are capable of overwriting the active thread id.
458
398
# 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
460
400
self .active_thread_id = None # type: Optional[int]
461
401
462
402
try :
@@ -479,7 +419,7 @@ def __init__(
479
419
480
420
def update_active_thread_id (self ):
481
421
# type: () -> None
482
- self .active_thread_id = get_current_thread_id ()
422
+ self .active_thread_id = get_current_thread_meta ()[ 0 ]
483
423
logger .debug (
484
424
"[Profiling] updating active thread id to {tid}" .format (
485
425
tid = self .active_thread_id
0 commit comments