Skip to content

fix: populate Default attributes on structured logs from current scope#5216

Open
jamescrosswell wants to merge 5 commits into
mainfrom
fix/logs-user-attributes
Open

fix: populate Default attributes on structured logs from current scope#5216
jamescrosswell wants to merge 5 commits into
mainfrom
fix/logs-user-attributes

Conversation

@jamescrosswell
Copy link
Copy Markdown
Collaborator

@jamescrosswell jamescrosswell commented May 11, 2026

Fixes #5209

Problem

When a user is set on the current scope via SentrySdk.ConfigureScope, the user attributes (user.id, user.name, user.email) were not being included on structured logs, despite being documented as automatic.

The root cause was in DefaultSentryStructuredLogger.CaptureLog — the scope was fetched but only used to retrieve the SDK version. The scope's user was never consulted.

Although the initial report only called out the missing user attributes, we should ensure all of the expected Default Attributes are set correctly:

  • Core Attributes
  • User Attributes
  • Server Attributes
  • Integration Attributes

Then Message Template Attributes are already handled correctly.

Audit

All three log emission paths set the default attributes per the docs:

Attribute DefaultSentryStructuredLogger (direct API) Sentry.Extensions.Logging Sentry.Serilog
sentry.environment SetDefaultAttributes
sentry.release SetDefaultAttributes (when non-null)
sentry.sdk.name / sentry.sdk.version ✅ from scope.Sdk ✅ from _sdk override ✅ from Sdk override
server.address SetDefaultAttributes
user.id / user.name / user.email SetDefaultAttributes
sentry.origin ❌ not set (intentional) auto.log.extensions_logging auto.log.serilog
sentry.message.template / sentry.message.parameter.X ✅ written in SentryLog.WriteTo
  • sentry.environment — set unconditionally in SentryLog.SetDefaultAttributes via options.SettingLocator.GetEnvironment(). That call always returns a value (defaults to "production" or "debug" when nothing is configured), so this attribute is always present.

  • sentry.release — set in SentryLog.SetDefaultAttributes when options.SettingLocator.GetRelease() returns non-null (falls back to the SENTRY_RELEASE env var, then ApplicationVersionLocator).

  • sentry.originnot set by SetDefaultAttributes. Each integration calls log.SetOrigin(...) itself after SetDefaultAttributes:

    • Sentry.Extensions.Logging/SentryStructuredLogger.cs"auto.log.extensions_logging"
    • Sentry.Serilog/SentrySink.Structured.cs"auto.log.serilog"

    Direct SentrySdk.Logger.LogInfo(...) calls (via DefaultSentryStructuredLogger) do not set sentry.origin, which matches the docs: "added when generated by an SDK integration".

Note

Sentry.NLog and Sentry.Log4Net don't have structured-log paths yet.

Fixes #5209 — user.id, user.name, and user.email were missing from
structured logs even when user info was set on the scope.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.16%. Comparing base (61a739b) to head (1a5d694).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5216      +/-   ##
==========================================
+ Coverage   74.13%   74.16%   +0.03%     
==========================================
  Files         506      506              
  Lines       18292    18306      +14     
  Branches     3576     3582       +6     
==========================================
+ Hits        13560    13576      +16     
+ Misses       3860     3859       -1     
+ Partials      872      871       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/Sentry/SentryLog.cs Outdated
Comment thread src/Sentry/SentryLog.cs
- Replace redundant SdkVersion parameter with Scope (SDK is on scope)
- Add server.address attribute (from options.ServerName or
  Environment.MachineName when SendDefaultPii is enabled)
- Integrations (Serilog, Extensions.Logging) now mirror their SDK
  identity onto the scope before logging

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@jamescrosswell jamescrosswell changed the title fix: populate user attributes on structured logs from current scope fix: populate Default attributes on structured logs from current scope May 11, 2026
@jamescrosswell jamescrosswell marked this pull request as ready for review May 11, 2026 04:09
@jamescrosswell jamescrosswell requested a review from Flash0ver as a code owner May 11, 2026 04:09
Comment thread src/Sentry/Internal/DefaultSentryStructuredLogger.cs
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.

Scoped attributes are not being sent w/ logs

1 participant