Skip to content

fix(spring-jakarta): [Queue Instrumentation 10] Fork root scopes and skip when OTel is active in Kafka consumer#5280

Draft
adinauer wants to merge 1 commit intofix/queue-instrumentation-threadlocal-cleanupfrom
fix/queue-instrumentation-root-scopes
Draft

fix(spring-jakarta): [Queue Instrumentation 10] Fork root scopes and skip when OTel is active in Kafka consumer#5280
adinauer wants to merge 1 commit intofix/queue-instrumentation-threadlocal-cleanupfrom
fix/queue-instrumentation-root-scopes

Conversation

@adinauer
Copy link
Copy Markdown
Member

@adinauer adinauer commented Apr 10, 2026

PR Stack (Queue Instrumentation)


📜 Description

Two changes to SentryKafkaRecordInterceptor:

  1. Fork root scopes — Use Sentry.forkedRootScopes() instead of scopes.forkedScopes(). Each Kafka message is an independent request boundary, so it should start with a clean scope from root rather than inheriting whatever scope happens to be on the consumer thread. This matches the pattern used by SentryWebFilter for reactive request boundaries.

  2. Skip when OTel is active — Add isIgnored() check using SpanUtils.isIgnored() on the trace origin. When OTel is active and the origin is in the ignored span origins list, the interceptor skips instrumentation entirely (no fork, no transaction, no ThreadLocal). This is consistent with how SentryTracingFilter handles OTel coexistence.

💡 Motivation and Context

  • Forking from current scopes on a Kafka consumer thread could inherit stale scope state from a previous message or unrelated context. Root scopes provide proper isolation per message.
  • The OTel dedup via ignored origins ensures Sentry's Kafka instrumentation doesn't create duplicate spans when OTel is also instrumenting Kafka consumers.

💚 How did you test it?

  • Updated existing tests to use Mockito.mockStatic(Sentry::class.java) for forkedRootScopes
  • Added test: does not create span when origin is ignored
  • All 13 tests pass

📝 Checklist

  • I added GH Issue ID & Linear ID
  • 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

Nothing — this completes the root scopes + OTel dedup fix for the consumer interceptor.

⚠️ Merge this PR using a merge commit (not squash). Only the collection branch is squash-merged into main.

#skip-changelog

… Kafka consumer interceptor

Use Sentry.forkedRootScopes() instead of scopes.forkedScopes() so each
Kafka message starts with a clean scope from root, matching the pattern
used by SentryWebFilter for reactive request boundaries.

Add isIgnored() check using SpanUtils.isIgnored() on the trace origin
so the interceptor no-ops when OTel is active and the origin is in the
ignored span origins list, consistent with SentryTracingFilter.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 10, 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).


This PR will not appear in the changelog.


🤖 This preview updates automatically when you update the PR.

@sentry
Copy link
Copy Markdown

sentry bot commented Apr 10, 2026

Sentry Build Distribution

App Name App ID Version Configuration Install Page
SDK Size io.sentry.tests.size 8.37.1 (1) release Install Build

Configure sentry-android build distribution settings

@github-actions
Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 333.60 ms 393.04 ms 59.44 ms
Size 0 B 0 B 0 B

Baseline results on branch: fix/queue-instrumentation-threadlocal-cleanup

Startup times

Revision Plain With Sentry Diff
76b8c09 324.73 ms 382.26 ms 57.53 ms

App size

Revision Plain With Sentry Diff
76b8c09 0 B 0 B 0 B

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.

1 participant