Skip to content

ref: Add support for custom sampling context to span first (14)#5628

Merged
sentrivana merged 96 commits intomasterfrom
ivana/span-first-14-custom-sampling-context
Mar 11, 2026
Merged

ref: Add support for custom sampling context to span first (14)#5628
sentrivana merged 96 commits intomasterfrom
ivana/span-first-14-custom-sampling-context

Conversation

@sentrivana
Copy link
Contributor

@sentrivana sentrivana commented Mar 10, 2026

Custom sampling context allows folks to have arbitrary data accessible in the traces_sampler in order to make a sampling decision. The SDK sets custom sampling context as well in some integrations, for example, in ASGI frameworks the ASGI scope will be available.

Previously, you could provide custom sampling context as an argument to the start_span function. In the spirit of keeping the new start_span API minimal, we'll be moving custom_sampling_context to the propagation context and providing a dedicated API function to set it. In this PR, it's a scope method (scope.set_custom_sampling_context()). We can (and probably should) promote it to top-level API at some point in the future.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 10, 2026

Codecov Results 📊

13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 7.82s

All tests are passing successfully.

❌ Patch coverage is 42.86%. Project has 14011 uncovered lines.

Files with missing lines (2)
File Patch % Lines
tracing_utils.py 40.96% ⚠️ 395 Missing and 28 partials
scope.py 68.88% ⚠️ 263 Missing and 66 partials

Generated by Codecov Action

@github-actions
Copy link
Contributor

github-actions bot commented Mar 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.

@sentrivana sentrivana marked this pull request as ready for review March 10, 2026 14:39
@sentrivana sentrivana requested a review from a team as a code owner March 10, 2026 14:39
Copy link

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

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

def _set_custom_sampling_context(
self, custom_sampling_context: "dict[str, Any]"
) -> None:
self.custom_sampling_context = custom_sampling_context
Copy link
Member

Choose a reason for hiding this comment

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

Related to the comment Cursor made - should we consider adding validation to the value that's going be set on the custom_sampling_context?

Copy link
Contributor Author

@sentrivana sentrivana Mar 11, 2026

Choose a reason for hiding this comment

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

Ideally you could set whatever you want on the custom sampling context, so I wouldn't validate. But mistakenly overriding existing keys is a concern. I'm thinking we could put the keys set by the SDK into their own subdictionary -- we're also doing this in the old code path with transaction_context. Just need to think of a nice new name since there are no transactions anymore in span first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe just span? Or span_context? Or just context? Naming is hard.

Copy link
Contributor Author

@sentrivana sentrivana Mar 11, 2026

Choose a reason for hiding this comment

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

Maybe span_context is actually the way to go -- very unique, so unlikely to collide with anything user-set, and similar to transaction_context so there's some continuity?

Went with that here

Copy link
Member

Choose a reason for hiding this comment

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

I agree, naming is hard 😅

I think span_context is a great name 👍🏻

...


def test_new_custom_sampling_context(sentry_init):
Copy link
Member

Choose a reason for hiding this comment

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

I think both of the test cases introduced here are good, but I think the names could be improved slightly to make it easier to understand (in terms of what broke) should they ever fail.

For this particular test, maybe something like test_custom_sampling_context_update_to_context_value_persists could work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sentrivana sentrivana changed the title ref: Add support for custom sampling context to span first ref: Add support for custom sampling context to span first (14) Mar 11, 2026
Base automatically changed from ivana/span-first-13-tests to master March 11, 2026 09:32
@sentrivana sentrivana enabled auto-merge (squash) March 11, 2026 12:15
@sentrivana sentrivana merged commit f2dbab6 into master Mar 11, 2026
158 checks passed
@sentrivana sentrivana deleted the ivana/span-first-14-custom-sampling-context branch March 11, 2026 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants