Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add range of executing node to frames #761

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions sentry_sdk/integrations/executing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sentry_sdk.utils import walk_exception_chain, iter_stacks

if MYPY:
from typing import Optional
from typing import Optional, Dict

from sentry_sdk._types import Event, Hint

Expand All @@ -16,6 +16,12 @@
except ImportError:
raise DidNotEnable("executing is not installed")

try:
# Used implicitly, just testing it's available
import asttokens # noqa
except ImportError:
raise DidNotEnable("asttokens is not installed")


class ExecutingIntegration(Integration):
identifier = "executing"
Expand Down Expand Up @@ -61,8 +67,17 @@ def add_executing_info(event, hint):
continue

for sentry_frame, tb in zip(sentry_frames, tbs):
frame = tb.tb_frame
source = executing.Source.for_frame(frame)
sentry_frame["function"] = source.code_qualname(frame.f_code)

ex = executing.Source.executing(tb)
ex.source.asttokens()
sentry_frame["function"] = ex.code_qualname()
if ex.node:
sentry_frame["executing_node"] = {
"start": line_and_column(*ex.node.first_token.start),
"end": line_and_column(*ex.node.last_token.end),
}
return event


def line_and_column(line, column):
# type: (int, int) -> Dict[str, int]
return locals()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"tornado": ["tornado>=5"],
"sqlalchemy": ["sqlalchemy>=1.2"],
"pyspark": ["pyspark>=2.4.4"],
"executing": ["executing", "asttokens"],
},
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down
Empty file.
42 changes: 42 additions & 0 deletions tests/integrations/executing/test_executing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# coding: utf-8
import pytest

from sentry_sdk import capture_exception
from sentry_sdk.integrations.executing import ExecutingIntegration


@pytest.mark.parametrize("integrations", [[], [ExecutingIntegration()]])
def test_executing_integration(sentry_init, capture_events, integrations):
sentry_init(integrations=integrations)
events = capture_events()

def foo():
try:
bar()
except Exception:
capture_exception()

def bar():
1 / 0

foo()

(event,) = events
(thread,) = event["exception"]["values"]
functions = [x["function"] for x in thread["stacktrace"]["frames"]]

if integrations:
assert functions == [
"test_executing_integration.<locals>.foo",
"test_executing_integration.<locals>.bar",
]
node_texts = []
for frame in thread["stacktrace"]["frames"]:
node = frame["executing_node"]
start = node["start"]
end = node["end"]
assert start["line"] == end["line"] == frame["lineno"]
node_texts.append(frame["context_line"][start["column"] : end["column"]])
assert node_texts == ["bar()", "1 / 0"]
else:
assert functions == ["foo", "bar"]
30 changes: 0 additions & 30 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
capture_exception,
capture_event,
)
from sentry_sdk.integrations.executing import ExecutingIntegration
from sentry_sdk.transport import Transport
from sentry_sdk._compat import reraise, text_type, PY2
from sentry_sdk.utils import HAS_CHAINED_EXCEPTIONS
Expand Down Expand Up @@ -217,35 +216,6 @@ def test_with_locals_disabled(sentry_init, capture_events):
)


@pytest.mark.parametrize("integrations", [[], [ExecutingIntegration()]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh damn, thanks! Seems like I overlooked this.

def test_function_names(sentry_init, capture_events, integrations):
sentry_init(integrations=integrations)
events = capture_events()

def foo():
try:
bar()
except Exception:
capture_exception()

def bar():
1 / 0

foo()

(event,) = events
(thread,) = event["exception"]["values"]
functions = [x["function"] for x in thread["stacktrace"]["frames"]]

if integrations:
assert functions == [
"test_function_names.<locals>.foo",
"test_function_names.<locals>.bar",
]
else:
assert functions == ["foo", "bar"]


def test_attach_stacktrace_enabled(sentry_init, capture_events):
sentry_init(attach_stacktrace=True)
events = capture_events()
Expand Down