Skip to content

Commit 53c4557

Browse files
authored
feat: set in_app accordingly (#16)
* feat: set in_app accordingly * fix: fix broken import * fix: various module-related bugfixes
1 parent 8f8bbfc commit 53c4557

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

sentry_sdk/integrations/django.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,24 @@ def processor(event):
6363
with _internal_exceptions():
6464
_set_user_info(request, event)
6565

66-
# TODO: user info
66+
with _internal_exceptions():
67+
_process_frames(event)
6768

6869
return processor
6970

7071

72+
def _process_frames(event):
73+
for frame in event.iter_frames():
74+
if "in_app" in frame:
75+
continue
76+
77+
module = frame.get("module")
78+
if not module:
79+
continue
80+
if module == "django" or module.startswith("django."):
81+
frame["in_app"] = False
82+
83+
7184
def _got_request_exception(request=None, **kwargs):
7285
capture_exception()
7386

sentry_sdk/integrations/flask.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
except ImportError:
1111
current_user = None
1212

13-
from flask import request
13+
from flask import current_app, request
1414
from flask.signals import (
1515
appcontext_pushed,
1616
appcontext_tearing_down,
@@ -56,6 +56,26 @@ def _event_processor(event):
5656
with _internal_exceptions():
5757
_set_user_info(event)
5858

59+
with _internal_exceptions():
60+
_process_frames(event)
61+
62+
63+
def _process_frames(event):
64+
for frame in event.iter_frames():
65+
if "in_app" in frame:
66+
continue
67+
module = frame.get("module")
68+
if not module:
69+
continue
70+
71+
if module == "flask" or module.startswith("flask."):
72+
frame["in_app"] = False
73+
elif current_app and (
74+
module.startswith("%s." % current_app.import_name)
75+
or module == current_app.import_name
76+
):
77+
frame["in_app"] = True
78+
5979

6080
class FlaskRequestExtractor(RequestExtractor):
6181
@property

sentry_sdk/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def frame_from_traceback(tb, with_locals=True):
264264
abs_path = None
265265
function = None
266266
try:
267-
module = frame.f_globals["__module__"]
267+
module = frame.f_globals["__name__"]
268268
except Exception:
269269
module = None
270270

@@ -354,6 +354,18 @@ def __iter__(self):
354354
def __len__(self):
355355
return len(self._data)
356356

357+
def iter_frames(self):
358+
stacktraces = []
359+
if "stacktrace" in self:
360+
stacktraces.append(self["stacktrace"])
361+
if "exception" in self:
362+
for exception in self["exception"].get("values") or ():
363+
if "stacktrace" in exception:
364+
stacktraces.append(exception["stacktrace"])
365+
for stacktrace in stacktraces:
366+
for frame in stacktrace.get("frames") or ():
367+
yield frame
368+
357369

358370
class SkipEvent(Exception):
359371
"""Risen from an event processor to indicate that the event should be

0 commit comments

Comments
 (0)