Skip to content

feat!: move tracing to rely on ctx - #1379

Open
giortzisg wants to merge 1 commit into
scopes/scope-mergefrom
scopes/context-tracing
Open

giortzisg wants to merge 1 commit into
scopes/scope-mergefrom
scopes/context-tracing

Conversation

@giortzisg

@giortzisg giortzisg commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Description

Modify existing tracing and propagation behavior to rely on context.Context. Tracing now resolves state from context rather than hubs. This includes a breaking change on ContinueTrace removing the need for a hub to be passed.

#skip-changelog

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/context-tracing branch from 5562df0 to 0e6bf84 Compare July 30, 2026 20:45
@linear-code

linear-code Bot commented Aug 3, 2026

Copy link
Copy Markdown

GO-154

@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 0e6bf84 to 09cf02b Compare August 4, 2026 15:25
@giortzisg giortzisg closed this Aug 4, 2026
@giortzisg giortzisg reopened this Aug 4, 2026
@giortzisg
giortzisg changed the base branch from scopes/context-capture to scopes/context-propagation-api August 4, 2026 15:37
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 09cf02b to 388298c Compare August 4, 2026 15:42
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 388298c to 569c73b Compare August 5, 2026 09:59
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 569c73b to 580aec3 Compare August 10, 2026 11:36
@giortzisg
giortzisg changed the base branch from scopes/context-propagation-api to scopes/context-capture August 13, 2026 08:34
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 580aec3 to 75a6927 Compare August 24, 2026 07:53
@giortzisg
giortzisg marked this pull request as ready for review August 24, 2026 07:54
Comment thread hub.go Outdated
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 75a6927 to bee39bc Compare August 24, 2026 10:17
Comment thread hub.go
Comment thread tracing.go Outdated
@giortzisg giortzisg changed the title feat: move tracing to rely on ctx feat!: move tracing to rely on ctx Sep 1, 2026
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 5a7b7e9 to ec45c0b Compare September 1, 2026 12:57
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch 2 times, most recently from fa24cc9 to 12311a2 Compare September 4, 2026 13:11
@giortzisg
giortzisg changed the base branch from scopes/context-capture to scopes/scope-merge September 4, 2026 13:11
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 12311a2 to e0c450d Compare September 4, 2026 13:26
Comment thread scope.go Outdated

@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.

Stale Bugbot comment from a previous run.

Comment thread http/sentryhttp.go
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from e0c450d to 8a4508e Compare September 7, 2026 11:40

@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.

Stale Bugbot comment from a previous run.

Comment thread scope.go
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 8a4508e to 80793c9 Compare September 7, 2026 11:49

@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.

Stale Bugbot comment from a previous run.

Comment thread tracing.go Outdated

@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.

I think this mostly looks good! I left some questions about some items I was unclear on

Comment thread client.go

func (client *Client) prepareEvent(event *Event, scope *Scope, opts captureOptions) *Event {
scopeProcessors := scope.applyToEvent(event, client, opts.hint, client.options.MaxBreadcrumbs)
func (client *Client) prepareEvent(ctx context.Context, event *Event, scope *Scope, opts captureOptions) *Event {

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.

[question] why do we still take an explicitly passed scope here? Can't we take it from the ctx?

Comment thread client_test.go Outdated
ctx := ContextWithClient(ContextWithScope(context.Background(), scope), client)
transaction := StartTransaction(ctx, "request", WithOpName("http.server"))

require.NotNil(t, client.CaptureMessage(transaction.Context(), "message"))

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.

I notice here we pass transaction.Context(), not ctx. I am assuming that is because of the copy-on-write behavior we were discussing earlier (i.e. ctx remains unchanged and the new context goes on the transaction)?

If that is the case, this new API does seem somewhat unergonomic because it requires an SDK user to remember to capture the message on the specific transaction context in order to get the message to show correctly within said transaction. Is this something you have considered? Is there an alternative API planned which will make this easier for users, or is it a tradeoff we need to accept?

Comment thread hub.go
// hubFromContext returns either a hub stored in the context or the current hub.
// The return value is guaranteed to be non-nil, unlike GetHubFromContext.
func hubFromContext(ctx context.Context) *Hub {
func hubFromContext(ctx context.Context) *Hub { // nolint: unused

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.

[question] why are we keeping this around if it is unused?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removing on the remove hub branch.

Comment thread span_recorder.go
maxSpans := defaultMaxSpans
if client := CurrentHub().Client(); client.IsEnabled() {
if client != nil && client.IsEnabled() {
maxSpans = client.options.MaxSpans

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.

I guess technically out of scope for this PR, but I believe max spans is not meant to be user-configurable? It is not listed as one of the options on this page

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, agreed, but this probably needs to be a separate change.

Comment thread tracing_test.go
Comment on lines -481 to -509
func TestContinueSpanFromRequest(t *testing.T) {
traceID := TraceIDFromHex("bc6d53f15eb88f4320054569b8c553d4")
spanID := SpanIDFromHex("b72fa28504b07285")

for _, sampled := range []Sampled{SampledTrue, SampledFalse, SampledUndefined} {
sampled := sampled
t.Run(sampled.String(), func(t *testing.T) {
var s Span
s.ctx = context.Background()
hkey := http.CanonicalHeaderKey("sentry-trace")
hval := (&Span{
TraceID: traceID,
SpanID: spanID,
Sampled: sampled,
}).ToSentryTrace()
header := http.Header{hkey: []string{hval}}
ContinueFromRequest(&http.Request{Header: header})(&s)
if s.TraceID != traceID {
t.Errorf("got %q, want %q", s.TraceID, traceID)
}
if s.ParentSpanID != spanID {
t.Errorf("got %q, want %q", s.ParentSpanID, spanID)
}
if s.Sampled != sampled {
t.Errorf("got %q, want %q", s.Sampled, sampled)
}
})
}
}

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.

Have this test (and the other removed tests in this file) been fully removed, or is the same logic moving elsewhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed them.

@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 80793c9 to 37acc6f Compare September 16, 2026 09:14
Comment thread hub.go

@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.

Stale Bugbot comment from a previous run.

Comment thread hub.go
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 37acc6f to 645b45e Compare September 17, 2026 08:38

@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 645b45e. Configure here.

Comment thread tracing.go
Use context.Context for active span lookup and parenting while retaining the request transaction on its scope as a stable correlation fallback. Child spans never replace or restore that fallback, and finishing a transaction preserves request correlation.

Freeze dynamic sampling context once for baggage, error capture, and transaction delivery. Explicit client bindings remain authoritative; unbound contexts follow Init. Keep incoming sampling decisions when recording is disabled, inherit local parent sampling before client overrides, and reject inconsistent continuation metadata without retaining a previous request's trace.

ContinueTrace accepts incoming header values as a span option without changing scope state before a transaction starts. StartTransaction returns the existing root without changing its context; callers reusing a transaction must retain their caller context and leave finishing to its owner.
@giortzisg
giortzisg removed this pull request from stack #1420 September 18, 2026 11:17
@giortzisg
giortzisg force-pushed the scopes/context-tracing branch from 645b45e to 65227ad Compare September 18, 2026 11:18
@giortzisg
giortzisg added this pull request to stack #1433 September 18, 2026 11:22
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.

Refactor scope propagation to rely on context.Context

2 participants