Skip to content

Extract additional expression values with pure_eval #750

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

Closed
wants to merge 2 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
47 changes: 45 additions & 2 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import os
import sys
import linecache
Expand Down Expand Up @@ -27,8 +28,22 @@

from sentry_sdk._types import ExcInfo, EndpointType

epoch = datetime(1970, 1, 1)
try:
import executing
except ImportError:
executing = None

try:
import asttokens
except ImportError:
asttokens = None

try:
import pure_eval
except ImportError:
pure_eval = None

epoch = datetime(1970, 1, 1)

# The logger is created here but initialized in the debug support module
logger = logging.getLogger("sentry_sdk.errors")
Expand Down Expand Up @@ -453,11 +468,39 @@ def serialize_frame(frame, tb_lineno=None, with_locals=True):
"post_context": post_context,
} # type: Dict[str, Any]
if with_locals:
rv["vars"] = frame.f_locals
rv["vars"] = pure_eval_frame(frame)
rv["vars"].update(frame.f_locals)

return rv


def pure_eval_frame(frame):
if not (executing and pure_eval and asttokens):
return {}

source = executing.Source.for_frame(frame)
if not source.tree:
return {}

statements = source.statements_at_line(frame.f_lineno)
if not statements:
return {}

stmt = list(statements)[0]
while True:
# Get the parent first in case the original statement is already
# a function definition, e.g. if we're calling a decorator
# In that case we still want the surrounding scope, not that function
stmt = stmt.parent
if isinstance(stmt, (ast.FunctionDef, ast.ClassDef, ast.Module)):
break

evaluator = pure_eval.Evaluator.from_frame(frame)
expressions = evaluator.interesting_expressions_grouped(stmt)
atok = source.asttokens()
return {atok.get_text(nodes[0]): value for nodes, value in expressions}


def stacktrace_from_traceback(tb=None, with_locals=True):
# type: (Optional[TracebackType], bool) -> Dict[str, List[Dict[str, Any]]]
return {
Expand Down
3 changes: 3 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ pytest-cov==2.8.1
gevent
eventlet
newrelic
executing
pure_eval
asttokens
12 changes: 11 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,13 @@ def e(exc):
def test_with_locals_enabled():
events = []
hub = Hub(Client(with_locals=True, transport=events.append))

def foo():
foo.d = {1: 2}
print(foo.d[1] / 0)

try:
1 / 0
foo()
except Exception:
hub.capture_exception()

Expand All @@ -192,6 +197,11 @@ def test_with_locals_enabled():
for frame in event["exception"]["values"][0]["stacktrace"]["frames"]
)

frame_vars = event["exception"]["values"][0]["stacktrace"]["frames"][-1]["vars"]
assert sorted(frame_vars.keys()) == ["foo", "foo.d", "foo.d[1]"]
assert frame_vars["foo.d"] == {"1": "2"}
assert frame_vars["foo.d[1]"] == "2"


def test_with_locals_disabled():
events = []
Expand Down