diff --git a/sentry_sdk/integrations/sanic.py b/sentry_sdk/integrations/sanic.py index be536b2a4f..8b80eb7364 100644 --- a/sentry_sdk/integrations/sanic.py +++ b/sentry_sdk/integrations/sanic.py @@ -11,13 +11,11 @@ ) from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.integrations._wsgi_common import RequestExtractor, _filter_headers -from sentry_sdk.scope import should_send_default_pii from sentry_sdk.traces import SegmentNameSource from sentry_sdk.utils import ( capture_internal_exceptions, ensure_integration_enabled, event_from_exception, - has_data_collection_enabled, parse_version, reraise, ) @@ -135,12 +133,8 @@ async def _context_enter(request: "Request") -> None: sentry_sdk.continue_trace(dict(request.headers)) scope.set_custom_sampling_context({"sanic_request": request}) - if request.remote_addr: - if has_data_collection_enabled(client.options): - if client.options["data_collection"]["user_info"]: - scope.set_attribute(SPANDATA.USER_IP_ADDRESS, request.remote_addr) - elif should_send_default_pii(): - scope.set_attribute(SPANDATA.USER_IP_ADDRESS, request.remote_addr) + if request.remote_addr and client.options["data_collection"]["user_info"]: + scope.set_attribute(SPANDATA.USER_IP_ADDRESS, request.remote_addr) span = sentry_sdk.start_span( # Unless the request results in a 404 error, the name and source @@ -258,35 +252,23 @@ def _get_request_attributes(request: "Request") -> "Dict[str, Any]": urlparts = urlsplit(request.url) client_options = sentry_sdk.get_client().options - if has_data_collection_enabled(client_options): - attributes["url.path"] = urlparts.path + attributes["url.path"] = urlparts.path - 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() - - if request.remote_addr: - if client_options["data_collection"]["user_info"]: - attributes[SPANDATA.CLIENT_ADDRESS] = request.remote_addr - - elif should_send_default_pii(): - attributes[SPANDATA.URL_FULL] = request.url - attributes["url.path"] = urlparts.path + 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 - if urlparts.query: - attributes[SPANDATA.HTTP_QUERY] = urlparts.query + attributes[SPANDATA.URL_FULL] = urlparts._replace( + query=filtered_query or "" + ).geturl() - if request.remote_addr: - attributes[SPANDATA.CLIENT_ADDRESS] = request.remote_addr + if request.remote_addr and client_options["data_collection"]["user_info"]: + attributes[SPANDATA.CLIENT_ADDRESS] = request.remote_addr if urlparts.scheme: attributes[SPANDATA.NETWORK_PROTOCOL_NAME] = urlparts.scheme @@ -320,25 +302,17 @@ def sanic_processor(event: "Event", hint: "Optional[Hint]") -> "Optional[Event]" ) 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 + 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 request_info["method"] = request.method - # REMOTE_ADDR was unconditionally set pre-data collection, so it - # continues to be set when data collection is not enabled. - if ( - not has_data_collection_enabled(client_options) - or client_options["data_collection"]["user_info"] - ): + if client_options["data_collection"]["user_info"]: request_info["env"] = {"REMOTE_ADDR": request.remote_addr} request_info["headers"] = _filter_headers(dict(request.headers)) diff --git a/tests/integrations/aiohttp/test_aiohttp.py b/tests/integrations/aiohttp/test_aiohttp.py index e92992cb86..6fa520e590 100644 --- a/tests/integrations/aiohttp/test_aiohttp.py +++ b/tests/integrations/aiohttp/test_aiohttp.py @@ -1410,14 +1410,14 @@ async def hello(request): @pytest.mark.asyncio -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize("data_collection, expect_ip", DATA_COLLECTION_USER_INFO_CASES) async def test_user_address_with_data_collection( - sentry_init, aiohttp_client, capture_items, init_kwargs, expect_ip + sentry_init, aiohttp_client, capture_items, data_collection, expect_ip ): sentry_init( integrations=[AioHttpIntegration()], traces_sample_rate=1.0, - **init_kwargs, + data_collection=data_collection, ) async def hello(request): @@ -2090,12 +2090,12 @@ async def hello(request): @pytest.mark.asyncio @pytest.mark.parametrize( - "init_kwargs, expect_remote_addr", DATA_COLLECTION_REMOTE_ADDR_CASES + "data_collection, expect_remote_addr", DATA_COLLECTION_REMOTE_ADDR_CASES ) async def test_remote_addr_data_collection( - sentry_init, aiohttp_client, capture_events, init_kwargs, expect_remote_addr + sentry_init, aiohttp_client, capture_events, data_collection, expect_remote_addr ): - sentry_init(integrations=[AioHttpIntegration()], **init_kwargs) + sentry_init(integrations=[AioHttpIntegration()], data_collection=data_collection) async def hello(request): capture_message("hi") diff --git a/tests/integrations/arq/test_arq.py b/tests/integrations/arq/test_arq.py index cdf1723390..8f15e48d0f 100644 --- a/tests/integrations/arq/test_arq.py +++ b/tests/integrations/arq/test_arq.py @@ -346,14 +346,14 @@ async def division(_, a, b=0): @pytest.mark.parametrize( - "init_kwargs,expected_args,expected_kwargs", + "data_collection,expected_args,expected_kwargs", DATA_COLLECTION_QUEUES_CASES, ) @pytest.mark.asyncio async def test_job_args_kwargs_data_collection( capture_items, init_arq, - init_kwargs, + data_collection, expected_args, expected_kwargs, ): @@ -364,7 +364,7 @@ async def division(_, a, b=1): pool, worker = init_arq( cls_functions=[division], - init_kwargs=init_kwargs, + init_kwargs={"data_collection": data_collection}, ) job = await pool.enqueue_job("division", 1, b=0) diff --git a/tests/integrations/django/asgi/test_asgi.py b/tests/integrations/django/asgi/test_asgi.py index e5b6775c99..6b4f8a436a 100644 --- a/tests/integrations/django/asgi/test_asgi.py +++ b/tests/integrations/django/asgi/test_asgi.py @@ -16,7 +16,7 @@ from sentry_sdk.integrations.django.asgi import _asgi_middleware_mixin_factory from tests.integrations.django.myapp.asgi import channels_application from tests.integrations.django.utils import pytest_mark_django_db_decorator -from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES +from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES_LEGACY try: from django.urls import reverse @@ -941,7 +941,9 @@ async def test_async_middleware_process_exception_is_awaited( @pytest.mark.skipif( django.VERSION < (3, 0), reason="Django ASGI support shipped in 3.0" ) -@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) @pytest_mark_django_db_decorator() async def test_user_identity_error_event_data_collection( sentry_init, capture_events, application, init_kwargs, expect_user diff --git a/tests/integrations/django/test_data_scrubbing.py b/tests/integrations/django/test_data_scrubbing.py index 6d452ea8b2..9fa4919637 100644 --- a/tests/integrations/django/test_data_scrubbing.py +++ b/tests/integrations/django/test_data_scrubbing.py @@ -7,7 +7,7 @@ from tests.conftest import unpack_werkzeug_response, werkzeug_set_cookie from tests.integrations.django.myapp.wsgi import application from tests.integrations.django.utils import pytest_mark_django_db_decorator -from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES +from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES_LEGACY try: from django.urls import reverse @@ -370,7 +370,9 @@ def test_empty_query_string_is_dropped_with_data_collection( @pytest.mark.forked @pytest_mark_django_db_decorator() -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_info_span_attributes_data_collection( sentry_init, client, capture_items, init_kwargs, expect_ip ): @@ -401,7 +403,9 @@ def test_user_info_span_attributes_data_collection( @pytest.mark.forked @pytest_mark_django_db_decorator() -@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_identity_span_attributes_data_collection( sentry_init, client, capture_items, init_kwargs, expect_user ): @@ -432,7 +436,9 @@ def test_user_identity_span_attributes_data_collection( @pytest.mark.forked @pytest_mark_django_db_decorator() -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_info_error_event_data_collection( sentry_init, client, capture_events, init_kwargs, expect_ip ): @@ -453,7 +459,9 @@ def test_user_info_error_event_data_collection( @pytest.mark.forked @pytest_mark_django_db_decorator() -@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_identity_error_event_data_collection( sentry_init, client, capture_events, init_kwargs, expect_user ): diff --git a/tests/integrations/flask/test_flask.py b/tests/integrations/flask/test_flask.py index 53898543e5..400a42dc33 100644 --- a/tests/integrations/flask/test_flask.py +++ b/tests/integrations/flask/test_flask.py @@ -32,7 +32,7 @@ from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.serializer import MAX_DATABAG_BREADTH -from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES +from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES_LEGACY # Query string used across the query-param filtering tests below. ``auth`` is a # built-in sensitive term, so it is redacted by the default denylist. @@ -1234,7 +1234,9 @@ def test_empty_query_string_is_dropped_with_data_collection( assert "query_string" not in event["request"] -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_info_span_attributes_data_collection( sentry_init, app, capture_items, monkeypatch, init_kwargs, expect_ip ): @@ -1265,7 +1267,9 @@ def test_user_info_span_attributes_data_collection( assert "client.address" not in segment["attributes"] -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_info_error_event_data_collection( sentry_init, app, capture_events, monkeypatch, init_kwargs, expect_ip ): @@ -1316,7 +1320,9 @@ def crash(): assert "ip_address" not in event.get("user", {}) -@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_flask_login_user_identity_error_event_data_collection( sentry_init, app, capture_events, init_kwargs, expect_user ): @@ -1364,7 +1370,9 @@ def crash(): assert "username" not in user -@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_flask_login_user_identity_span_attributes_data_collection( sentry_init, app, capture_items, init_kwargs, expect_user ): diff --git a/tests/integrations/huey/test_huey.py b/tests/integrations/huey/test_huey.py index d528307033..5c37081cc4 100644 --- a/tests/integrations/huey/test_huey.py +++ b/tests/integrations/huey/test_huey.py @@ -10,7 +10,7 @@ from sentry_sdk.integrations.huey import HueyIntegration from sentry_sdk.traces import SegmentNameSource, SpanStatus from sentry_sdk.utils import parse_version -from tests.integrations.utils import DATA_COLLECTION_QUEUES_CASES +from tests.integrations.utils import DATA_COLLECTION_QUEUES_CASES_LEGACY HUEY_VERSION = parse_version(HUEY_VERSION) @@ -205,7 +205,7 @@ def maybe_locked_task(): @pytest.mark.parametrize( "init_kwargs,expected_args,expected_kwargs", - DATA_COLLECTION_QUEUES_CASES, + DATA_COLLECTION_QUEUES_CASES_LEGACY, ) def test_task_args_kwargs_data_collection( capture_items, diff --git a/tests/integrations/litestar/test_litestar.py b/tests/integrations/litestar/test_litestar.py index 363b83fdab..8e4b7d26c6 100644 --- a/tests/integrations/litestar/test_litestar.py +++ b/tests/integrations/litestar/test_litestar.py @@ -19,7 +19,7 @@ from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE from tests.conftest import ApproxDict from tests.integrations.conftest import parametrize_test_configurable_status_codes -from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES +from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES_LEGACY def litestar_app_factory(middleware=None, debug=True, exception_handlers=None): @@ -448,7 +448,9 @@ def test_span_origin( assert span["attributes"]["sentry.origin"] == "auto.http.litestar" -@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_litestar_scope_user_on_exception_event( sentry_init, capture_exceptions, diff --git a/tests/integrations/pyramid/test_pyramid.py b/tests/integrations/pyramid/test_pyramid.py index 57548b4620..6f68708b44 100644 --- a/tests/integrations/pyramid/test_pyramid.py +++ b/tests/integrations/pyramid/test_pyramid.py @@ -15,7 +15,7 @@ from sentry_sdk.serializer import MAX_DATABAG_BREADTH from sentry_sdk.traces import SpanStatus from tests.conftest import unpack_werkzeug_response -from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES +from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES_LEGACY def hi(request): @@ -536,7 +536,9 @@ def test_span_origin( assert segment["attributes"]["sentry.origin"] == "auto.http.pyramid" -@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_span_sets_user_id_on_segment( sentry_init, pyramid_config, @@ -575,7 +577,9 @@ def authenticated_userid(self, request): assert "user.id" not in segment["attributes"] -@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_id_error_event_data_collection( sentry_init, pyramid_config, diff --git a/tests/integrations/rq/test_rq.py b/tests/integrations/rq/test_rq.py index 68a3afae3b..dc566a3e26 100644 --- a/tests/integrations/rq/test_rq.py +++ b/tests/integrations/rq/test_rq.py @@ -8,7 +8,7 @@ from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.rq import RqIntegration from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE, parse_version -from tests.integrations.utils import DATA_COLLECTION_QUEUES_CASES +from tests.integrations.utils import DATA_COLLECTION_QUEUES_CASES_LEGACY @pytest.fixture(autouse=True) @@ -91,7 +91,7 @@ def test_basic( @pytest.mark.parametrize( "init_kwargs,expected_args,expected_kwargs", - DATA_COLLECTION_QUEUES_CASES, + DATA_COLLECTION_QUEUES_CASES_LEGACY, ) def test_job_args_kwargs_data_collection( sentry_init, diff --git a/tests/integrations/sanic/test_sanic.py b/tests/integrations/sanic/test_sanic.py index 6ca80edd4b..e25dce66d0 100644 --- a/tests/integrations/sanic/test_sanic.py +++ b/tests/integrations/sanic/test_sanic.py @@ -104,7 +104,7 @@ def reusable_client(app): def test_request_data(sentry_init, app, capture_events): - sentry_init(integrations=[SanicIntegration()]) + sentry_init(integrations=[SanicIntegration()], data_collection={}) events = capture_events() c = get_client(app) @@ -368,7 +368,17 @@ def __init__( self.expected_source = expected_source -@pytest.mark.parametrize("send_pii", [True, False]) +@pytest.mark.parametrize( + "data_collection, expect_query", + [ + pytest.param({}, True, id="data_collection_default"), + pytest.param( + {"url_query_params": {"mode": "off"}}, + False, + id="data_collection_url_query_params_off", + ), + ], +) @pytest.mark.parametrize( "test_config", [ @@ -407,13 +417,14 @@ def test_transactions( sentry_init: "Any", app: "Any", capture_items: "Any", - send_pii: bool, + data_collection: "Any", + expect_query: bool, ) -> None: # Init the SanicIntegration with the desired arguments sentry_init( integrations=[SanicIntegration()], traces_sample_rate=1.0, - send_default_pii=send_pii, + data_collection=data_collection, ) items = capture_items("span") @@ -458,16 +469,16 @@ def test_transactions( "error" if test_config.expected_status >= 400 else "ok" ) - if send_pii: - assert attrs["url.full"].endswith(test_config.url) - assert attrs["url.path"] == test_config.url.split("?")[0] - if "?" in test_config.url: + expected_url = ( + test_config.url if expect_query else test_config.url.split("?", 1)[0] + ) + assert attrs["url.full"].endswith(expected_url) + assert attrs["url.path"] == test_config.url.split("?")[0] + if "?" in test_config.url: + if expect_query: assert attrs["http.query"] == test_config.url.split("?", 1)[1] - - else: - assert "url.full" not in attrs - assert "url.path" not in attrs - assert "http.query" not in attrs + else: + assert "http.query" not in attrs def test_span_origin(sentry_init, app, capture_items): @@ -492,9 +503,9 @@ def test_span_origin(sentry_init, app, capture_items): assert segment["attributes"]["sentry.origin"] == "auto.http.sanic" -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize("data_collection, expect_ip", DATA_COLLECTION_USER_INFO_CASES) def test_user_ip_address_on_all_spans( - sentry_init, app, capture_items, init_kwargs, expect_ip + sentry_init, app, capture_items, data_collection, expect_ip ): app.config.FORWARDED_SECRET = "test" @@ -508,7 +519,7 @@ def child_span_handler(request): integrations=[SanicIntegration()], default_integrations=False, traces_sample_rate=1.0, - **init_kwargs, + data_collection=data_collection, ) items = capture_items("span") @@ -532,9 +543,9 @@ def child_span_handler(request): assert "user.ip_address" not in child_span["attributes"] -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize("data_collection, expect_ip", DATA_COLLECTION_USER_INFO_CASES) def test_client_address_span_attribute_data_collection( - sentry_init, app, capture_items, init_kwargs, expect_ip + sentry_init, app, capture_items, data_collection, expect_ip ): app.config.FORWARDED_SECRET = "test" @@ -542,7 +553,7 @@ def test_client_address_span_attribute_data_collection( integrations=[SanicIntegration()], default_integrations=False, traces_sample_rate=1.0, - **init_kwargs, + data_collection=data_collection, ) items = capture_items("span") @@ -570,78 +581,43 @@ def test_client_address_span_attribute_data_collection( _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}, + {"url_query_params": {"mode": "off"}}, None, - id="send_default_pii_false", + id="data_collection_off", ), pytest.param( {}, - None, - id="defaults", - ), - pytest.param( - {"data_collection": {}}, "toy=tennisball&color=red&auth=%5BFiltered%5D", id="data_collection_denylist_default", ), pytest.param( - { - "data_collection": { - "url_query_params": {"mode": "denylist", "terms": ["toy"]} - } - }, + {"url_query_params": {"mode": "denylist", "terms": ["toy"]}}, "toy=%5BFiltered%5D&color=red&auth=%5BFiltered%5D", id="data_collection_denylist_custom_terms", ), pytest.param( - { - "data_collection": { - "url_query_params": {"mode": "allowlist", "terms": ["toy"]} - } - }, + {"url_query_params": {"mode": "allowlist", "terms": ["toy"]}}, "toy=tennisball&color=%5BFiltered%5D&auth=%5BFiltered%5D", id="data_collection_allowlist", ), pytest.param( - { - "data_collection": { - "url_query_params": {"mode": "allowlist", "terms": ["auth"]} - } - }, + {"url_query_params": {"mode": "allowlist", "terms": ["auth"]}}, "toy=%5BFiltered%5D&color=%5BFiltered%5D&auth=%5BFiltered%5D", id="data_collection_allowlist_sensitive_term", ), - pytest.param( - {"data_collection": {"url_query_params": {"mode": "off"}}}, - None, - id="data_collection_off", - ), - pytest.param( - { - "send_default_pii": True, - "data_collection": {"url_query_params": {"mode": "off"}}, - }, - None, - id="data_collection_wins_over_send_default_pii", - ), ] @pytest.mark.parametrize( - "init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES + "data_collection, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES ) def test_url_query_data_collection( - sentry_init, app, capture_items, init_kwargs, expected_query + sentry_init, app, capture_items, data_collection, expected_query ): sentry_init( integrations=[SanicIntegration()], traces_sample_rate=1.0, - **init_kwargs, + data_collection=data_collection, ) items = capture_items("span") @@ -660,19 +636,10 @@ def test_url_query_data_collection( and i.payload["is_segment"] ] - data_collection_enabled = "data_collection" in init_kwargs - 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"] + assert server_span["attributes"]["url.full"].endswith("/message") + assert server_span["attributes"]["url.path"].endswith("/message") else: assert server_span["attributes"]["http.query"] == expected_query assert server_span["attributes"]["url.full"].endswith( @@ -682,12 +649,12 @@ def test_url_query_data_collection( @pytest.mark.parametrize( - "init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES + "data_collection, 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, app, capture_events, data_collection, expected_query ): - sentry_init(integrations=[SanicIntegration()], **init_kwargs) + sentry_init(integrations=[SanicIntegration()], data_collection=data_collection) events = capture_events() @@ -700,11 +667,7 @@ def test_url_query_data_collection_event_processor( assert event["request"]["url"].endswith("/message") assert event["request"]["method"] == "GET" - if "data_collection" not in init_kwargs: - assert ( - event["request"]["query_string"] == "toy=tennisball&color=red&auth=secret" - ) - elif expected_query is None: + if expected_query is None: assert "query_string" not in event["request"] else: assert event["request"]["query_string"] == expected_query @@ -791,12 +754,12 @@ def oversized_handler(request): @pytest.mark.parametrize( - "init_kwargs, expect_remote_addr", DATA_COLLECTION_REMOTE_ADDR_CASES + "data_collection, expect_remote_addr", DATA_COLLECTION_REMOTE_ADDR_CASES ) def test_remote_addr_data_collection( - sentry_init, app, capture_events, init_kwargs, expect_remote_addr + sentry_init, app, capture_events, data_collection, expect_remote_addr ): - sentry_init(integrations=[SanicIntegration()], **init_kwargs) + sentry_init(integrations=[SanicIntegration()], data_collection=data_collection) events = capture_events() c = get_client(app) diff --git a/tests/integrations/starlite/test_starlite.py b/tests/integrations/starlite/test_starlite.py index 6693b6a40f..a6d5be2018 100644 --- a/tests/integrations/starlite/test_starlite.py +++ b/tests/integrations/starlite/test_starlite.py @@ -13,7 +13,7 @@ from sentry_sdk import capture_message from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE from sentry_sdk.integrations.starlite import StarliteIntegration -from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES +from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES_LEGACY def starlite_app_factory(middleware=None, debug=True, exception_handlers=None): @@ -398,7 +398,9 @@ def test_span_origin(sentry_init, capture_items): assert item.payload["attributes"]["sentry.origin"] == "auto.http.starlite" -@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_starlite_scope_user_on_exception_event( sentry_init, capture_exceptions, capture_events, init_kwargs, expect_user ): diff --git a/tests/integrations/tornado/test_tornado.py b/tests/integrations/tornado/test_tornado.py index 0750d49747..bfe0f0873f 100644 --- a/tests/integrations/tornado/test_tornado.py +++ b/tests/integrations/tornado/test_tornado.py @@ -9,8 +9,8 @@ from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE from sentry_sdk.integrations.tornado import TornadoIntegration from tests.integrations.utils import ( - DATA_COLLECTION_REMOTE_ADDR_CASES, - DATA_COLLECTION_USER_INFO_CASES, + DATA_COLLECTION_REMOTE_ADDR_CASES_LEGACY, + DATA_COLLECTION_USER_INFO_CASES_LEGACY, ) @@ -602,7 +602,9 @@ def get(self): assert "user" not in event -@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_auth_data_collection( tornado_testcase, sentry_init, capture_events, init_kwargs, expect_user ): @@ -845,7 +847,9 @@ def test_span_origin( assert segment["attributes"]["sentry.origin"] == "auto.http.tornado" -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_ip_address_on_all_spans( tornado_testcase, sentry_init, capture_items, init_kwargs, expect_ip ): @@ -872,7 +876,9 @@ def test_user_ip_address_on_all_spans( assert "user.ip_address" not in child_span["attributes"] -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_client_address_span_attribute_data_collection( tornado_testcase, sentry_init, capture_items, init_kwargs, expect_ip ): @@ -902,7 +908,7 @@ def test_client_address_span_attribute_data_collection( @pytest.mark.parametrize( - "init_kwargs, expect_remote_addr", DATA_COLLECTION_REMOTE_ADDR_CASES + "init_kwargs, expect_remote_addr", DATA_COLLECTION_REMOTE_ADDR_CASES_LEGACY ) def test_remote_addr_data_collection( tornado_testcase, sentry_init, capture_events, init_kwargs, expect_remote_addr diff --git a/tests/integrations/utils.py b/tests/integrations/utils.py index 5f5999089f..4fd3dec490 100644 --- a/tests/integrations/utils.py +++ b/tests/integrations/utils.py @@ -1,32 +1,32 @@ import pytest # Shared parametrization test matrix for ``data_collection.user_info`` setting. -# Each case is ``(init_kwargs, expect_user_info)`` where the second element indicates +# Each case is ``(data_collection, expect_user_info)`` where the second element indicates # whether user info (IP address, user identity, etc.) is expected to be collected. DATA_COLLECTION_USER_INFO_CASES = [ pytest.param( - {"data_collection": {"user_info": True}}, + {"user_info": True}, True, id="data_collection_user_info_true", ), pytest.param( - {"data_collection": {"user_info": False}}, + {"user_info": False}, False, id="data_collection_user_info_false", ), ] # Shared parametrization test matrix for ``REMOTE_ADDR`` on events. -# Each case is ``(init_kwargs, expect_remote_addr)``. +# Each case is ``(data_collection, expect_remote_addr)``. DATA_COLLECTION_REMOTE_ADDR_CASES = [ - pytest.param({"data_collection": {}}, True, id="data_collection_default"), + pytest.param({}, True, id="data_collection_default"), pytest.param( - {"data_collection": {"user_info": True}}, + {"user_info": True}, True, id="data_collection_user_info_true", ), pytest.param( - {"data_collection": {"user_info": False}}, + {"user_info": False}, False, id="data_collection_user_info_false", ), @@ -34,25 +34,44 @@ # Shared parametrization test matrix for ``data_collection.queues`` setting. -# Each case is ``(init_kwargs, expected_args, expected_kwargs)`` where ``None`` for +# Each case is ``(data_collection, expected_args, expected_kwargs)`` where ``None`` for # the expected values means args/kwargs are not collected at all. DATA_COLLECTION_QUEUES_CASES = [ pytest.param( - {"data_collection": {}}, + {}, [1], {"b": 0}, id="data_collection_default", ), pytest.param( - {"data_collection": {"queues": True}}, + {"queues": True}, [1], {"b": 0}, id="data_collection_queues_on", ), pytest.param( - {"data_collection": {"queues": False}}, + {"queues": False}, None, None, id="data_collection_queues_off", ), ] + + +DATA_COLLECTION_USER_INFO_CASES_LEGACY = [ + pytest.param({"data_collection": case.values[0]}, case.values[1], id=case.id) + for case in DATA_COLLECTION_USER_INFO_CASES +] +DATA_COLLECTION_REMOTE_ADDR_CASES_LEGACY = [ + pytest.param({"data_collection": case.values[0]}, case.values[1], id=case.id) + for case in DATA_COLLECTION_REMOTE_ADDR_CASES +] +DATA_COLLECTION_QUEUES_CASES_LEGACY = [ + pytest.param( + {"data_collection": case.values[0]}, + case.values[1], + case.values[2], + id=case.id, + ) + for case in DATA_COLLECTION_QUEUES_CASES +] diff --git a/tests/integrations/wsgi/test_wsgi.py b/tests/integrations/wsgi/test_wsgi.py index 12fed06ff8..c71a78c44e 100644 --- a/tests/integrations/wsgi/test_wsgi.py +++ b/tests/integrations/wsgi/test_wsgi.py @@ -11,7 +11,7 @@ _ScopedResponse, get_request_url, ) -from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES +from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES_LEGACY @pytest.fixture @@ -1114,7 +1114,9 @@ def dogpark(environ, start_response): assert "user.ip_address" not in child_span["attributes"] -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_info_span_attributes_data_collection( sentry_init, capture_items, init_kwargs, expect_ip ): @@ -1151,7 +1153,9 @@ def dogpark(environ, start_response): assert "client.address" not in server_span["attributes"] -@pytest.mark.parametrize("init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES) +@pytest.mark.parametrize( + "init_kwargs, expect_ip", DATA_COLLECTION_USER_INFO_CASES_LEGACY +) def test_user_info_error_event_data_collection( sentry_init, crashing_app, capture_events, init_kwargs, expect_ip ):