Skip to content

Commit d8a69fd

Browse files
authored
feat(profiling): Extract more frame info (#1702)
This extracts a little more information around the frame that we'll use to improve the visualization/groupings including - in_app - module
1 parent f3f2eb0 commit d8a69fd

File tree

3 files changed

+214
-44
lines changed

3 files changed

+214
-44
lines changed

sentry_sdk/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def capture_event(
429429

430430
if is_transaction:
431431
if profile is not None:
432-
envelope.add_profile(profile.to_json(event_opt))
432+
envelope.add_profile(profile.to_json(event_opt, self.options))
433433
envelope.add_transaction(event_opt)
434434
else:
435435
envelope.add_event(event_opt)

sentry_sdk/profiler.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"""
1414

1515
import atexit
16+
import os
1617
import platform
1718
import random
1819
import signal
@@ -27,9 +28,15 @@
2728
from sentry_sdk._compat import PY33
2829
from sentry_sdk._queue import Queue
2930
from sentry_sdk._types import MYPY
30-
from sentry_sdk.utils import nanosecond_time
31+
from sentry_sdk.utils import (
32+
filename_for_module,
33+
handle_in_app_impl,
34+
nanosecond_time,
35+
)
3136

32-
RawFrameData = namedtuple("RawFrameData", ["function", "abs_path", "lineno"])
37+
RawFrameData = namedtuple(
38+
"RawFrameData", ["abs_path", "filename", "function", "lineno", "module"]
39+
)
3340

3441
if MYPY:
3542
from types import FrameType
@@ -61,9 +68,11 @@
6168
ProcessedFrame = TypedDict(
6269
"ProcessedFrame",
6370
{
71+
"abs_path": str,
72+
"filename": Optional[str],
6473
"function": str,
65-
"filename": str,
6674
"lineno": int,
75+
"module": Optional[str],
6776
},
6877
)
6978

@@ -162,13 +171,24 @@ def extract_stack(frame, max_stack_depth=MAX_STACK_DEPTH):
162171
stack.append(frame)
163172
frame = frame.f_back
164173

165-
return tuple(
166-
RawFrameData(
167-
function=get_frame_name(frame),
168-
abs_path=frame.f_code.co_filename,
169-
lineno=frame.f_lineno,
170-
)
171-
for frame in stack
174+
return tuple(extract_frame(frame) for frame in stack)
175+
176+
177+
def extract_frame(frame):
178+
# type: (FrameType) -> RawFrameData
179+
abs_path = frame.f_code.co_filename
180+
181+
try:
182+
module = frame.f_globals["__name__"]
183+
except Exception:
184+
module = None
185+
186+
return RawFrameData(
187+
abs_path=os.path.abspath(abs_path),
188+
filename=filename_for_module(module, abs_path) or None,
189+
function=get_frame_name(frame),
190+
lineno=frame.f_lineno,
191+
module=module,
172192
)
173193

174194

@@ -243,18 +263,24 @@ def __exit__(self, ty, value, tb):
243263
self.scheduler.stop_profiling()
244264
self._stop_ns = nanosecond_time()
245265

246-
def to_json(self, event_opt):
247-
# type: (Any) -> Dict[str, Any]
266+
def to_json(self, event_opt, options):
267+
# type: (Any, Dict[str, Any]) -> Dict[str, Any]
248268
assert self._start_ns is not None
249269
assert self._stop_ns is not None
250270

271+
profile = self.scheduler.sample_buffer.slice_profile(
272+
self._start_ns, self._stop_ns
273+
)
274+
275+
handle_in_app_impl(
276+
profile["frames"], options["in_app_exclude"], options["in_app_include"]
277+
)
278+
251279
return {
252280
"environment": event_opt.get("environment"),
253281
"event_id": uuid.uuid4().hex,
254282
"platform": "python",
255-
"profile": self.scheduler.sample_buffer.slice_profile(
256-
self._start_ns, self._stop_ns
257-
),
283+
"profile": profile,
258284
"release": event_opt.get("release", ""),
259285
"timestamp": event_opt["timestamp"],
260286
"version": "1",
@@ -358,9 +384,11 @@ def slice_profile(self, start_ns, stop_ns):
358384
frames[frame] = len(frames)
359385
frames_list.append(
360386
{
361-
"function": frame.function,
362-
"filename": frame.abs_path,
387+
"abs_path": frame.abs_path,
388+
"function": frame.function or "<unknown>",
389+
"filename": frame.filename,
363390
"lineno": frame.lineno,
391+
"module": frame.module,
364392
}
365393
)
366394

0 commit comments

Comments
 (0)