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 random value #4153

Merged
merged 18 commits into from
Feb 12, 2025
Merged

Propagate sampling random value #4153

merged 18 commits into from
Feb 12, 2025

Conversation

adinauer
Copy link
Member

📜 Description

Propagate sampling random value to downstream systems (sentry-sample_rand in baggage) and Sentry (sample_rand in envelope header).

Use incoming sentry-sample_rand.

Backfill sampling random value in case it is missing.

💡 Motivation and Context

Closes #4115
Closes #4032

💚 How did you test it?

📝 Checklist

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Copy link
Contributor

github-actions bot commented Feb 10, 2025

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 85768d6

Copy link
Contributor

github-actions bot commented Feb 10, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 434.67 ms 500.98 ms 66.31 ms
Size 1.58 MiB 2.21 MiB 640.91 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
c2c78de 415.28 ms 505.08 ms 89.80 ms

App size

Revision Plain With Sentry Diff
c2c78de 1.58 MiB 2.21 MiB 640.27 KiB

Previous results on branch: feat/sample_rand

Startup times

Revision Plain With Sentry Diff
b5d63c8 384.39 ms 443.98 ms 59.59 ms
b674478 399.19 ms 482.67 ms 83.48 ms
b4d2996 319.66 ms 369.94 ms 50.28 ms
473d6ab 446.68 ms 464.90 ms 18.22 ms
7c29bb1 647.18 ms 767.43 ms 120.25 ms
4e41178 388.33 ms 531.18 ms 142.86 ms

App size

Revision Plain With Sentry Diff
b5d63c8 1.58 MiB 2.21 MiB 641.09 KiB
b674478 1.58 MiB 2.21 MiB 640.91 KiB
b4d2996 1.58 MiB 2.21 MiB 640.92 KiB
473d6ab 1.58 MiB 2.21 MiB 640.93 KiB
7c29bb1 1.58 MiB 2.21 MiB 640.92 KiB
4e41178 1.58 MiB 2.21 MiB 641.07 KiB

sentrySpan.toBaggageHeader(Collections.emptyList());
if (baggageHeader != null) {
setter.set(carrier, baggageHeader.getName(), baggageHeader.getValue());
// TODO can we use traceIfAllowed? do we have the URL here? need to access span attrs
Copy link
Member Author

Choose a reason for hiding this comment

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

Left this as a TODO for the future. We could start looking at span attributes to retrieve the URL of the downstream system.

@@ -125,11 +130,6 @@ public <C> Context extract(
.getLogger()
.log(SentryLevel.DEBUG, "Continuing Sentry trace %s", sentryTraceHeader.getTraceId());

final @NotNull PropagationContext propagationContext =
Copy link
Member Author

Choose a reason for hiding this comment

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

Since this causes baggage to be frozen immediately, we no longer want to do this in the propagator.

if (baggageHeader != null) {
setter.set(carrier, baggageHeader.getName(), baggageHeader.getValue());
// TODO can we use traceIfAllowed? do we have the URL here? need to access span attrs
final @Nullable TracingUtils.TracingHeaders tracingHeaders =
Copy link
Member Author

Choose a reason for hiding this comment

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

This ensures baggage is frozen when sending out headers. Not freezing was a bug.

@@ -87,6 +82,8 @@ public void onStart(final @NotNull Context parentContext, final @NotNull ReadWri
new PropagationContext(
new SentryId(traceId), sentrySpanId, sentryParentSpanId, baggage, sampled);

baggage = propagationContext.getBaggage();
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't want to send the wrong baggage into the OtelSpanWrapper ctor further down.

@@ -64,7 +64,6 @@ public final class OtelSpanWrapper implements IOtelSpanWrapper {
private final @NotNull Contexts contexts = new Contexts();
private @Nullable String transactionName;
private @Nullable TransactionNameSource transactionNameSource;
private final @Nullable Baggage baggage;
Copy link
Member Author

Choose a reason for hiding this comment

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

No need to keep this as a property here. We can always ask the context for it.

@@ -123,7 +128,12 @@ public static Baggage fromHeader(
thirdPartyKeyValueStrings.isEmpty()
? null
: StringUtils.join(",", thirdPartyKeyValueStrings);
return new Baggage(keyValues, thirdPartyHeader, mutable, logger);
/*
Copy link
Member Author

Choose a reason for hiding this comment

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

When creating Baggage from headers we now ignore the sample random value entry and instead of freezing right away we just mark the baggage for being frozen. This shouldFreeze flag is then checked when passing baggage into either PropagationContext or TransactionContext and frozen if true.

@@ -81,12 +80,6 @@ public SentryTracer(
this.transactionNameSource = context.getTransactionNameSource();
this.transactionOptions = transactionOptions;

if (context.getBaggage() != null) {
Copy link
Member Author

Choose a reason for hiding this comment

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

We're relying on TransactionContext to bring in baggage here. Instead of holding on directly, we're using context to access it.

@@ -90,6 +93,7 @@ public TransactionContext(
this.name = Objects.requireNonNull(name, "name is required");
this.transactionNameSource = transactionNameSource;
this.setSamplingDecision(samplingDecision);
this.baggage = TracingUtils.ensureBaggage(null, samplingDecision);
Copy link
Member Author

Choose a reason for hiding this comment

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

Takes care of creating a new Baggage if missing, backfilling sample random value and freezing baggage if marked for freezing.

We can't force customers to go through Sentry.continueTrace which would create a PropagationContext so we also have to handle this here. This is something we should improve when replacing the Performance API.

@@ -56,7 +60,7 @@ public static PropagationContext fromHeaders(

private @Nullable Boolean sampled;

private @Nullable Baggage baggage;
private final @NotNull Baggage baggage;
Copy link
Member Author

Choose a reason for hiding this comment

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

Changed this to non null, so we don't have to handle null everywhere we access this.

@@ -67,18 +71,10 @@ public PropagationContext(final @NotNull PropagationContext propagationContext)
propagationContext.getTraceId(),
propagationContext.getSpanId(),
propagationContext.getParentSpanId(),
cloneBaggage(propagationContext.getBaggage()),
Copy link
Member Author

Choose a reason for hiding this comment

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

We were cloning baggage here for each scope fork meaning a non frozen baggage would never freeze. This might no longer be a problem after having done a lot more changes now. However I don't see a reason to clone / separate any pre-existing baggage. If anything we should make sure a new one is created when isolation scope is forked.

@adinauer adinauer mentioned this pull request Feb 10, 2025
@adinauer adinauer merged commit dc85168 into main Feb 12, 2025
35 checks passed
@adinauer adinauer deleted the feat/sample_rand branch February 12, 2025 16:50
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.

sample_rand Implement Sampling Seed Propagation
4 participants