Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(sdks): Propagated Sampling Rates & Strict Trace Propagation #11912

Merged
merged 12 commits into from
Nov 28, 2024

Conversation

cleptric
Copy link
Member

No description provided.

Copy link

vercel bot commented Nov 22, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
develop-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 28, 2024 10:50am
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
changelog ⬜️ Ignored (Inspect) Visit Preview Nov 28, 2024 10:50am
sentry-docs ⬜️ Ignored (Inspect) Visit Preview Nov 28, 2024 10:50am


### `tracesSampler`

This should be a callback, called when a transaction is started, which will be given a `samplingContext` object and which should return a sample rate between `0.0` and `1.0` _for the transaction in question_. This sample rate should behave the same way as the `tracesSampleRate` above, with the difference that it only applies to the newly-created transaction, such that different transactions can be sampled at different rates. Returning `0.0` should force the transaction to be dropped (set to `sampled = false`) and returning `1.0` should force the transaction to be sent (set `sampled = true`).

Optionally, the `tracesSampler` callback can also return a boolean to force a sampling decision (with `false` equivalent to `0.0` and `true` equivalent to `1.0`). If returning two different datatypes isn't an option in the implementing language, this possibility can safely be omitted.
Historically, the `tracesSampler` callback could have also returned a boolean to force a sampling decision (with `false` equivalent to `0.0` and `true` equivalent to `1.0`). This behavior is now **deprecated** and should be removed from all SDKs.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why deprecate this and not just keep treating true as 1.0 and false as 0.0?

Copy link
Member

Choose a reason for hiding this comment

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

What would be the benefit of doing so?

On first glance, I see a couple of downsides in having booleans in the API:

  1. It creates multiple overloads in the function signature or a polymorphic/mixed return type
  2. The semantics of 1.0 (100%) for extrapolation are inherently clear, but for return True not so much. My expectation would be that we "inherit" the parent extrapolation factor, but in reality we apply a hidden 100% sample rate.
  3. It's multiple ways to do the same thing.

@cleptric cleptric self-assigned this Nov 25, 2024
@cleptric cleptric changed the title Propagated Sampling Rates docs(sdks): Propagated Sampling Rates & Strict Trace Propagation Nov 25, 2024
@cleptric cleptric mentioned this pull request Nov 25, 2024
Co-authored-by: Ivana Kellyer <ivana.kellyer@sentry.io>
Co-authored-by: Anton Pirker <anton.pirker@sentry.io>
Co-authored-by: Anton Pirker <anton.pirker@sentry.io>
@cleptric cleptric marked this pull request as ready for review November 26, 2024 18:46
Copy link
Contributor

@lizokm lizokm left a comment

Choose a reason for hiding this comment

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

Added some language suggestions :)

Co-authored-by: Liza Mock <liza.mock@sentry.io>
Comment on lines 235 to 246
1. When an SDK starts a new trace, `sample_rand` is always set to a random number in range `[0, 1]`. This explicitly includes traces that are not sampled, as well as when the `tracesSampleRate` is set to `0.0` or `1.0`.
2. It is _recommended_ to generate the random number deterministically using the trace ID as seed or source of randomness. The exact method by which the random number is created is implementation defined and may vary between SDK implementations. See 4. on why this behaviour is desirable.
3. On incoming traces, an SDK assumes the `sample_rand` value along with the rest of the DSC, overriding an existing value if needed.
4. If `sample_rand` is missing on an incoming trace, the SDK creates and from now on propagates a new random number on-the-fly, based on the following rules:
1. If `sample_rate` and `sampled` are propgated, create `sample_rand` so that it adheres to the invariant. This means, for a decision of `True` generate a random number in half-open range `[0, rate)` and for a decision of `False` generate a random number in range `[rate, 1]`.
2. If the `sampled` is missing `sample_rate`, generate a random number in the range `[0, 1]` like for a new trace.

The SDK should exclusively use the stored random number and no longer use `math.random()` or similar anywhere else in the tracing related code base:

1. When the `tracesSampler` is invoked, this also applies to the return value of traces sampler. That is, `trace["sentry-sample_rand"] < tracesSampler(context)`
2. Otherwise, when the SDK is the head of a trace, this also applies to sample decisions based on `tracesSampleRate`. That is, `trace["sentry-sample_rand"] < config.tracesSampleRate`
3. There is no more direct comparison with `math.random()` during the sampling process.
Copy link
Member

Choose a reason for hiding this comment

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

I would really appreciate adding some examples, like we have above for strictTracePropagation, to see these rules in action. It would help me understand better how that actually works.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure!

@cleptric cleptric enabled auto-merge (squash) November 28, 2024 10:46
@cleptric cleptric merged commit 399f529 into master Nov 28, 2024
10 checks passed
@cleptric cleptric deleted the sampling branch November 28, 2024 10:50
@github-actions github-actions bot locked and limited conversation to collaborators Dec 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants