Skip to content
Merged
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
4 changes: 2 additions & 2 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ def _capture_log(self, log: "Optional[Log]") -> None:
log["attributes"]["sentry.trace.parent_span_id"] = span_id

# The user, if present, is always set on the isolation scope.
if isolation_scope._user is not None:
if self.should_send_default_pii() and isolation_scope._user is not None:
for log_attribute, user_attribute in (
("user.id", "id"),
("user.name", "username"),
Expand Down Expand Up @@ -998,7 +998,7 @@ def _capture_metric(self, metric: "Optional[Metric]") -> None:
if span_id is not None:
metric["span_id"] = span_id

if isolation_scope._user is not None:
if self.should_send_default_pii() and isolation_scope._user is not None:
for metric_attribute, user_attribute in (
("user.id", "id"),
("user.name", "username"),
Expand Down
24 changes: 22 additions & 2 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ def test_auto_flush_logs_after_100(sentry_init, capture_envelopes):


def test_log_user_attributes(sentry_init, capture_envelopes):
"""User attributes are sent if enable_logs is True."""
sentry_init(enable_logs=True)
"""User attributes are sent if enable_logs is True and send_default_pii is True."""
sentry_init(enable_logs=True, send_default_pii=True)

sentry_sdk.set_user({"id": "1", "email": "test@example.com", "username": "test"})
envelopes = capture_envelopes()
Expand All @@ -381,6 +381,26 @@ def test_log_user_attributes(sentry_init, capture_envelopes):
}


def test_log_no_user_attributes_if_no_pii(sentry_init, capture_envelopes):
"""User attributes are not if PII sending is off."""
sentry_init(enable_logs=True, send_default_pii=False)

sentry_sdk.set_user({"id": "1", "email": "test@example.com", "username": "test"})
envelopes = capture_envelopes()

python_logger = logging.Logger("test-logger")
python_logger.warning("Hello, world!")

get_client().flush()

logs = envelopes_to_logs(envelopes)
(log,) = logs

assert "user.id" not in log["attributes"]
assert "user.email" not in log["attributes"]
assert "user.name" not in log["attributes"]


@minimum_python_37
def test_auto_flush_logs_after_5s(sentry_init, capture_envelopes):
"""
Expand Down
21 changes: 20 additions & 1 deletion tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_metrics_with_attributes(sentry_init, capture_envelopes):


def test_metrics_with_user(sentry_init, capture_envelopes):
sentry_init()
sentry_init(send_default_pii=True)
envelopes = capture_envelopes()

sentry_sdk.set_user(
Expand All @@ -135,6 +135,25 @@ def test_metrics_with_user(sentry_init, capture_envelopes):
assert metrics[0]["attributes"]["user.name"] == "testuser"


def test_metrics_no_user_if_pii_off(sentry_init, capture_envelopes):
sentry_init(send_default_pii=False)
envelopes = capture_envelopes()

sentry_sdk.set_user(
{"id": "user-123", "email": "test@example.com", "username": "testuser"}
)
sentry_sdk.metrics.count("test.user.counter", 1)

get_client().flush()

metrics = envelopes_to_metrics(envelopes)
assert len(metrics) == 1

assert "user.id" not in metrics[0]["attributes"]
assert "user.email" not in metrics[0]["attributes"]
assert "user.name" not in metrics[0]["attributes"]


def test_metrics_with_span(sentry_init, capture_envelopes):
sentry_init(traces_sample_rate=1.0)
envelopes = capture_envelopes()
Expand Down
Loading