Skip to content

Commit 3a8c66f

Browse files
Kyle-Verhoogjd
andauthored
feat(profiling): disable trace tracking in tracer by default (#1488) (#1502)
This disables the tracer usage by default in the profiler. Co-authored-by: Julien Danjou <[email protected]>
1 parent bcebd33 commit 3a8c66f

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

ddtrace/profiling/collector/stack.pyx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ cdef stack_collect(ignore_profiler, thread_time, max_nframes, interval, wall_tim
262262

263263
running_thread_ids = {t[0] for t in running_threads}
264264

265-
thread_span_links.clear_threads(running_thread_ids)
265+
if thread_span_links:
266+
thread_span_links.clear_threads(running_thread_ids)
266267

267268
if ignore_profiler:
268269
running_thread_ids -= _periodic.PERIODIC_THREAD_IDS
@@ -273,7 +274,10 @@ cdef stack_collect(ignore_profiler, thread_time, max_nframes, interval, wall_tim
273274
for tid, frame in running_threads:
274275
if ignore_profiler and tid in _periodic.PERIODIC_THREAD_IDS:
275276
continue
276-
spans = thread_span_links.get_active_leaf_spans_from_thread_id(tid)
277+
if thread_span_links:
278+
spans = thread_span_links.get_active_leaf_spans_from_thread_id(tid)
279+
else:
280+
spans = set()
277281
frames, nframes = _traceback.pyframe_to_frames(frame, max_nframes)
278282
stack_events.append(
279283
StackSampleEvent(
@@ -376,7 +380,7 @@ class StackCollector(collector.PeriodicCollector):
376380
tracer = attr.ib(default=None)
377381
_thread_time = attr.ib(init=False, repr=False)
378382
_last_wall_time = attr.ib(init=False, repr=False)
379-
_thread_span_links = attr.ib(factory=_ThreadSpanLinks, init=False, repr=False)
383+
_thread_span_links = attr.ib(default=None, init=False, repr=False)
380384

381385
@max_time_usage_pct.validator
382386
def _check_max_time_usage(self, attribute, value):
@@ -387,6 +391,7 @@ class StackCollector(collector.PeriodicCollector):
387391
self._thread_time = ThreadTime()
388392
self._last_wall_time = compat.monotonic_ns()
389393
if self.tracer is not None:
394+
self._thread_span_links = _ThreadSpanLinks()
390395
self.tracer.on_start_span(self._thread_span_links.link_span)
391396
super(StackCollector, self).start()
392397

ddtrace/profiling/profiler.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import logging
33
import os
44

5-
import ddtrace
65
from ddtrace.profiling import recorder
76
from ddtrace.profiling import scheduler
87
from ddtrace.vendor import attr
@@ -32,16 +31,6 @@ def _build_default_exporters():
3231
return exporters
3332

3433

35-
def _build_default_collectors():
36-
r = recorder.Recorder()
37-
return [
38-
stack.StackCollector(r, tracer=ddtrace.tracer),
39-
memory.MemoryCollector(r),
40-
exceptions.UncaughtExceptionCollector(r),
41-
threading.LockCollector(r),
42-
]
43-
44-
4534
# This ought to use `enum.Enum`, but since it's not available in Python 2, we just use a dumb class.
4635
@attr.s(repr=False)
4736
class ProfilerStatus(object):
@@ -69,12 +58,26 @@ class Profiler(object):
6958
7059
"""
7160

72-
collectors = attr.ib(factory=_build_default_collectors)
61+
collectors = attr.ib(default=None)
7362
exporters = attr.ib(factory=_build_default_exporters)
7463
schedulers = attr.ib(init=False, factory=list)
7564
status = attr.ib(init=False, type=ProfilerStatus, default=ProfilerStatus.STOPPED)
65+
tracer = attr.ib(default=None)
66+
67+
@staticmethod
68+
def _build_default_collectors(tracer):
69+
r = recorder.Recorder()
70+
return [
71+
stack.StackCollector(r, tracer=tracer),
72+
memory.MemoryCollector(r),
73+
exceptions.UncaughtExceptionCollector(r),
74+
threading.LockCollector(r),
75+
]
7676

7777
def __attrs_post_init__(self):
78+
if self.collectors is None:
79+
self.collectors = self._build_default_collectors(self.tracer)
80+
7881
if self.exporters:
7982
for rec in self.recorders:
8083
self.schedulers.append(scheduler.Scheduler(recorder=rec, exporters=self.exporters))

tests/profiling/test_profiler.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import pytest
2+
3+
import ddtrace
14
from ddtrace.profiling import profiler
5+
from ddtrace.profiling.collector import stack
26

37

48
def test_status():
@@ -16,3 +20,15 @@ def test_restart():
1620
p.stop()
1721
p.start()
1822
p.stop()
23+
24+
25+
def test_tracer_api(monkeypatch):
26+
monkeypatch.setenv("DD_API_KEY", "foobar")
27+
prof = profiler.Profiler(tracer=ddtrace.tracer)
28+
assert prof.tracer == ddtrace.tracer
29+
for collector in prof.collectors:
30+
if isinstance(collector, stack.StackCollector):
31+
assert collector.tracer == ddtrace.tracer
32+
break
33+
else:
34+
pytest.fail("Unable to find stack collector")

0 commit comments

Comments
 (0)