Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
157 changes: 157 additions & 0 deletions tests/integrations/sanic/test_sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,160 @@ def child_span_handler(request):
else:
assert "user.ip_address" not in server_span["attributes"]
assert "user.ip_address" not in child_span["attributes"]


NO_QUERY_STRING = object()

_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},
NO_QUERY_STRING,
id="send_default_pii_false",
),
pytest.param(
{},
NO_QUERY_STRING,
id="defaults",
),
pytest.param(
{"_experiments": {"data_collection": {}}},
"toy=tennisball&color=red&auth=[Filtered]",
id="data_collection_denylist_default",
),
pytest.param(
{
"_experiments": {
"data_collection": {
"url_query_params": {"mode": "denylist", "terms": ["toy"]}
}
}
},
"toy=[Filtered]&color=red&auth=[Filtered]",
id="data_collection_denylist_custom_terms",
),
pytest.param(
{
"_experiments": {
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": ["toy"]}
}
}
},
"toy=tennisball&color=[Filtered]&auth=[Filtered]",
id="data_collection_allowlist",
),
pytest.param(
{
"_experiments": {
"data_collection": {
"url_query_params": {"mode": "allowlist", "terms": ["auth"]}
}
}
},
"toy=[Filtered]&color=[Filtered]&auth=[Filtered]",
id="data_collection_allowlist_sensitive_term",
),
pytest.param(
{"_experiments": {"data_collection": {"url_query_params": {"mode": "off"}}}},
NO_QUERY_STRING,
id="data_collection_off",
),
pytest.param(
{
"send_default_pii": True,
"_experiments": {"data_collection": {"url_query_params": {"mode": "off"}}},
},
NO_QUERY_STRING,
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 NO_QUERY_STRING:
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 NO_QUERY_STRING:
assert "query_string" not in event["request"]
else:
assert event["request"]["query_string"] == expected_query
Loading