-
Notifications
You must be signed in to change notification settings - Fork 637
test: Add streaming tests to test_http_headers
#6785
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import pytest | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.traces import StreamedSpan | ||
| from sentry_sdk.tracing import Transaction | ||
| from sentry_sdk.tracing_utils import extract_sentrytrace_data | ||
|
|
||
|
|
@@ -26,6 +28,32 @@ | |
| assert parts[2] == "1" if sampled is True else "0" # sampled | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("traces_sample_rate", [1.0, 0.0, None]) | ||
| def test_to_traceparent_span_streaming(sentry_init, traces_sample_rate): | ||
| sentry_init( | ||
| traces_sample_rate=traces_sample_rate, | ||
| _experiments={ | ||
| "trace_lifecycle": "stream", | ||
| }, | ||
| ) | ||
|
|
||
| with sentry_sdk.traces.start_span(name="/interactions/other-dogs/new-dog") as span: | ||
| traceparent = sentry_sdk.get_traceparent() | ||
|
|
||
| parts = traceparent.split("-") | ||
| if traces_sample_rate is None: | ||
| propagation_context = ( | ||
| sentry_sdk.get_current_scope().get_active_propagation_context() | ||
| ) | ||
| assert parts[0] == propagation_context.trace_id # trace_id | ||
| assert parts[1] == propagation_context.span_id # parent_span_id | ||
| assert len(parts) == 2 | ||
| else: | ||
| assert parts[0] == span.trace_id # trace_id | ||
| assert parts[1] == span.span_id # parent_span_id | ||
| assert parts[2] == "1" if traces_sample_rate == 1.0 else "0" # sampled | ||
|
Check warning on line 54 in tests/tracing/test_http_headers.py
|
||
|
|
||
|
Check warning on line 55 in tests/tracing/test_http_headers.py
|
||
|
|
||
| @pytest.mark.parametrize("sampling_decision", [True, False]) | ||
| def test_sentrytrace_extraction(sampling_decision): | ||
| sentrytrace_header = "12312012123120121231201212312012-0415201309082013-{}".format( | ||
|
|
@@ -75,3 +103,24 @@ | |
| assert ( | ||
| headers["sentry-trace"] == "12312012123120121231201212312012-0415201309082013-0" | ||
| ) | ||
|
|
||
|
Check warning on line 106 in tests/tracing/test_http_headers.py
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test With Evidence
Identified by Warden code-review · P8Z-Q9L |
||
|
|
||
| def test_iter_headers_span_streaming(sentry_init, monkeypatch): | ||
| sentry_init( | ||
| traces_sample_rate=0.0, | ||
| _experiments={ | ||
| "trace_lifecycle": "stream", | ||
| }, | ||
| ) | ||
| monkeypatch.setattr( | ||
| StreamedSpan, | ||
| "_to_traceparent", | ||
| mock.Mock(return_value="12312012123120121231201212312012-0415201309082013-0"), | ||
| ) | ||
|
|
||
| with sentry_sdk.traces.start_span(name="/interactions/other-dogs/new-dog") as span: | ||
| headers = dict(span._iter_headers()) | ||
| assert ( | ||
| headers["sentry-trace"] | ||
| == "12312012123120121231201212312012-0415201309082013-0" | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assertion for
traces_sample_rate == 0.0is always truthy and validates nothingDue to Python operator precedence,
assert parts[2] == "1" if traces_sample_rate == 1.0 else "0"is parsed asassert (parts[2] == "1") if traces_sample_rate == 1.0 else "0"— so whentraces_sample_rate == 0.0the asserted value is the string"0", which is always truthy and never checks the actual sampled flag. Useassert parts[2] == ("1" if traces_sample_rate == 1.0 else "0")instead.Evidence
test_to_traceparent_span_streamingthe new assertionassert parts[2] == "1" if traces_sample_rate == 1.0 else "0"(line 54) parses as(parts[2] == "1") if traces_sample_rate == 1.0 else "0"because the conditional expression binds looser than==.traces_sample_rate == 0.0case, the whole expression evaluates to the string literal"0", which is always truthy, soassertnever validates the unsampled traceparent flag.0.0to cover the unsampled path, but that path exercises no real assertion, silently passing regardless ofparts[2].Identified by Warden code-review · HRH-9YM