Skip to content

feat: add trace methods to public API - #1394

Open
giortzisg wants to merge 4 commits into
scopes/context-tracingfrom
scopes/integration-primitives
Open

giortzisg wants to merge 4 commits into
scopes/context-tracingfrom
scopes/integration-primitives

Conversation

@giortzisg

@giortzisg giortzisg commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Description

This adds new trace propagation APIs that resolve with context. Also changes logs and metrics to resolve scope through context rather than the old hub API.

Issues

Changelog Entry Instructions

To add a custom changelog entry, uncomment the section above. Supports:

  • Single entry: just write text
  • Multiple entries: use bullet points
  • Nested bullets: indent 4+ spaces

For more details: custom changelog entries

Reminders

@giortzisg
giortzisg force-pushed the scopes/integration-primitives branch from 4d788fd to deeab0c Compare August 24, 2026 10:53
@giortzisg
giortzisg marked this pull request as ready for review August 24, 2026 13:24
Comment thread scope.go
@giortzisg
giortzisg force-pushed the scopes/integration-primitives branch from deeab0c to 7e6baff Compare August 31, 2026 10:50
Comment thread metrics.go Outdated
Comment thread log.go
@giortzisg
giortzisg force-pushed the scopes/integration-primitives branch from 7e6baff to 51c535e Compare September 1, 2026 09:12

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 51c535e. Configure here.

Comment thread log.go
@giortzisg
giortzisg requested a review from Litarnus September 1, 2026 09:19
@giortzisg
giortzisg force-pushed the scopes/integration-primitives branch from 51c535e to d7b1f55 Compare September 1, 2026 12:57
@giortzisg
giortzisg force-pushed the scopes/integration-primitives branch from d7b1f55 to 45876fc Compare September 2, 2026 07:59
@giortzisg
giortzisg force-pushed the scopes/integration-primitives branch from 45876fc to 71754bd Compare September 4, 2026 13:11
Comment thread tracing.go Outdated
@giortzisg
giortzisg force-pushed the scopes/integration-primitives branch from 71754bd to 4ec37b3 Compare September 4, 2026 13:26
Comment thread tracing.go Outdated
@giortzisg
giortzisg force-pushed the scopes/integration-primitives branch from 4ec37b3 to c4e83dd Compare September 7, 2026 11:40
Comment thread tracing.go Outdated
@giortzisg
giortzisg force-pushed the scopes/integration-primitives branch from c4e83dd to c2a58ea Compare September 7, 2026 11:49

@szokeasaurusrex szokeasaurusrex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The changes mostly look okay, but it seems this PR contains more changes than are suggested by the PR description. Was there perhaps a problem when rebasing the stack? Or these are all intended to be here?

Edit: Nvm, I re-read the description and suppose everything is in scope 😅

Comment thread log_fallback.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What was this file's purpose? Was it for enable_logs == false?

Comment thread log_test.go Outdated
Comment on lines +23 to +39
type panicStringer struct{}

func (panicStringer) String() string {
panic("disabled logs must not format values")
}

// flushFromContext flushes the client from the given context.
func flushFromContext(ctx context.Context, timeout time.Duration) {
hub := GetHubFromContext(ctx)
if hub == nil {
hub = CurrentHub()
}
hub.Flush(timeout)
ClientFromContext(ctx).Flush(timeout)
}

func TestDisabledLoggerDoesNotFormatValues(_ *testing.T) {
ctx := ContextWithClient(context.Background(), NewNoopClient())
logger := NewLogger(ctx)

logger.Info().Emit(panicStringer{})
logger.Info().Emitf("%v", panicStringer{})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice!

Comment thread metrics.go
Comment on lines +76 to 85
fallbackCtx := m.fallbackCtx
scope := ScopeFromContext(ctx)
if scope == nil {
scope = ScopeFromContext(fallbackCtx)
} else {
fallbackCtx = nil
}
if options.scope != nil {
scope = options.scope
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

l: It looks like we had similar logic in log.go. Perhaps we can extract it into a function?

Comment thread scope.go
Comment on lines +548 to +555
setString("sentry.release", client.options.Release)
setString("sentry.environment", client.options.Environment)
setString("sentry.server.address", serverAddress)
setString("sentry.sdk.name", sdkName)
setString("sentry.sdk.version", client.sdkVersion)
setString("user.id", scope.user.ID)
setString("user.name", scope.user.Name)
setString("user.email", scope.user.Email)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

m: It would probably be a good idea to extract these attribute names into constants rather than hardcoding them

Resolve log, metric, and propagation data from context-backed clients and scopes, and expose context-based trace header helpers while retaining the existing Hub compatibility APIs.
@giortzisg
giortzisg force-pushed the scopes/integration-primitives branch from c2a58ea to aeb2126 Compare September 16, 2026 09:14
Comment thread metrics.go
Comment on lines +79 to +83
scope = ScopeFromContext(fallbackCtx)
} else {
fallbackCtx = nil
}
if options.scope != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: When a scope is found in the primary context, the fallback context is set to nil, preventing a search for trace information and potentially losing existing trace context.
Severity: MEDIUM

Suggested Fix

Do not set fallbackCtx to nil when a scope is found in the primary context. Allow activeTraceFromContexts to search both the primary and fallback contexts to ensure trace information is not lost, preserving the behavior of the previous implementation.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: metrics.go#L79-L83

Potential issue: When a scope is found in the primary context `ctx`, the `fallbackCtx`
is explicitly set to `nil`. This prevents the subsequent call to
`activeTraceFromContexts` from searching the fallback context for an active span or
trace. This can cause a loss of trace context in scenarios where a logger or meter is
initialized with a context containing a span, and later used via `.WithCtx()` with a
different context that has a scope but no span. The trace from the original context will
be dropped, silently breaking trace continuity.

Also affects:

  • log.go:90~94

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.

2 participants