Skip to content

Reduce scope forking when using OpenTelemetry #4565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: feat/e2e-tests-console-otel
Choose a base branch
from

Conversation

adinauer
Copy link
Member

@adinauer adinauer commented Jul 21, 2025

📜 Description

We are now re-wrapping Context after with has been called on the wrapped context. We do not perform any additional scope forking in this case.

We now also wrap the root context immediately.

💡 Motivation and Context

Previously our scope forking logic kicked in more often than desired because our wrapped Context would be converted to a new ArrayBasedContext on every invocation of with that didn't set a span.

This broke our withScope (and siblings) where we were passing a forked scope into the callback but when making the forked Scopes the current ones, our OpenTelemetry wrapper would also fork again. This caused the parameter passed into the callback of withScope to be different from Sentry.getCurrentScopes().getScope().

This change also reduces scope forking, when Context is being written to like this:

try (final @NotNull Scope s1 = Context.current().with(ContextKey.named("testkey"), "test1").makeCurrent()) {
...
}

Previously this would have caused the scopes to be forked later.

💚 How did you test it?

📝 Checklist

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Copy link
Contributor

github-actions bot commented Jul 21, 2025

Fails
🚫 Please consider adding a changelog entry for the next release.
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

Example:

## Unreleased

- Reduce scope forking when using OpenTelemetry ([#4565](https://github.com/getsentry/sentry-java/pull/4565))

If none of the above apply, you can opt out of this check by adding #skip-changelog to the PR description.

Generated by 🚫 dangerJS against c496629

Copy link
Contributor

github-actions bot commented Jul 21, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 403.07 ms 472.79 ms 69.72 ms
Size 1.58 MiB 2.09 MiB 519.45 KiB

Previous results on branch: feat/otel-less-scope-forking

Startup times

Revision Plain With Sentry Diff
b7e5a4f 438.98 ms 493.47 ms 54.49 ms

App size

Revision Plain With Sentry Diff
b7e5a4f 1.58 MiB 2.09 MiB 519.05 KiB

Copy link
Member

@lcian lcian left a comment

Choose a reason for hiding this comment

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

Good catch, LGTM

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.

3 participants