-
-
Notifications
You must be signed in to change notification settings - Fork 466
Refactor trace header parsing logic #1876
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\Tracing\Traits; | ||
|
|
||
| use Sentry\Tracing\DynamicSamplingContext; | ||
| use Sentry\Tracing\SpanId; | ||
| use Sentry\Tracing\TraceId; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| trait TraceHeaderParserTrait | ||
| { | ||
| /** | ||
| * @var string The regex for parsing the sentry-trace header | ||
| */ | ||
| private static $sentryTraceparentHeaderRegex = '/^[ \\t]*(?<trace_id>[0-9a-f]{32})?-?(?<span_id>[0-9a-f]{16})?-?(?<sampled>[01])?[ \\t]*$/i'; | ||
|
|
||
| /** | ||
| * Parses the sentry-trace and baggage headers and returns the extracted data. | ||
| * | ||
| * @param string $sentryTrace The sentry-trace header value | ||
| * @param string $baggage The baggage header value | ||
| * | ||
| * @return array{ | ||
| * traceId: TraceId|null, | ||
| * parentSpanId: SpanId|null, | ||
| * parentSampled: bool|null, | ||
| * dynamicSamplingContext: DynamicSamplingContext|null, | ||
| * sampleRand: float|null, | ||
| * parentSamplingRate: float|null | ||
| * } | ||
| */ | ||
| protected static function parseTraceAndBaggageHeaders(string $sentryTrace, string $baggage): array | ||
| { | ||
| $result = [ | ||
| 'traceId' => null, | ||
| 'parentSpanId' => null, | ||
| 'parentSampled' => null, | ||
| 'dynamicSamplingContext' => null, | ||
| 'sampleRand' => null, | ||
| 'parentSamplingRate' => null, | ||
| ]; | ||
|
|
||
| $hasSentryTrace = false; | ||
|
|
||
| if (preg_match(self::$sentryTraceparentHeaderRegex, $sentryTrace, $matches)) { | ||
| if (!empty($matches['trace_id'])) { | ||
| $result['traceId'] = new TraceId($matches['trace_id']); | ||
| $hasSentryTrace = true; | ||
| } | ||
|
|
||
| if (!empty($matches['span_id'])) { | ||
| $result['parentSpanId'] = new SpanId($matches['span_id']); | ||
| $hasSentryTrace = true; | ||
| } | ||
|
|
||
| if (isset($matches['sampled'])) { | ||
| $result['parentSampled'] = $matches['sampled'] === '1'; | ||
| $hasSentryTrace = true; | ||
| } | ||
| } | ||
|
|
||
| $samplingContext = DynamicSamplingContext::fromHeader($baggage); | ||
|
|
||
| if ($hasSentryTrace && !$samplingContext->hasEntries()) { | ||
| // 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. | ||
| $samplingContext->freeze(); | ||
| $result['dynamicSamplingContext'] = $samplingContext; | ||
| } | ||
|
|
||
| if ($hasSentryTrace && $samplingContext->hasEntries()) { | ||
| // The baggage header contains Dynamic Sampling Context data from an upstream SDK. | ||
| // Propagate this Dynamic Sampling Context. | ||
| $result['dynamicSamplingContext'] = $samplingContext; | ||
| } | ||
|
|
||
| // Store the propagated traces sample rate | ||
| if ($samplingContext->has('sample_rate')) { | ||
| $result['parentSamplingRate'] = (float) $samplingContext->get('sample_rate'); | ||
| } | ||
|
|
||
| // Store the propagated trace sample rand or generate a new one | ||
| if ($samplingContext->has('sample_rand')) { | ||
| $result['sampleRand'] = (float) $samplingContext->get('sample_rand'); | ||
| } else { | ||
| if ($samplingContext->has('sample_rate') && $result['parentSampled'] !== null) { | ||
| if ($result['parentSampled'] === true) { | ||
| // [0, rate) | ||
| $result['sampleRand'] = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (float) $samplingContext->get('sample_rate'), 6); | ||
| } else { | ||
| // [rate, 1) | ||
| $result['sampleRand'] = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (1 - (float) $samplingContext->get('sample_rate')) + (float) $samplingContext->get('sample_rate'), 6); | ||
| } | ||
| } elseif ($result['parentSampled'] !== null) { | ||
| // [0, 1) | ||
| $result['sampleRand'] = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 6); | ||
| } | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Didn't find an elegant way to fix this