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
74 changes: 24 additions & 50 deletions sentry_sdk/integrations/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down
12 changes: 6 additions & 6 deletions tests/integrations/aiohttp/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions tests/integrations/arq/test_arq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions tests/integrations/django/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions tests/integrations/django/test_data_scrubbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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
):
Expand All @@ -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
):
Expand Down
18 changes: 13 additions & 5 deletions tests/integrations/flask/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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
):
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/huey/test_huey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions tests/integrations/litestar/test_litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions tests/integrations/pyramid/test_pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/rq/test_rq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
Loading
Loading