X Tutup
Skip to content

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

Open
sentrivana wants to merge 1 commit intoivana/span-first-13-testsfrom
ivana/span-first-14-custom-sampling-context
Open

ref: Add support for custom sampling context to span first#5628
sentrivana wants to merge 1 commit intoivana/span-first-13-testsfrom
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 📊

5 passed | Total: 5 | Pass Rate: 100% | Execution Time: 1.08s

All tests are passing successfully.

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

Files with missing lines (2)
File Patch % Lines
tracing_utils.py 23.62% ⚠️ 511 Missing and 16 partials
scope.py 55.98% ⚠️ 372 Missing and 71 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.

}

if propagation_context.custom_sampling_context:
sampling_context.update(propagation_context.custom_sampling_context)
Copy link

Choose a reason for hiding this comment

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

Custom sampling context can silently override built-in keys

Medium Severity

Using sampling_context.update(propagation_context.custom_sampling_context) allows user-provided custom sampling context to silently override built-in keys like name, trace_id, parent_span_id, parent_sampled, and attributes. Unlike the old start_transaction path where the built-in keys were transaction_context and parent_sampled (uncommon names), keys like name and attributes are very common and likely to be used innocuously by someone calling scope.set_custom_sampling_context({"name": "my_feature"}), resulting in a corrupted sampling context reaching the traces_sampler.

Fix in Cursor Fix in Web

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?

...


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?

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.

2 participants

X Tutup