Skip to content

Commit e32f224

Browse files
authored
fix(profiling): Check transaction sampled status before profiling (getsentry#1624)
Should always check if the transaction is sampled before deciding to profile to avoid profiling when it's not necessary.
1 parent 7dc58d2 commit e32f224

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

sentry_sdk/profiler.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,13 @@ def stop_profiling(self):
327327
return should_stop_timer
328328

329329

330-
def _should_profile(hub):
331-
# type: (Optional[sentry_sdk.Hub]) -> bool
330+
def _should_profile(transaction, hub):
331+
# type: (sentry_sdk.tracing.Transaction, Optional[sentry_sdk.Hub]) -> bool
332+
333+
# The corresponding transaction was not sampled,
334+
# so don't generate a profile for it.
335+
if not transaction.sampled:
336+
return False
332337

333338
# The profiler hasn't been properly initialized.
334339
if _sample_buffer is None or _scheduler is None:
@@ -357,7 +362,7 @@ def start_profiling(transaction, hub=None):
357362
# type: (sentry_sdk.tracing.Transaction, Optional[sentry_sdk.Hub]) -> Generator[None, None, None]
358363

359364
# if profiling was not enabled, this should be a noop
360-
if _should_profile(hub):
365+
if _should_profile(transaction, hub):
361366
with Profile(transaction, hub=hub):
362367
yield
363368
else:

0 commit comments

Comments
 (0)