-
-
Notifications
You must be signed in to change notification settings - Fork 468
feat: add strict_trace_continuation support
#2016
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| namespace Sentry\Tracing\Traits; | ||
|
|
||
| use Sentry\SentrySdk; | ||
| use Sentry\Tracing\DynamicSamplingContext; | ||
| use Sentry\Tracing\SpanId; | ||
| use Sentry\Tracing\TraceId; | ||
|
|
@@ -65,6 +66,14 @@ protected static function parseTraceAndBaggageHeaders(string $sentryTrace, strin | |
|
|
||
| $samplingContext = DynamicSamplingContext::fromHeader($baggage); | ||
|
|
||
| if ($hasSentryTrace && !self::shouldContinueTrace($samplingContext)) { | ||
| $result['traceId'] = null; | ||
| $result['parentSpanId'] = null; | ||
| $result['parentSampled'] = null; | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| if ($hasSentryTrace) { | ||
| // The request comes from an old SDK which does not support Dynamic Sampling. | ||
| // Propagate the Dynamic Sampling Context as is, but frozen, even without sentry-* entries. | ||
|
|
@@ -102,4 +111,51 @@ protected static function parseTraceAndBaggageHeaders(string $sentryTrace, strin | |
|
|
||
| return $result; | ||
| } | ||
|
|
||
| private static function shouldContinueTrace(DynamicSamplingContext $samplingContext): bool | ||
| { | ||
| $hub = SentrySdk::getCurrentHub(); | ||
| $client = $hub->getClient(); | ||
|
|
||
| if ($client === null) { | ||
| return true; | ||
| } | ||
|
|
||
| $options = $client->getOptions(); | ||
| $clientOrgId = $options->getOrgId(); | ||
| if ($clientOrgId === null && $options->getDsn() !== null) { | ||
| $clientOrgId = $options->getDsn()->getOrgId(); | ||
| } | ||
|
Comment on lines
+125
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: The name here is a bit confusing if we get the orgID from the DSN. clientOrgID is the one on clientOptions and the other is the DSN one.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, the naming was more meant to represent something that comes from the client (DSN and options are both coming from the client) vs what comes from the baggage header |
||
|
|
||
| $baggageOrgId = $samplingContext->get('org_id'); | ||
| $logger = $options->getLoggerOrNullLogger(); | ||
|
|
||
| // both org IDs are set but are not equals | ||
| if ($clientOrgId !== null && $baggageOrgId !== null && ((string) $clientOrgId !== $baggageOrgId)) { | ||
| $logger->debug( | ||
| \sprintf( | ||
| "Starting a new trace because org IDs don't match (incoming baggage org_id: %s, SDK org_id: %s)", | ||
| $baggageOrgId, | ||
| $clientOrgId | ||
| ) | ||
| ); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| // One org ID is not set and strict trace continuation is enabled | ||
| if ($options->isStrictTraceContinuationEnabled() && ($clientOrgId === null) !== ($baggageOrgId === null)) { | ||
| $logger->debug( | ||
| \sprintf( | ||
| 'Starting a new trace because strict trace continuation is enabled and one org ID is missing (incoming baggage org_id: %s, SDK org_id: %s)', | ||
| $baggageOrgId !== null ? $baggageOrgId : 'none', | ||
| $clientOrgId !== null ? (string) $clientOrgId : 'none' | ||
| ) | ||
| ); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think that
hasSentryTraceis not-needed here, we already check on the next if statement and we don't use the trace anywhere.