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

Propagate Sampling Seed #3951

Merged
merged 14 commits into from
Feb 19, 2025
Merged

Propagate Sampling Seed #3951

merged 14 commits into from
Feb 19, 2025

Conversation

jamescrosswell
Copy link
Collaborator

@jamescrosswell jamescrosswell commented Feb 10, 2025

Resolves #3921

Implementation in other SDKs:

Progress

  • Implement hash algorithm to convert Trace ID to a seed that can be used with .NET's Random class
  • Ensure sentry-sample_rand is read from the DSC and PropagationContext (TwP)
  • Generate sample_rand when none is provided in BaggageHeader
  • Generate/store sample_rand when creating new Transactions
  • Use sample_rand in sampling decision
  • Ensure sentry-sample_rand gets propagated in trace headers

@jamescrosswell jamescrosswell marked this pull request as ready for review February 12, 2025 10:26
Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

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

only got as far as 2 files

@@ -61,6 +68,11 @@ private DynamicSamplingContext(
items.Add("sample_rate", sampleRate.Value.ToString(CultureInfo.InvariantCulture));
}

if (sampleRand is not null)
{
items.Add("sample_rand", sampleRand.Value.ToString("N4", CultureInfo.InvariantCulture));
Copy link
Member

Choose a reason for hiding this comment

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

we want this even if we have sample_rate in? see line 68

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe so yes. The docs developer docs indicate:

If sample_rate (inside baggage) and the sampling decision (trailing -1 or -0 from the sentry-trace header) are present in the incoming trace, create sample_rand so that when compared to the incoming sample_rate it would lead to the same sampling decision that is in the sentry-trace header.

Which we're doing... and then we propagate the sample_rand so that if any downstream nodes can use this in their sampling decision.

@@ -111,6 +123,27 @@ private DynamicSamplingContext(
return null;
}

// See https://develop.sentry.dev/sdk/telemetry/traces/#propagated-random-value
if (items.TryGetValue("sample_rand", out var sampleRand))
Copy link
Member

Choose a reason for hiding this comment

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

odd that we validate this to be within a range as a float but then we make it a string then try to parse it back out.
Can we rely on the original value and avoid the parsing instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure I follow. It may or may not be present as a string in the baggage header. If it's present then here we try to cast it to a float and ensure it's in the correct range.

If it's present but malformed then we don't create the DSC from the baggage header.

if (items.TryGetValue("sample_rand", out var sampleRand))
{
if (!double.TryParse(sampleRand, NumberStyles.Float, CultureInfo.InvariantCulture, out var rand) ||
rand is < 0.0 or >= 1.0)
Copy link
Member

Choose a reason for hiding this comment

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

didn't we validate this above before adding it to items? or it could already be in items when the method was caled? in which case wouldn't we be adding it twice? (I add Add so assume it's not a dictionary)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This method is creating a DSC from the baggage header. The BaggageHeader is a pretty simple class that just pulls stuff directly off a certain HTTP header, without any strong opinions about what the items in that header should be. Other vendors might be putting things on that header so we just propagate it, pretty much verbatim (we might add things to it but we never remove things from it).

But we can't control what will be on that baggage header in this SDK. A request could be made to our SDK with a malformed sentry-sample_rand item in the BaggageHeader.

@bruno-garcia
Copy link
Member

added @cleptric and @buenaflor to check alignment with the other SDKs

@jamescrosswell jamescrosswell merged commit e0ed84c into main Feb 19, 2025
22 checks passed
@jamescrosswell jamescrosswell deleted the sample-seed-propagation branch February 19, 2025 22:03
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 Sampling Seed Propagation
3 participants