Skip to content

Commit 7bc4d62

Browse files
improved validation
1 parent 7cf3d7c commit 7bc4d62

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

sentry_sdk/tracing.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -979,18 +979,19 @@ def _in_http_status_code_range(self, code, code_ranges):
979979
return True
980980
continue
981981

982-
if (
983-
len(target) != 2
984-
or not isinstance(target[0], int)
985-
or not isinstance(target[1], int)
986-
):
987-
logger.warning(
988-
"trace_ignore_status_codes must be a sequences including only integers or pairs of integers."
989-
)
990-
continue
991-
992-
if target[0] <= code <= target[1]:
993-
return True
982+
wrong_type_message = "trace_ignore_status_codes must be a list of integers or pairs of integers."
983+
try:
984+
if (
985+
len(target) == 2
986+
and isinstance(target[0], int)
987+
and isinstance(target[1], int)
988+
and target[0] <= code <= target[1]
989+
):
990+
return True
991+
elif not isinstance(target[0], int) or not isinstance(target[1], int):
992+
logger.warning(wrong_type_message)
993+
except (TypeError, IndexError):
994+
logger.warning(wrong_type_message)
994995

995996
return False
996997

@@ -1074,7 +1075,7 @@ def finish(
10741075
):
10751076
logger.debug(
10761077
"[Tracing] Discarding {transaction_description} because the HTTP status code {status_code} is matched by trace_ignore_status_codes: {trace_ignore_status_codes}".format(
1077-
transaction_description=self._get_log_reprensentation(),
1078+
transaction_description=self._get_log_representation(),
10781079
status_code=self._data[SPANDATA.HTTP_STATUS_CODE],
10791080
trace_ignore_status_codes=client.options[
10801081
"trace_ignore_status_codes"

tests/tracing/test_ignore_status_codes.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ def test_single_code_ignored(sentry_init, capture_events, status_code):
3939
def test_range_ignored(sentry_init, capture_events, status_code):
4040
sentry_init(
4141
traces_sample_rate=1.0,
42-
trace_ignore_status_codes=((305, 399),),
42+
trace_ignore_status_codes=(
43+
(
44+
305,
45+
399,
46+
),
47+
),
4348
)
4449
events = capture_events()
4550

@@ -61,8 +66,14 @@ def test_variety_ignored(sentry_init, capture_events, status_code):
6166
301,
6267
302,
6368
303,
64-
(305, 399),
65-
(401, 404),
69+
(
70+
305,
71+
399,
72+
),
73+
(
74+
401,
75+
404,
76+
),
6677
),
6778
)
6879
events = capture_events()
@@ -79,3 +90,31 @@ def test_variety_ignored(sentry_init, capture_events, status_code):
7990
assert not events
8091
else:
8192
assert len(events) == 1
93+
94+
95+
def test_malformed_argument_ignored(sentry_init, capture_events):
96+
sentry_init(
97+
traces_sample_rate=1.0,
98+
trace_ignore_status_codes=(
99+
404.0,
100+
"404",
101+
"401-404",
102+
(404,),
103+
(
104+
"401",
105+
"404",
106+
),
107+
(
108+
401,
109+
404,
110+
500,
111+
),
112+
),
113+
)
114+
events = capture_events()
115+
116+
with start_transaction(op="http", name="GET /"):
117+
span_or_tx = sentry_sdk.get_current_span()
118+
span_or_tx.set_data("http.response.status_code", 404)
119+
120+
assert len(events) == 1

0 commit comments

Comments
 (0)