Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
7541460
ref(integrations): route HTTP header filtering through data collectio…
ericapisani Jul 8, 2026
5e9f610
Merge branch 'master' into py-2584-update-wsgi-filter-headers
ericapisani Jul 9, 2026
a590c8e
Make changes to adjust to the data collection option being within the…
ericapisani Jul 9, 2026
16ed0c8
fix test
ericapisani Jul 9, 2026
486f0bc
make sure host header is not filtered when being added to the url
ericapisani Jul 9, 2026
7d14a81
add test coverage for edge case around host header being dropped but …
ericapisani Jul 9, 2026
bb98334
add aws lambda test coverage
ericapisani Jul 9, 2026
61d1618
fix(aws-lambda): Remove vendored deps from new embedded-sdk test fixt…
ericapisani Jul 9, 2026
951c408
add test coverage within the wsgi test file
ericapisani Jul 9, 2026
a19be39
feat(integrations): apply data_collection cookie filtering to wsgi, s…
ericapisani Jul 10, 2026
620e222
fix(integrations): omit cookies field when data_collection cookies mo…
ericapisani Jul 10, 2026
ec6f520
update tornado test
ericapisani Jul 10, 2026
4dd3ba6
ref(integrations): route HTTP header filtering through data collectio…
ericapisani Jul 8, 2026
d0bae90
Make changes to adjust to the data collection option being within the…
ericapisani Jul 9, 2026
a6c4688
fix test
ericapisani Jul 9, 2026
d7b378b
make sure host header is not filtered when being added to the url
ericapisani Jul 9, 2026
52c2da1
add test coverage for edge case around host header being dropped but …
ericapisani Jul 9, 2026
aa645d7
add aws lambda test coverage
ericapisani Jul 9, 2026
8b6e3ad
fix(aws-lambda): Remove vendored deps from new embedded-sdk test fixt…
ericapisani Jul 9, 2026
d717172
add test coverage within the wsgi test file
ericapisani Jul 9, 2026
c7f590f
Merge branch 'py-2584-update-wsgi-filter-headers' of github.com:getse…
ericapisani Jul 14, 2026
293a291
feat(integrations): apply data_collection cookie filtering to wsgi, s…
ericapisani Jul 10, 2026
530e83c
fix(integrations): omit cookies field when data_collection cookies mo…
ericapisani Jul 10, 2026
326d796
update tornado test
ericapisani Jul 10, 2026
e9f0e20
Merge branch 'master' into py-2584-update-wsgi-filter-headers
ericapisani Jul 15, 2026
48f7c7b
Merge branch 'py-2581-cookies' of github.com:getsentry/sentry-python …
ericapisani Jul 15, 2026
621d79b
Merge branch 'py-2584-update-wsgi-filter-headers' into py-2581-cookies
ericapisani Jul 15, 2026
3d7bddb
test(aiohttp): Expect single span in streaming passthrough test
ericapisani Jul 15, 2026
fad2292
Merge branch 'py-2584-update-wsgi-filter-headers' into py-2581-cookies
ericapisani Jul 15, 2026
40d9f38
feat(wsgi): Apply data_collection filtering to URL query strings
ericapisani Jul 15, 2026
e6e7148
Merge branch 'py-2581-cookies' into py-2583-query-parameters
ericapisani Jul 15, 2026
1f9bce7
lint
ericapisani Jul 15, 2026
fad0c9a
Do not encode what is added to event/span attributes in order to conf…
ericapisani Jul 16, 2026
21cf40b
feat(asgi): Apply data_collection filtering to URL query strings
ericapisani Jul 16, 2026
3f0bc2a
lint
ericapisani Jul 16, 2026
eee4ea2
feat(aiohttp): Apply data_collection filtering to URL query strings
ericapisani Jul 16, 2026
3b17dd3
feat(tornado): Apply data_collection filtering to URL query strings
ericapisani Jul 16, 2026
96e0ac0
test(tornado): Use literal [Filtered] in query param expectations
ericapisani Jul 16, 2026
c3baf08
feat(sanic): Apply data_collection filtering to URL query strings
ericapisani Jul 17, 2026
066b45c
Merge branch 'master' into py-2583-query-params-sanic
ericapisani Jul 22, 2026
b4f5f65
.
ericapisani Jul 22, 2026
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
36 changes: 34 additions & 2 deletions sentry_sdk/integrations/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import sentry_sdk
from sentry_sdk import continue_trace
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.data_collection import (
_apply_data_collection_filtering_to_query_string,
)
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
from sentry_sdk.integrations._wsgi_common import RequestExtractor, _filter_headers
from sentry_sdk.integrations.logging import ignore_logger
Expand All @@ -21,6 +24,7 @@
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
has_data_collection_enabled,
parse_version,
reraise,
)
Expand Down Expand Up @@ -379,8 +383,25 @@ def _get_request_attributes(request: "Request") -> "Dict[str, Any]":
attributes[f"{SPANDATA.HTTP_REQUEST_HEADER}.{header.lower()}"] = value

urlparts = urlsplit(request.url)
client_options = sentry_sdk.get_client().options

if has_data_collection_enabled(client_options):
attributes["url.path"] = urlparts.path

if should_send_default_pii():
filtered_query = None
if urlparts.query:
filtered_query = _apply_data_collection_filtering_to_query_string(
query_string=urlparts.query,
behaviour=client_options["data_collection"]["url_query_params"],
)
if filtered_query:
attributes[SPANDATA.HTTP_QUERY] = filtered_query

attributes[SPANDATA.URL_FULL] = urlparts._replace(
query=filtered_query or ""
).geturl()

elif should_send_default_pii():
attributes[SPANDATA.URL_FULL] = request.url
attributes["url.path"] = urlparts.path

Expand Down Expand Up @@ -421,7 +442,18 @@ def sanic_processor(event: "Event", hint: "Optional[Hint]") -> "Optional[Event]"
urlparts.path,
)

request_info["query_string"] = urlparts.query
client_options = sentry_sdk.get_client().options
if has_data_collection_enabled(client_options):
if urlparts.query:
filtered_query = _apply_data_collection_filtering_to_query_string(
query_string=urlparts.query,
behaviour=client_options["data_collection"]["url_query_params"],
)
if filtered_query:
request_info["query_string"] = filtered_query
else:
request_info["query_string"] = urlparts.query

request_info["method"] = request.method
request_info["env"] = {"REMOTE_ADDR": request.remote_addr}
request_info["headers"] = _filter_headers(dict(request.headers))
Expand Down
155 changes: 155 additions & 0 deletions tests/integrations/sanic/test_sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,158 @@ def child_span_handler(request):
else:
assert "user.ip_address" not in server_span["attributes"]
assert "user.ip_address" not in child_span["attributes"]


_QUERY_PARAM_DATA_COLLECTION_CASES = [
pytest.param(
{"send_default_pii": True},
"toy=tennisball&color=red&auth=secret",
id="send_default_pii_true",
),
pytest.param(
{"send_default_pii": False},
None,
id="send_default_pii_false",
),
pytest.param(
{},
None,
id="defaults",
),
pytest.param(
{"_experiments": {"data_collection": {}}},
"toy=tennisball&color=red&auth=%5BFiltered%5D",
id="data_collection_denylist_default",
),
pytest.param(
{
"_experiments": {
"data_collection": {
"url_query_params": {"mode": "denylist", "terms": ["toy"]}
}
}
},
"toy=%5BFiltered%5D&color=red&auth=%5BFiltered%5D",
id="data_collection_denylist_custom_terms",
),
pytest.param(
{
"_experiments": {
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": ["toy"]}
}
}
},
"toy=tennisball&color=%5BFiltered%5D&auth=%5BFiltered%5D",
id="data_collection_allowlist",
),
pytest.param(
{
"_experiments": {
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": ["auth"]}
}
}
},
"toy=%5BFiltered%5D&color=%5BFiltered%5D&auth=%5BFiltered%5D",
id="data_collection_allowlist_sensitive_term",
),
pytest.param(
{"_experiments": {"data_collection": {"url_query_params": {"mode": "off"}}}},
None,
id="data_collection_off",
),
pytest.param(
{
"send_default_pii": True,
"_experiments": {"data_collection": {"url_query_params": {"mode": "off"}}},
},
None,
id="data_collection_wins_over_send_default_pii",
),
]


@pytest.mark.skipif(
not PERFORMANCE_SUPPORTED, reason="Performance not supported on this Sanic version"
)
@pytest.mark.parametrize(
"init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
)
def test_url_query_data_collection_span_streaming(
sentry_init, app, capture_items, init_kwargs, expected_query
):
init_kwargs = dict(init_kwargs)
experiments = dict(init_kwargs.pop("_experiments", {}))
experiments["trace_lifecycle"] = "stream"
sentry_init(
integrations=[SanicIntegration()],
traces_sample_rate=1.0,
_experiments=experiments,
**init_kwargs,
)

items = capture_items("span")

c = get_client(app)
with c as client:
_, response = client.get("/message?toy=tennisball&color=red&auth=secret")
assert response.status == 200

sentry_sdk.flush()

(server_span,) = [
i.payload
for i in items
if i.payload["attributes"].get("sentry.origin") == "auto.http.sanic"
and i.payload["is_segment"]
]

data_collection_enabled = "data_collection" in experiments
url_attrs_expected = data_collection_enabled or init_kwargs.get(
"send_default_pii", False
)

if expected_query is None:
assert "http.query" not in server_span["attributes"]
if url_attrs_expected:
assert server_span["attributes"]["url.full"].endswith("/message")
assert server_span["attributes"]["url.path"].endswith("/message")
else:
assert "url.full" not in server_span["attributes"]
assert "url.path" not in server_span["attributes"]
else:
assert server_span["attributes"]["http.query"] == expected_query
assert server_span["attributes"]["url.full"].endswith(
f"/message?{expected_query}"
)
assert server_span["attributes"]["url.path"].endswith("/message")


@pytest.mark.parametrize(
"init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
)
def test_url_query_data_collection_event_processor(
sentry_init, app, capture_events, init_kwargs, expected_query
):
sentry_init(integrations=[SanicIntegration()], **init_kwargs)

events = capture_events()

c = get_client(app)
with c as client:
_, response = client.get("/message?toy=tennisball&color=red&auth=secret")
assert response.status == 200

(event,) = events

assert event["request"]["url"].endswith("/message")
assert event["request"]["method"] == "GET"
if "data_collection" not in init_kwargs.get("_experiments", {}):
assert (
event["request"]["query_string"] == "toy=tennisball&color=red&auth=secret"
)
elif expected_query is None:
assert "query_string" not in event["request"]
else:
assert event["request"]["query_string"] == expected_query
Loading