Skip to content

Commit fbd7d1a

Browse files
farhat-nawazfarhatnawazantonpirkersentrivana
authored
Ref: Add include_source_context option in utils (#2020)
Some users do not like the source context to be there, and so add `include_source_context` option to opt-out. --------- Co-authored-by: Farhat Nawaz <[email protected]> Co-authored-by: Anton Pirker <[email protected]> Co-authored-by: Ivana Kellyerova <[email protected]>
1 parent eb5ee4a commit fbd7d1a

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

sentry_sdk/utils.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,10 @@ def filename_for_module(module, abs_path):
594594
return abs_path
595595

596596

597-
def serialize_frame(frame, tb_lineno=None, include_local_variables=True):
598-
# type: (FrameType, Optional[int], bool) -> Dict[str, Any]
597+
def serialize_frame(
598+
frame, tb_lineno=None, include_local_variables=True, include_source_context=True
599+
):
600+
# type: (FrameType, Optional[int], bool, bool) -> Dict[str, Any]
599601
f_code = getattr(frame, "f_code", None)
600602
if not f_code:
601603
abs_path = None
@@ -611,18 +613,19 @@ def serialize_frame(frame, tb_lineno=None, include_local_variables=True):
611613
if tb_lineno is None:
612614
tb_lineno = frame.f_lineno
613615

614-
pre_context, context_line, post_context = get_source_context(frame, tb_lineno)
615-
616616
rv = {
617617
"filename": filename_for_module(module, abs_path) or None,
618618
"abs_path": os.path.abspath(abs_path) if abs_path else None,
619619
"function": function or "<unknown>",
620620
"module": module,
621621
"lineno": tb_lineno,
622-
"pre_context": pre_context,
623-
"context_line": context_line,
624-
"post_context": post_context,
625622
} # type: Dict[str, Any]
623+
624+
if include_source_context:
625+
rv["pre_context"], rv["context_line"], rv["post_context"] = get_source_context(
626+
frame, tb_lineno
627+
)
628+
626629
if include_local_variables:
627630
rv["vars"] = frame.f_locals
628631

@@ -1240,7 +1243,6 @@ def sanitize_url(url, remove_authority=True, remove_query_values=True):
12401243

12411244

12421245
def parse_url(url, sanitize=True):
1243-
12441246
# type: (str, bool) -> ParsedUrl
12451247
"""
12461248
Splits a URL into a url (including path), query and fragment. If sanitize is True, the query

tests/test_utils.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import pytest
22
import re
3+
import sys
34

4-
from sentry_sdk.utils import is_valid_sample_rate, logger, parse_url, sanitize_url
5+
from sentry_sdk.utils import (
6+
is_valid_sample_rate,
7+
logger,
8+
parse_url,
9+
sanitize_url,
10+
serialize_frame,
11+
)
512

613
try:
714
from unittest import mock # python 3.3 and above
@@ -221,3 +228,16 @@ def test_warns_on_invalid_sample_rate(rate, StringContaining): # noqa: N803
221228
result = is_valid_sample_rate(rate, source="Testing")
222229
logger.warning.assert_any_call(StringContaining("Given sample rate is invalid"))
223230
assert result is False
231+
232+
233+
@pytest.mark.parametrize(
234+
"include_source_context",
235+
[True, False],
236+
)
237+
def test_include_source_context_when_serializing_frame(include_source_context):
238+
frame = sys._getframe()
239+
result = serialize_frame(frame, include_source_context=include_source_context)
240+
241+
assert include_source_context ^ ("pre_context" in result) ^ True
242+
assert include_source_context ^ ("context_line" in result) ^ True
243+
assert include_source_context ^ ("post_context" in result) ^ True

0 commit comments

Comments
 (0)