Skip to content

feat!: add scope merging mechanism - #1373

Open
giortzisg wants to merge 1 commit into
scopes/scope-storagefrom
scopes/scope-merge
Open

giortzisg wants to merge 1 commit into
scopes/scope-storagefrom
scopes/scope-merge

Conversation

@giortzisg

@giortzisg giortzisg commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Description

This adds the scope merging mechanism by allocating a captureState object and copying all needed event attributes in place. This removes taking multiple scope clones on every capture.

The PR also includes breaking changes around EventModifier. The EventModifier was an agnostic modifier that was passed on ApplyToEvent. This duplicated the EventProcessor behavior and also complicated the scope merging mechanism, since we need to have a concrete *Scope type to merge the scopes. Furthermore, there is no documented behavior or signature for any EventModifier and this seems to be just the first archaic way to modify events before EventProcessor and BeforeSend hooks where a thing. Thus decided to remove this and have a concrete *Scope passed on ApplyToEvent. This behavior aligns with other sdks that pass a concrete scope.

This affects the public API with all the CaptureX methods now requiring a concrete scope to be passed, but further scope changes in the stack also change the CaptureX APIs to work with context.Context, so this change seems acceptable. Functionality isn't really removed but just shifted to BeforeSend hooks as intended.

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

@linear-code

linear-code Bot commented Jul 23, 2026

Copy link
Copy Markdown

GO-155

@giortzisg
giortzisg force-pushed the scopes/scope-merge branch from 8f6a8b4 to e509cb6 Compare July 23, 2026 08:45
@giortzisg
giortzisg marked this pull request as ready for review July 23, 2026 08:51
Comment thread scope.go Outdated
@giortzisg
giortzisg force-pushed the scopes/scope-merge branch from e509cb6 to 5ea0d02 Compare July 30, 2026 20:45
@giortzisg
giortzisg force-pushed the scopes/scope-merge branch from 5ea0d02 to 2905cce Compare August 4, 2026 15:25
Comment thread scope.go Outdated
@giortzisg
giortzisg force-pushed the scopes/scope-merge branch 2 times, most recently from 8b46a73 to b3bd79f Compare August 5, 2026 09:59
Comment thread scope.go Outdated
@giortzisg
giortzisg force-pushed the scopes/scope-merge branch 2 times, most recently from a32be8d to af060b8 Compare August 12, 2026 12:11
Comment thread scope.go
Comment thread scope.go Outdated
Comment thread scope.go Outdated
Comment thread scope.go Outdated
Comment thread scope.go Outdated
Comment thread metrics.go Outdated
Comment thread client.go Outdated
@giortzisg
giortzisg requested a review from Litarnus September 1, 2026 09:13
Comment thread client.go

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

Admittedly only skimmed this, as the PR is quite huge, but I left a few questions

Comment thread internal/sentrytest/fixture.go
Comment thread client.go Outdated
Comment thread client.go
Comment thread log.go
Comment thread internal/util/map.go Outdated
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 scope_merge.go Outdated
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 scope.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.

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 5d18fe6. Configure here.

Comment thread scope.go
Comment thread client.go Outdated
Apply the single scope selected by the capture context directly to events, preserve event precedence, enforce breadcrumb limits, and run request processing and event processors after releasing the scope lock. Preserve explicitly supplied event and scope trace contexts, and retain dynamic sampling context only when it belongs to the selected trace.

Event values now take precedence over scope values during capture, context scopes replace the global scope, and scope breadcrumbs and attachments are applied before event-specific values.
@giortzisg
giortzisg removed this pull request from stack #1420 September 18, 2026 11:17
@giortzisg
giortzisg added this pull request to stack #1433 September 18, 2026 11:22
Comment thread scope.go
Comment on lines +523 to +526
func mergeBreadcrumbs(scope, event []*Breadcrumb, limit int) []*Breadcrumb {
switch {
case limit < 0:
return 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 MaxBreadcrumbs is negative, mergeBreadcrumbs returns nil, which clears any pre-existing breadcrumbs on an event instead of preserving them.
Severity: MEDIUM

Suggested Fix

Modify mergeBreadcrumbs to return the original eventBreadcrumbs slice unmodified when the limit is negative. This will preserve any breadcrumbs already on the event while correctly preventing new scope breadcrumbs from being added, aligning with the intended behavior of ignoring breadcrumbs.

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: scope.go#L523-L526

Potential issue: When `MaxBreadcrumbs` is set to a negative value, the
`mergeBreadcrumbs` function unconditionally returns `nil`. This `nil` value is then
assigned to `event.Breadcrumbs` in the `applyToEvent` function. This action overwrites
and destroys any breadcrumbs that a user may have pre-populated on the event before it
was captured. The previous implementation would have preserved these existing
breadcrumbs, as it only appended scope breadcrumbs if they were present. This change
introduces a regression that causes the loss of user-provided data in a configuration
intended to ignore new breadcrumbs, not clear existing ones.

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.

Add scope merging for global and isolation scopes

2 participants