Skip to content

Conversation

@Ten0
Copy link
Contributor

@Ten0 Ten0 commented Dec 4, 2025

Description

Similarly to sentry.trace, this allows overriding whether the transaction is sampled while still going through the tracing integration.

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.

Bug: `sentry.sample` field not removed in `on_record` method

The on_record method removes SENTRY_TRACE_FIELD to prevent it from being recorded as span data since it cannot be applied retroactively. However, the new SENTRY_SAMPLE_FIELD is not similarly removed. This causes sentry.sample to be incorrectly stored as span data when recorded retroactively via span.record, even though the value has no effect on sampling (which is only determined during on_new_span). This is inconsistent with how sentry.trace is handled and pollutes span data with useless fields.

sentry-tracing/src/layer.rs#L436-L437

// `sentry.trace` cannot be applied retroactively
data.json_values.remove(SENTRY_TRACE_FIELD);

Fix in Cursor Fix in Web


@codecov
Copy link

codecov bot commented Dec 4, 2025

Codecov Report

❌ Patch coverage is 84.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.80%. Comparing base (196501d) to head (4072dc5).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #945      +/-   ##
==========================================
- Coverage   73.81%   73.80%   -0.01%     
==========================================
  Files          64       64              
  Lines        7504     7517      +13     
==========================================
+ Hits         5539     5548       +9     
- Misses       1965     1969       +4     

@lcian
Copy link
Member

lcian commented Dec 5, 2025

Hi @Ten0, thank you for the PR.
Do you need tail-based sampling based on some attributes, or would using a span_filter work for you?

pub fn span_filter<F>(mut self, filter: F) -> Self

Generally, all of our other SDKs offer before_send_span/before_send_transaction hooks to centrally manage span filtering and mutation. So we should consider whether we want to introduce this, the hooks, or both.
There's also traces_sampler which is already available in this SDK, albeit less powerful (very similar to span_filter but it acts on whole traces as opposed to spans).

@Ten0
Copy link
Contributor Author

Ten0 commented Dec 5, 2025

Hello, yes I'm looking for specifying sampling not just based on the span itself, specifically I'm using another header than sentry trace to specify just "please sample this", without specifying a trace ID. That's useful e.g. when request is initialized via Insomnia.
I guess traces_sampler might work as well if I could efficiently read an attribute that I would dedicate to this...

@lcian
Copy link
Member

lcian commented Dec 5, 2025

@Ten0 thanks for the context.
I think TracesSampler wouldn't work, because you only have access to some limited information about the trace there like name and op but not attributes.

before_send_span/transaction however would work because that would happen, clearly, before sending, so all attributes would be available in that callback.
If we implemented before_send_span, you could even recreate the feature you're PRing here on your own, so that could be another point in favor of implementing one of these hooks.

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.

2 participants