Skip to content

Commit 72e00b3

Browse files
Julien Danjoumergify[bot]
andauthored
profiling: store trace endpoint in profiling events (#2573)
* feat(profiling): store trace resource in profiling events * feat(pprof): export trace resource in profiles Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 513acc9 commit 72e00b3

File tree

9 files changed

+644
-216
lines changed

9 files changed

+644
-216
lines changed

ddtrace/profiling/collector/stack.pyx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,32 @@ cdef stack_collect(ignore_profiler, thread_time, max_nframes, interval, wall_tim
345345

346346
frames, nframes = _traceback.pyframe_to_frames(frame, max_nframes)
347347

348+
if span is None:
349+
trace_id = None
350+
span_id = None
351+
trace_type = None
352+
trace_resource = None
353+
else:
354+
trace_id = span.trace_id
355+
span_id = span.span_id
356+
if span._local_root is None:
357+
trace_type = None
358+
trace_resource = None
359+
else:
360+
trace_type = span._local_root.span_type
361+
trace_resource = span._local_root.resource
362+
348363
stack_events.append(
349364
StackSampleEvent(
350365
thread_id=thread_id,
351366
thread_native_id=thread_native_id,
352367
thread_name=thread_name,
353368
task_id=task_id,
354369
task_name=task_name,
355-
trace_id=None if span is None else span.trace_id,
356-
span_id=None if span is None else span.span_id,
370+
trace_id=trace_id,
371+
span_id=span_id,
372+
trace_resource=trace_resource,
373+
trace_type=trace_type,
357374
nframes=nframes, frames=frames,
358375
wall_time_ns=wall_time,
359376
cpu_time_ns=cpu_time,
@@ -371,8 +388,10 @@ cdef stack_collect(ignore_profiler, thread_time, max_nframes, interval, wall_tim
371388
thread_native_id=thread_native_id,
372389
task_id=task_id,
373390
task_name=task_name,
374-
trace_id=None if span is None else span.trace_id,
375-
span_id=None if span is None else span.span_id,
391+
trace_id=trace_id,
392+
span_id=span_id,
393+
trace_resource=trace_resource,
394+
trace_type=trace_type,
376395
nframes=nframes,
377396
frames=frames,
378397
sampling_period=int(interval * 1e9),

ddtrace/profiling/collector/threading.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,25 @@ def __init__(self, wrapped, recorder, tracer, max_nframes, capture_sampler):
7777
code = frame.f_code
7878
self._self_name = "%s:%d" % (os.path.basename(code.co_filename), frame.f_lineno)
7979

80-
def _get_trace_and_span_id(self):
81-
# type: (...) -> Tuple[Optional[int], Optional[int]]
82-
"""Return current trace and span ids."""
80+
def _get_trace_and_span_info(self):
81+
# type: (...) -> Tuple[Optional[int], Optional[int], Optional[str], Optional[str]]
82+
"""Return current trace id, span id and trace resource and type."""
8383
if self._self_tracer is None:
84-
return (None, None)
84+
return (None, None, None, None)
8585

8686
ctxt = self._self_tracer.current_trace_context()
8787
if ctxt is None:
88-
return (None, None)
88+
return (None, None, None, None)
8989

90-
return (ctxt.trace_id, ctxt.span_id)
90+
root = self._self_tracer.current_root_span()
91+
if root is None:
92+
resource = None
93+
span_type = None
94+
else:
95+
resource = root.resource
96+
span_type = root.span_type
97+
98+
return (ctxt.trace_id, ctxt.span_id, resource, span_type)
9199

92100
def acquire(self, *args, **kwargs):
93101
if not self._self_capture_sampler.capture():
@@ -101,7 +109,7 @@ def acquire(self, *args, **kwargs):
101109
end = self._self_acquired_at = compat.monotonic_ns()
102110
thread_id, thread_name = _current_thread()
103111
frames, nframes = _traceback.pyframe_to_frames(sys._getframe(1), self._self_max_nframes)
104-
trace_id, span_id = self._get_trace_and_span_id()
112+
trace_id, span_id, trace_resource, trace_type = self._get_trace_and_span_info()
105113
self._self_recorder.push_event(
106114
LockAcquireEvent(
107115
lock_name=self._self_name,
@@ -111,6 +119,8 @@ def acquire(self, *args, **kwargs):
111119
thread_name=thread_name,
112120
trace_id=trace_id,
113121
span_id=span_id,
122+
trace_resource=trace_resource,
123+
trace_type=trace_type,
114124
wait_time_ns=end - start,
115125
sampling_pct=self._self_capture_sampler.capture_pct,
116126
)
@@ -128,7 +138,7 @@ def release(self, *args, **kwargs):
128138
end = compat.monotonic_ns()
129139
frames, nframes = _traceback.pyframe_to_frames(sys._getframe(1), self._self_max_nframes)
130140
thread_id, thread_name = _current_thread()
131-
trace_id, span_id = self._get_trace_and_span_id()
141+
trace_id, span_id, trace_resource, trace_type = self._get_trace_and_span_info()
132142
self._self_recorder.push_event(
133143
LockReleaseEvent(
134144
lock_name=self._self_name,
@@ -138,6 +148,8 @@ def release(self, *args, **kwargs):
138148
thread_name=thread_name,
139149
trace_id=trace_id,
140150
span_id=span_id,
151+
trace_resource=trace_resource,
152+
trace_type=trace_type,
141153
locked_for_ns=end - self._self_acquired_at,
142154
sampling_pct=self._self_capture_sampler.capture_pct,
143155
)

ddtrace/profiling/event.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import typing
2+
13
import attr
24

35
from ddtrace.internal import compat
@@ -44,3 +46,5 @@ class StackBasedEvent(SampleEvent):
4446
nframes = attr.ib(default=None)
4547
trace_id = attr.ib(default=None)
4648
span_id = attr.ib(default=None)
49+
trace_type = attr.ib(default=None, type=typing.Optional[str])
50+
trace_resource = attr.ib(default=None, type=typing.Optional[str])

0 commit comments

Comments
 (0)