From e61b1e70468617849d1d23fba8334f148a2adc83 Mon Sep 17 00:00:00 2001 From: Farhat Nawaz Date: Tue, 18 Apr 2023 03:14:57 +0500 Subject: [PATCH 1/3] ref: Add `include_source_context` option Some users do not like the source context to be there, and so add `include_source_context` option to opt-out. Fixes #2017 --- sentry_sdk/utils.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index cc91e37448..46f20c8872 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -594,8 +594,10 @@ def filename_for_module(module, abs_path): return abs_path -def serialize_frame(frame, tb_lineno=None, include_local_variables=True): - # type: (FrameType, Optional[int], bool) -> Dict[str, Any] +def serialize_frame( + frame, tb_lineno=None, include_local_variables=True, include_source_context=True +): + # type: (FrameType, Optional[int], bool, bool) -> Dict[str, Any] f_code = getattr(frame, "f_code", None) if not f_code: abs_path = None @@ -611,18 +613,24 @@ def serialize_frame(frame, tb_lineno=None, include_local_variables=True): if tb_lineno is None: tb_lineno = frame.f_lineno - pre_context, context_line, post_context = get_source_context(frame, tb_lineno) - rv = { "filename": filename_for_module(module, abs_path) or None, "abs_path": os.path.abspath(abs_path) if abs_path else None, "function": function or "", "module": module, "lineno": tb_lineno, - "pre_context": pre_context, - "context_line": context_line, - "post_context": post_context, } # type: Dict[str, Any] + + if include_source_context: + pre_context, context_line, post_context = get_source_context(frame, tb_lineno) + rv.update( + { + "pre_context": pre_context, + "context_line": context_line, + "post_context": post_context, + } + ) + if include_local_variables: rv["vars"] = frame.f_locals @@ -1240,7 +1248,6 @@ def sanitize_url(url, remove_authority=True, remove_query_values=True): def parse_url(url, sanitize=True): - # type: (str, bool) -> ParsedUrl """ Splits a URL into a url (including path), query and fragment. If sanitize is True, the query From 964690bf4380560fe772a8728085730871567904 Mon Sep 17 00:00:00 2001 From: Farhat Nawaz Date: Tue, 18 Apr 2023 03:19:10 +0500 Subject: [PATCH 2/3] Add tests --- tests/test_utils.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 7578e6255b..275885745d 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,7 +1,14 @@ import pytest import re +import sys -from sentry_sdk.utils import is_valid_sample_rate, logger, parse_url, sanitize_url +from sentry_sdk.utils import ( + is_valid_sample_rate, + logger, + parse_url, + sanitize_url, + serialize_frame, +) try: from unittest import mock # python 3.3 and above @@ -221,3 +228,16 @@ def test_warns_on_invalid_sample_rate(rate, StringContaining): # noqa: N803 result = is_valid_sample_rate(rate, source="Testing") logger.warning.assert_any_call(StringContaining("Given sample rate is invalid")) assert result is False + + +@pytest.mark.parametrize( + "include_source_context", + [True, False], +) +def test_include_source_context_when_serializing_frame(include_source_context): + frame = sys._getframe() + result = serialize_frame(frame, include_source_context=include_source_context) + + assert (include_source_context ^ ("pre_context" in result)) ^ True + assert (include_source_context ^ ("context_line" in result)) ^ True + assert (include_source_context ^ ("post_context" in result)) ^ True From 02490f0a8db8618cdb253cc7226e999fa1a4a942 Mon Sep 17 00:00:00 2001 From: Farhat Nawaz Date: Wed, 19 Apr 2023 00:24:15 +0500 Subject: [PATCH 3/3] small refactor --- sentry_sdk/utils.py | 9 ++------- tests/test_utils.py | 6 +++--- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 46f20c8872..3800ed205d 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -622,13 +622,8 @@ def serialize_frame( } # type: Dict[str, Any] if include_source_context: - pre_context, context_line, post_context = get_source_context(frame, tb_lineno) - rv.update( - { - "pre_context": pre_context, - "context_line": context_line, - "post_context": post_context, - } + rv["pre_context"], rv["context_line"], rv["post_context"] = get_source_context( + frame, tb_lineno ) if include_local_variables: diff --git a/tests/test_utils.py b/tests/test_utils.py index 275885745d..aa88d26c44 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -238,6 +238,6 @@ def test_include_source_context_when_serializing_frame(include_source_context): frame = sys._getframe() result = serialize_frame(frame, include_source_context=include_source_context) - assert (include_source_context ^ ("pre_context" in result)) ^ True - assert (include_source_context ^ ("context_line" in result)) ^ True - assert (include_source_context ^ ("post_context" in result)) ^ True + assert include_source_context ^ ("pre_context" in result) ^ True + assert include_source_context ^ ("context_line" in result) ^ True + assert include_source_context ^ ("post_context" in result) ^ True