Skip to content

ref: Remove _capture_experimental_log scope parameter #4424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2025
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
28 changes: 23 additions & 5 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import TYPE_CHECKING, List, Dict, cast, overload
import warnings

import sentry_sdk
from sentry_sdk._compat import PY37, check_uwsgi_thread_support
from sentry_sdk.utils import (
AnnotatedValue,
Expand Down Expand Up @@ -215,8 +216,8 @@ def capture_event(self, *args, **kwargs):
# type: (*Any, **Any) -> Optional[str]
return None

def _capture_experimental_log(self, scope, log):
# type: (Scope, Log) -> None
def _capture_experimental_log(self, log):
# type: (Log) -> None
pass

def capture_session(self, *args, **kwargs):
Expand Down Expand Up @@ -893,12 +894,14 @@ def capture_event(

return return_value

def _capture_experimental_log(self, current_scope, log):
# type: (Scope, Log) -> None
def _capture_experimental_log(self, log):
# type: (Log) -> None
logs_enabled = self.options["_experiments"].get("enable_logs", False)
if not logs_enabled:
return
isolation_scope = current_scope.get_isolation_scope()

current_scope = sentry_sdk.get_current_scope()
isolation_scope = sentry_sdk.get_isolation_scope()

log["attributes"]["sentry.sdk.name"] = SDK_INFO["name"]
log["attributes"]["sentry.sdk.version"] = SDK_INFO["version"]
Expand Down Expand Up @@ -927,6 +930,21 @@ def _capture_experimental_log(self, current_scope, log):
elif propagation_context is not None:
log["trace_id"] = propagation_context.trace_id

# The user, if present, is always set on the isolation scope.
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"),
("user.email", "email"),
):
if (
user_attribute in isolation_scope._user
and log_attribute not in log["attributes"]
):
log["attributes"][log_attribute] = isolation_scope._user[
user_attribute
]

# If debug is enabled, log the log to the console
debug = self.options.get("debug", False)
if debug:
Expand Down
2 changes: 0 additions & 2 deletions sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ def emit(self, record):

def _capture_log_from_record(self, client, record):
# type: (BaseClient, LogRecord) -> None
scope = sentry_sdk.get_current_scope()
otel_severity_number, otel_severity_text = _python_level_to_otel(record.levelno)
project_root = client.options["project_root"]
attrs = self._extra_from_record(record) # type: Any
Expand Down Expand Up @@ -394,7 +393,6 @@ def _capture_log_from_record(self, client, record):

# noinspection PyProtectedMember
client._capture_experimental_log(
scope,
{
"severity_text": otel_severity_text,
"severity_number": otel_severity_number,
Expand Down
4 changes: 1 addition & 3 deletions sentry_sdk/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import time
from typing import Any

from sentry_sdk import get_client, get_current_scope
from sentry_sdk import get_client
from sentry_sdk.utils import safe_repr


def _capture_log(severity_text, severity_number, template, **kwargs):
# type: (str, int, str, **Any) -> None
client = get_client()
scope = get_current_scope()

attrs = {
"sentry.message.template": template,
Expand All @@ -36,7 +35,6 @@ def _capture_log(severity_text, severity_number, template, **kwargs):

# noinspection PyProtectedMember
client._capture_experimental_log(
scope,
{
"severity_text": severity_text,
"severity_number": severity_number,
Expand Down
43 changes: 43 additions & 0 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,49 @@ def test_auto_flush_logs_after_100(sentry_init, capture_envelopes):
raise AssertionError("200 logs were never flushed after five seconds")


def test_user_attributes(sentry_init, capture_envelopes):
"""User attributes are sent if send_default_pii is True."""
sentry_init(send_default_pii=True, _experiments={"enable_logs": True})

sentry_sdk.set_user({"id": "1", "email": "[email protected]", "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

# Check that all expected user attributes are present.
assert log["attributes"].items() >= {
("user.id", "1"),
("user.email", "[email protected]"),
("user.name", "test"),
}


def test_user_attributes_no_pii(sentry_init, capture_envelopes):
"""Ensure no user attributes are sent if send_default_pii is False."""
sentry_init(_experiments={"enable_logs": True})

sentry_sdk.set_user({"id": "1", "email": "[email protected]", "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
Loading