Skip to content

feat(integrations): Gate user info behind data_collection config#6876

Open
ericapisani wants to merge 23 commits into
masterfrom
py-2583-misc
Open

feat(integrations): Gate user info behind data_collection config#6876
ericapisani wants to merge 23 commits into
masterfrom
py-2583-misc

Conversation

@ericapisani

@ericapisani ericapisani commented Jul 23, 2026

Copy link
Copy Markdown
Member

This is a clean up pass from areas that I missed initially.

Refs #6743
Fixes #6746

Apply the data_collection.user_info setting to Django user identity (id, email, username) collection, giving it precedence over the legacy send_default_pii boolean when explicitly configured. This mirrors the existing behavior for client IP/user info already shipped for Flask, WSGI, and ASGI.

The user extraction logic in _after_get_response is pulled into a shared helper used by both the WSGI and ASGI event processors, and the parametrized send_default_pii/data_collection test matrix is extracted into tests/integrations/django/utils.py for reuse across the WSGI and ASGI test suites.

Refs PY-2583
The new test was the only DB-touching test without @pytest.mark.forked. Running in the parent process leaked the created user into the shared in-memory sqlite DB (UNIQUE constraint failures in test_basic) and caused the parent to hold the session-scoped django_db_setup fixture, so forked children skipped postgres test DB creation but still destroyed it on teardown (database does not exist errors).
Move the identical USER_INFO_INIT_KWARGS parametrization from the flask, wsgi, and django test suites into tests/integrations/utils.py as DATA_COLLECTION_USER_INFO_CASES, documenting that the second element of each case is whether user info is expected to be collected.
Apply the data_collection.user_info setting to the Sanic integration's user IP collection, giving it precedence over the legacy send_default_pii boolean when explicitly configured. This covers both the user.ip_address scope attribute applied to all spans and the client.address span attribute, mirroring the behavior already shipped for Flask, WSGI, ASGI, and Django.

The user IP span test is reparametrized with the shared DATA_COLLECTION_USER_INFO_CASES matrix from tests/integrations/utils.py, and a new test covers the previously untested client.address span attribute.

Refs PY-2583
Apply the data_collection.user_info setting to the Tornado integration's user info collection, giving it precedence over the legacy send_default_pii boolean when explicitly configured. This covers the user.ip_address scope attribute applied to all spans, the client.address span attribute, and the is_authenticated user flag on error events, mirroring the behavior already shipped for Flask, WSGI, ASGI, Django, and Sanic.

The user IP span test is reparametrized with the shared DATA_COLLECTION_USER_INFO_CASES matrix from tests/integrations/utils.py, and new tests cover the previously untested client.address span attribute and the data_collection gating of the is_authenticated flag.

Refs PY-2583
Apply the data_collection.user_info setting to the Pyramid integration's user id collection, giving it precedence over the legacy send_default_pii boolean when explicitly configured. This covers both the user.id segment attribute under span streaming and the user id attached to error events, mirroring the behavior already shipped for Flask, WSGI, ASGI, Django, Sanic, and Tornado.

The user id segment test is reparametrized with the shared DATA_COLLECTION_USER_INFO_CASES matrix from tests/integrations/utils.py, and a new test adds the first coverage of the user id on error events.

Refs PY-2583
Apply the data_collection.user_info setting to the aiohttp integration's user IP collection under span streaming, giving it precedence over the legacy send_default_pii boolean when explicitly configured. This covers both the client.address span attribute and the user.ip_address scope attribute, mirroring the behavior already shipped for Flask, WSGI, ASGI, Django, Sanic, Tornado, and Pyramid.

Add test_user_address_with_data_collection_and_span_streaming, parametrized with the shared DATA_COLLECTION_USER_INFO_CASES matrix from tests/integrations/utils.py, and remove the now-redundant IP assertion block from test_sensitive_header_passthrough_with_pii_span_streaming that its TODO reserved for this change.

Refs PY-2583
Apply the data_collection.user_info setting to the Starlite and Litestar integrations' user info collection on exception events, giving it precedence over the legacy send_default_pii boolean when explicitly configured. This gates the retrieve_user_from_scope call in each integration's exception handler, mirroring the behavior already shipped for Flask, WSGI, ASGI, Django, Sanic, Tornado, Pyramid, and aiohttp.

The scope user exception event tests in both suites are reparametrized with the shared DATA_COLLECTION_USER_INFO_CASES matrix from tests/integrations/utils.py.

Refs PY-2583
@linear-code

linear-code Bot commented Jul 23, 2026

Copy link
Copy Markdown

PY-2583

@ericapisani
ericapisani changed the base branch from master to py-2583-starlite-and-litestar July 23, 2026 20:52
The request event processor reads aws_event["identity"] at the top level,
but the payloads used by the user info tests nested it under requestContext,
so no user was attached and the assertions failed in CI.
@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

97631 passed | ❌ 1 failed | ⏭️ 6410 skipped | Total: 104042 | Pass Rate: 93.84% | Execution Time: 351m 56s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +200
Passed Tests 📈 +200
Failed Tests
Skipped Tests

➕ New Tests (1)

View new tests
  • test_trace_decorator_no_trx
    • File: tests.tracing.test_decorator
    • Status: ❌ Failing

➖ Removed Tests (1)

View removed tests
  • test_circular_references_span_streaming
    • File: tests.tracing.test_misc

❌ Failed Tests

test_trace_decorator_no_trx

File: tests.tracing.test_decorator
Suite: py3.10-gevent
Error: AssertionError: Expected 'mock' to be called once. Called 2 times. Calls: [call('Cannot create a child span for %s. Please start a Sentry transaction before calling this function.', 'test_decorator.my_example_function'), call('Cannot create a child span for %s. Please start a Sentry transaction before calling this function.', 'time.sleep'), call('[Monitor] health check negative, downsampling with a factor of %d', 10), call('Cannot create a child span for %s. Please start a Sentry transaction before calling this function.', 'time.sleep')].

Stack Trace
tests/tracing/test_decorator.py:45: in test_trace_decorator_no_trx
    fake_debug.assert_called_once_with(
/usr/lib/python3.10/unittest/mock.py:940: in assert_called_once_with
    raise AssertionError(msg)
E   AssertionError: Expected 'mock' to be called once. Called 2 times.
E   Calls: [call('Cannot create a child span for %s. Please start a Sentry transaction before calling this function.', 'test_decorator.my_example_function'),
E    call('Cannot create a child span for %s. Please start a Sentry transaction before calling this function.', 'time.sleep'),
E    call('[Monitor] health check negative, downsampling with a factor of %d', 10),
E    call('Cannot create a child span for %s. Please start a Sentry transaction before calling this function.', 'time.sleep')].

❌ Patch coverage is 75.00%. Project has 2528 uncovered lines.
❌ Project coverage is 89.65%. Comparing base (base) to head (head).

Files with missing lines (2)
File Patch % Lines
sentry_sdk/integrations/aws_lambda.py 20.00% ⚠️ 12 Missing and 2 partials
sentry_sdk/integrations/quart.py 100.00% ⚠️ 3 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    89.71%    89.65%    -0.06%
==========================================
  Files          193       193         —
  Lines        24392     24434       +42
  Branches      8642      8678       +36
==========================================
+ Hits         21882     21906       +24
- Misses        2510      2528       +18
- Partials      1399      1403        +4

Generated by Codecov Action

Comment thread sentry_sdk/integrations/aws_lambda.py
Comment thread tests/integrations/aws_lambda/test_aws_lambda.py Outdated
The JSON payloads in test_request_data_with_send_default_pii_true and USER_INFO_PAYLOAD contained trailing commas inside requestContext, which the Lambda runtime rejects with InvalidRequestContent. This caused three test failures in CI.
user_info = sentry_event.setdefault("user", {})

identity = aws_event.get("identity")
identity = aws_event.get("requestContext", {}).get("identity")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bug that was found in adding test coverage for this. Docs for the expected payload can be found here but the tl;dr image from the page:

Image

@ericapisani
ericapisani marked this pull request as ready for review July 24, 2026 14:30
@ericapisani
ericapisani requested a review from a team as a code owner July 24, 2026 14:30
Base automatically changed from py-2583-starlite-and-litestar to master July 24, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support redacting/removing user data based on the data collection configuration

1 participant