Skip to content

feat: Implement strict trace continuation#4981

Merged
jamescrosswell merged 6 commits intomainfrom
feat/strict-trace-continuation-v2
May 7, 2026
Merged

feat: Implement strict trace continuation#4981
jamescrosswell merged 6 commits intomainfrom
feat/strict-trace-continuation-v2

Conversation

@giortzisg
Copy link
Copy Markdown
Contributor

@giortzisg giortzisg commented Mar 4, 2026

Summary

By default, the SDK should extract the organization ID from the DSN (e.g. o123.ingest.sentry.io) and compare it with the sentry-org_id value in incoming baggage headers. When the two differ, the SDK should start a fresh trace instead of continuing the foreign one. This guards against accidentally linking traces across organizations.

Implementation

Implements strict trace continuation to validate org IDs in distributed traces, preventing trace continuation from unknown third-party services.

  • Parse org_id from DSN host (e.g., o1 in https://key@o1.ingest.us.sentry.io/123 yields org ID "1")
  • Add OrgId config option to manually override DSN-parsed value
  • Add StrictTraceContinuation bool config option (default false)
  • Propagate sentry-org_id in outgoing baggage via DynamicSamplingContext
  • Validate org IDs in Hub.ContinueTrace():
    • Mismatched org IDs always start a new trace (regardless of setting)
    • Missing incoming org_id + StrictTraceContinuation: true -> start new trace
    • Missing incoming org_id + StrictTraceContinuation: false -> continue trace (default behavior)

Changes

Source files:

  • src/Sentry/Dsn.cs - Parse org ID from DSN host subdomain
  • src/Sentry/SentryOptions.cs - Add StrictTraceContinuation and OrgId options, GetEffectiveOrgId() helper
  • src/Sentry/DynamicSamplingContext.cs - Add org_id to outgoing baggage in all DSC factory methods
  • src/Sentry/Internal/Hub.cs - Add ShouldContinueTrace() validation logic in ContinueTrace()

Test files:

  • test/Sentry.Tests/Protocol/DsnTests.cs - Tests for DSN org ID parsing
  • test/Sentry.Tests/HubTests.cs - Comprehensive [Theory] tests for all org ID validation scenarios

Snapshot files:

  • Updated 4 API approval snapshot files with new OrgId and StrictTraceContinuation properties

References

Closes #4963

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 4, 2026

Semver Impact of This PR

None (no version bump detected)

📋 Changelog Preview

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


Breaking Changes 🛠

  • The _Metrics_ APIs are now stable: removed Experimental from SentrySdk, SentryOptions and IHub by Flash0ver in #5023

Features ✨

  • Report a new _Diagnostic_ (SENTRY1001) when a Metrics-API is invoked with an unsupported numeric type by Flash0ver in #4840
  • feat: Implement strict trace continuation by giortzisg in #4981

Fixes 🐛

  • fix: Workaround FileNotFoundException on Android when recovering sessions by jamescrosswell in #5084
  • fix: CaptureFeedback now supports multiple attachments correctly by bitsandfoxes in #5077
  • fix: include Data set via ITransactionTracer in SentryTransaction by Flash0ver in #4148
  • fix: CaptureFeedback now applies event processors by jamescrosswell in #4942

Dependencies ⬆️

Deps

  • chore(deps): update Native SDK to v0.13.6 by github-actions in #5128
  • chore(deps): update Native SDK to v0.13.5 by github-actions in #5119
  • chore(deps): update CLI to v3.3.5 by github-actions in #5093
  • chore(deps): update Native SDK to v0.13.4 by github-actions in #5081
  • chore(deps): update Java SDK to v8.37.1 by github-actions in #5071
  • chore(deps): update CLI to v3.3.4 by github-actions in #5068
  • chore(deps): update Java SDK to v8.37.0 by github-actions in #5069
  • chore(deps): update Cocoa SDK to v9.8.0 by github-actions in #5044
  • chore(deps): update Java SDK to v8.36.0 by github-actions in #5036
  • chore(deps): update epitaph to 0.1.1 by github-actions in #5036
  • chore(deps): update Native SDK to v0.13.3 by github-actions in #5045
  • chore(deps): update Cocoa SDK to v9.7.0 by github-actions in #5015
  • chore(deps): update Java SDK to v8.35.0 by github-actions in #5017
  • chore(deps): replaced the heavy protobuf-javalite 3.25.8 dependency with a light-weight epitaph 0.1.0 alternative on Android (getsentry/sentry-java#5157) by github-actions in #5017
  • chore(deps): update CLI to v3.3.3 by github-actions in #5002
  • chore(deps): update Cocoa SDK to v9.6.0 by github-actions in #4958

Other

  • release: 6.3.1 by jamescrosswell in 667437e5
  • chore: Update validate-pr workflow by stephanie-anderson in #5108
  • ci: fix workflows that always fail for fork PRs by jamescrosswell in #5065
  • docs: Update CHANGELOG by Flash0ver in 4a79de0a
  • release: 6.3.0 by Flash0ver in 818b7911
  • ref: Use .NET 6.0 ArgumentNullException throw helpers by copilot-swe-agent in #4985

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 4, 2026

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against b178f46

giortzisg added a commit that referenced this pull request Mar 4, 2026
…ion to CreateFromHeaders

- Add CHANGELOG.md entry for strict trace continuation feature (#4981)
- Fix CS8632 build error by adding #nullable enable before test methods
  using string? parameters in HubTests.cs
- Add org ID mismatch validation directly in
  SentryPropagationContext.CreateFromHeaders so it starts a new trace
  when SDK and baggage org IDs don't match
- Pass effective org ID from Hub.ContinueTrace to CreateFromHeaders
- Add CreateFromHeaders_WithOrgMismatch_StartsNewTrace and
  CreateFromHeaders_WithOrgMatch_ContinuesTrace tests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Flash0ver Flash0ver self-assigned this Mar 17, 2026
@jamescrosswell
Copy link
Copy Markdown
Collaborator

@giortzisg I've added some context to AGENTS.md in #5035, which should give the agent enough context to be able to correct this PR (once that's been merged into main/this PR).

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 96.15385% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.14%. Comparing base (1d43862) to head (ebb0957).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/Sentry/SentryOptions.cs 71.42% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4981      +/-   ##
==========================================
+ Coverage   74.08%   74.14%   +0.05%     
==========================================
  Files         506      506              
  Lines       18247    18292      +45     
  Branches     3564     3576      +12     
==========================================
+ Hits        13519    13562      +43     
- Misses       3858     3859       +1     
- Partials      870      871       +1     

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

@jamescrosswell jamescrosswell marked this pull request as ready for review March 19, 2026 21:41
@jamescrosswell
Copy link
Copy Markdown
Collaborator

@giortzisg / @Flash0ver this looks good to me. OK to merge?

Comment thread src/Sentry/SentryPropagationContext.cs Outdated
@jamescrosswell jamescrosswell self-requested a review March 30, 2026 23:45
Comment thread src/Sentry/DynamicSamplingContext.cs
Copy link
Copy Markdown

@cursor cursor Bot left a comment

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 c861e08. Configure here.

Comment thread src/Sentry/DynamicSamplingContext.cs
giortzisg and others added 5 commits May 7, 2026 17:00
Add org ID validation to distributed trace continuation to prevent
traces from being continued across different Sentry organizations.

- Parse org ID from DSN host (e.g., o1.ingest.us.sentry.io -> "1")
- Add OrgId option to SentryOptions to override DSN-parsed value
- Add StrictTraceContinuation bool option (default false)
- Propagate sentry-org_id in outgoing baggage via DynamicSamplingContext
- Validate org IDs in Hub.ContinueTrace:
  - Mismatched org IDs always start new trace (regardless of setting)
  - Missing incoming org_id + strict=true -> start new trace
  - Missing incoming org_id + strict=false -> continue trace (default)
- Add comprehensive tests for all org ID validation scenarios
- Update API approval snapshots

Closes #4963

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ion to CreateFromHeaders

- Add CHANGELOG.md entry for strict trace continuation feature (#4981)
- Fix CS8632 build error by adding #nullable enable before test methods
  using string? parameters in HubTests.cs
- Add org ID mismatch validation directly in
  SentryPropagationContext.CreateFromHeaders so it starts a new trace
  when SDK and baggage org IDs don't match
- Pass effective org ID from Hub.ContinueTrace to CreateFromHeaders
- Add CreateFromHeaders_WithOrgMismatch_StartsNewTrace and
  CreateFromHeaders_WithOrgMatch_ContinuesTrace tests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…tests

Hub.ShouldContinueTrace already nulls both headers before calling
CreateFromHeaders, making the org-ID guard inside CreateFromHeaders
unreachable from all current call paths. Remove the duplicate check and
its sdkOrgId parameter to eliminate the inconsistency and maintenance risk.

Add ValidDsnWithOrgId DSN sample and six new DynamicSamplingContextTests
covering that org_id is included in outgoing baggage for all three factory
methods (CreateFromTransaction, CreateFromUnsampledTransaction,
CreateFromPropagationContext) when the DSN has an o<num> host prefix,
and excluded when it does not.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
CreateFromExternalPropagationContext (the OTel path) was missing
orgId: options.GetEffectiveOrgId(), which would cause downstream services
with StrictTraceContinuation to start new traces unnecessarily.

Add two DynamicSamplingContextTests covering that org_id is included
when the DSN has an o<num> host prefix and excluded when it does not.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jamescrosswell jamescrosswell force-pushed the feat/strict-trace-continuation-v2 branch from c861e08 to ce0127d Compare May 7, 2026 05:01
Comment thread CHANGELOG.md Outdated
@jamescrosswell jamescrosswell merged commit 54802a9 into main May 7, 2026
27 checks passed
@jamescrosswell jamescrosswell deleted the feat/strict-trace-continuation-v2 branch May 7, 2026 07:50
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.

Implement strict trace continuation (org_id validation)

3 participants