Skip to content

Conversation

@giortzisg
Copy link
Contributor

Description

Since the addition of Telemetry Buffers moves the serialization to a background worker, user provided attributes that can be mutated should be deep copied, to avoid panics during serialization.

@giortzisg giortzisg requested a review from lcian November 27, 2025 14:50
@giortzisg giortzisg self-assigned this Nov 27, 2025
@codecov
Copy link

codecov bot commented Nov 27, 2025

Codecov Report

❌ Patch coverage is 28.84615% with 74 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.83%. Comparing base (34261f3) to head (647e8aa).

Files with missing lines Patch % Lines
deepcopy.go 24.32% 50 Missing and 6 partials ⚠️
client.go 0.00% 18 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1148      +/-   ##
==========================================
- Coverage   86.85%   85.83%   -1.02%     
==========================================
  Files          62       63       +1     
  Lines        6092     6184      +92     
==========================================
+ Hits         5291     5308      +17     
- Misses        587      656      +69     
- Partials      214      220       +6     

☔ 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 on lines 330 to 342
clone.breadcrumbs = make([]*Breadcrumb, len(scope.breadcrumbs))
copy(clone.breadcrumbs, scope.breadcrumbs)
clone.breadcrumbs = make([]*Breadcrumb, 0, len(scope.breadcrumbs))
for _, b := range scope.breadcrumbs {
clone.breadcrumbs = append(clone.breadcrumbs, deepCopyBreadcrumb(b))
}
clone.attachments = make([]*Attachment, len(scope.attachments))
copy(clone.attachments, scope.attachments)
for key, value := range scope.tags {
clone.tags[key] = value
}
for key, value := range scope.contexts {
clone.contexts[key] = cloneContext(value)
}
for key, value := range scope.extra {
clone.extra[key] = value
Copy link
Member

Choose a reason for hiding this comment

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

Correct me if I'm wrong, but:
this was here already and was not causing problems, and it's a separate code path from the buffer/transport, so I don't think this should be changed at all.
I assume this was deliberately done this way because scope forking happens frequently and so you wouldn't want to do an expensive deep copy.

// a proper deep copy: if some context values are pointer types (e.g. maps),
// they won't be properly copied.
func cloneContext(c Context) Context {
res := make(Context, len(c))
Copy link
Member

Choose a reason for hiding this comment

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

same as above

client.go Outdated
if event.User.Data != nil {
eventToBuffer.User.Data = deepCopyMapStringString(event.User.Data)
}

Copy link
Member

Choose a reason for hiding this comment

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

Nested in Event there are other things we might care about, for example a Span can have Data (and apparently tags and extra), same about Log attributes, etc. Do we care about those?

@aldy505
Copy link
Contributor

aldy505 commented Dec 30, 2025

...or we can serialize it up front, and then send them to telemetry buffer after serialization? I would assume doing deep copy would inflict in more RAM usage than just serialize it up front.

@giortzisg
Copy link
Contributor Author

giortzisg commented Jan 7, 2026

...or we can serialize it up front, and then send them to telemetry buffer after serialization?

I was weighting both approaches, but i think that pre-serialization has more drawbacks:

  • the scheduler needs the whole event, to get the trace context to build the envelope (we would need to pass metadata around, along with the serialized event)
  • batching logic for logs/spans would also require metadata and more complicated
  • performance wise, it's better to serialize on the background for:
    • request latency for the user is low
    • when rate limited we drop immediately, skipping serialization completely

I would assume doing deep copy would inflict in more RAM

That is a valid concern but we only keep the copy around till we flush the event (short lifespan) and other SDKs already copy some event data.

Comment on lines 443 to 445
}

for key, value := range scope.extra {
event.Extra[key] = value
event.Extra[key] = deepCopyValue(value)
}
}

This comment was marked as outdated.

@aldy505
Copy link
Contributor

aldy505 commented Jan 15, 2026

Okay so what's blocking this PR? Just the reviews?

@giortzisg
Copy link
Contributor Author

Okay so what's blocking this PR? Just the reviews?

Validity mostly.

  • Want to be sure that all values get deep copied correctly (maybe we introduce a library for that)
  • that the issue isn't caused in another resource that we can't deep copy (eg. On SetRequest)

I am also not 100% sure that this is better than preserialization so weighting that as well.

@github-actions
Copy link

github-actions bot commented Feb 3, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • Log specific message for RequestEntityTooLarge by giortzisg in #1185

Bug Fixes 🐛

  • Deep-copy user mutated attributes by giortzisg in #1148

Internal Changes 🔧

  • (deps) Bump getsentry/craft from 2.19.0 to 2.20.1 by dependabot in #1187
  • Rename Telemetry Processor components by giortzisg in #1186

🤖 This preview updates automatically when you update the PR.

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.

4 participants