Skip to content

Commit 72455f4

Browse files
authored
ref(profiling): Add debug logs to profiling (#1883)
1 parent b009ef8 commit 72455f4

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

Diff for: sentry_sdk/profiler.py

+38-7
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ def setup_profiler(options):
150150
global _scheduler
151151

152152
if _scheduler is not None:
153-
logger.debug("profiling is already setup")
153+
logger.debug("[Profiling] Profiler is already setup")
154154
return False
155155

156156
if not PY33:
157-
logger.warn("profiling is only supported on Python >= 3.3")
157+
logger.warn("[Profiling] Profiler requires Python >= 3.3")
158158
return False
159159

160160
frequency = DEFAULT_SAMPLING_FREQUENCY
@@ -184,6 +184,9 @@ def setup_profiler(options):
184184
else:
185185
raise ValueError("Unknown profiler mode: {}".format(profiler_mode))
186186

187+
logger.debug(
188+
"[Profiling] Setting up profiler in {mode} mode".format(mode=_scheduler.mode)
189+
)
187190
_scheduler.setup()
188191

189192
atexit.register(teardown_profiler)
@@ -440,6 +443,11 @@ def __init__(
440443
def update_active_thread_id(self):
441444
# type: () -> None
442445
self.active_thread_id = get_current_thread_id()
446+
logger.debug(
447+
"[Profiling] updating active thread id to {tid}".format(
448+
tid=self.active_thread_id
449+
)
450+
)
443451

444452
def _set_initial_sampling_decision(self, sampling_context):
445453
# type: (SamplingContext) -> None
@@ -456,11 +464,17 @@ def _set_initial_sampling_decision(self, sampling_context):
456464
# The corresponding transaction was not sampled,
457465
# so don't generate a profile for it.
458466
if not self.sampled:
467+
logger.debug(
468+
"[Profiling] Discarding profile because transaction is discarded."
469+
)
459470
self.sampled = False
460471
return
461472

462473
# The profiler hasn't been properly initialized.
463474
if self.scheduler is None:
475+
logger.debug(
476+
"[Profiling] Discarding profile because profiler was not started."
477+
)
464478
self.sampled = False
465479
return
466480

@@ -478,6 +492,9 @@ def _set_initial_sampling_decision(self, sampling_context):
478492
# The profiles_sample_rate option was not set, so profiling
479493
# was never enabled.
480494
if sample_rate is None:
495+
logger.debug(
496+
"[Profiling] Discarding profile because profiling was not enabled."
497+
)
481498
self.sampled = False
482499
return
483500

@@ -486,6 +503,15 @@ def _set_initial_sampling_decision(self, sampling_context):
486503
# to a float (True becomes 1.0 and False becomes 0.0)
487504
self.sampled = random.random() < float(sample_rate)
488505

506+
if self.sampled:
507+
logger.debug("[Profiling] Initializing profile")
508+
else:
509+
logger.debug(
510+
"[Profiling] Discarding profile because it's not included in the random sample (sample rate = {sample_rate})".format(
511+
sample_rate=float(sample_rate)
512+
)
513+
)
514+
489515
def get_profile_context(self):
490516
# type: () -> ProfileContext
491517
return {"profile_id": self.event_id}
@@ -496,6 +522,7 @@ def start(self):
496522
return
497523

498524
assert self.scheduler, "No scheduler specified"
525+
logger.debug("[Profiling] Starting profile")
499526
self.active = True
500527
self.start_ns = nanosecond_time()
501528
self.scheduler.start_profiling(self)
@@ -506,6 +533,7 @@ def stop(self):
506533
return
507534

508535
assert self.scheduler, "No scheduler specified"
536+
logger.debug("[Profiling] Stopping profile")
509537
self.active = False
510538
self.scheduler.stop_profiling(self)
511539
self.stop_ns = nanosecond_time()
@@ -651,11 +679,14 @@ def to_json(self, event_opt, options):
651679

652680
def valid(self):
653681
# type: () -> bool
654-
return (
655-
self.sampled is not None
656-
and self.sampled
657-
and self.unique_samples >= PROFILE_MINIMUM_SAMPLES
658-
)
682+
if self.sampled is None or not self.sampled:
683+
return False
684+
685+
if self.unique_samples < PROFILE_MINIMUM_SAMPLES:
686+
logger.debug("[Profiling] Discarding profile because insufficient samples.")
687+
return False
688+
689+
return True
659690

660691

661692
class Scheduler(object):

0 commit comments

Comments
 (0)