Skip to content

Commit 5606bb3

Browse files
authored
fix(logs, metrics): Gate metrics, logs user attributes behind send_default_pii (#5240)
User attributes on logs and metrics are currently not gated behind `send_default_pii`, which is a bug.
1 parent 6046f2d commit 5606bb3

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

sentry_sdk/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ def _capture_log(self, log: "Optional[Log]") -> None:
934934
log["attributes"]["sentry.trace.parent_span_id"] = span_id
935935

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

1001-
if isolation_scope._user is not None:
1001+
if self.should_send_default_pii() and isolation_scope._user is not None:
10021002
for metric_attribute, user_attribute in (
10031003
("user.id", "id"),
10041004
("user.name", "username"),

tests/test_logs.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ def test_auto_flush_logs_after_100(sentry_init, capture_envelopes):
359359

360360

361361
def test_log_user_attributes(sentry_init, capture_envelopes):
362-
"""User attributes are sent if enable_logs is True."""
363-
sentry_init(enable_logs=True)
362+
"""User attributes are sent if enable_logs is True and send_default_pii is True."""
363+
sentry_init(enable_logs=True, send_default_pii=True)
364364

365365
sentry_sdk.set_user({"id": "1", "email": "[email protected]", "username": "test"})
366366
envelopes = capture_envelopes()
@@ -381,6 +381,26 @@ def test_log_user_attributes(sentry_init, capture_envelopes):
381381
}
382382

383383

384+
def test_log_no_user_attributes_if_no_pii(sentry_init, capture_envelopes):
385+
"""User attributes are not if PII sending is off."""
386+
sentry_init(enable_logs=True, send_default_pii=False)
387+
388+
sentry_sdk.set_user({"id": "1", "email": "[email protected]", "username": "test"})
389+
envelopes = capture_envelopes()
390+
391+
python_logger = logging.Logger("test-logger")
392+
python_logger.warning("Hello, world!")
393+
394+
get_client().flush()
395+
396+
logs = envelopes_to_logs(envelopes)
397+
(log,) = logs
398+
399+
assert "user.id" not in log["attributes"]
400+
assert "user.email" not in log["attributes"]
401+
assert "user.name" not in log["attributes"]
402+
403+
384404
@minimum_python_37
385405
def test_auto_flush_logs_after_5s(sentry_init, capture_envelopes):
386406
"""

tests/test_metrics.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_metrics_with_attributes(sentry_init, capture_envelopes):
117117

118118

119119
def test_metrics_with_user(sentry_init, capture_envelopes):
120-
sentry_init()
120+
sentry_init(send_default_pii=True)
121121
envelopes = capture_envelopes()
122122

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

137137

138+
def test_metrics_no_user_if_pii_off(sentry_init, capture_envelopes):
139+
sentry_init(send_default_pii=False)
140+
envelopes = capture_envelopes()
141+
142+
sentry_sdk.set_user(
143+
{"id": "user-123", "email": "[email protected]", "username": "testuser"}
144+
)
145+
sentry_sdk.metrics.count("test.user.counter", 1)
146+
147+
get_client().flush()
148+
149+
metrics = envelopes_to_metrics(envelopes)
150+
assert len(metrics) == 1
151+
152+
assert "user.id" not in metrics[0]["attributes"]
153+
assert "user.email" not in metrics[0]["attributes"]
154+
assert "user.name" not in metrics[0]["attributes"]
155+
156+
138157
def test_metrics_with_span(sentry_init, capture_envelopes):
139158
sentry_init(traces_sample_rate=1.0)
140159
envelopes = capture_envelopes()

0 commit comments

Comments
 (0)