|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\Tracing\Traits; |
| 6 | + |
| 7 | +use Sentry\Tracing\DynamicSamplingContext; |
| 8 | +use Sentry\Tracing\SpanId; |
| 9 | +use Sentry\Tracing\TraceId; |
| 10 | + |
| 11 | +/** |
| 12 | + * @internal |
| 13 | + */ |
| 14 | +trait TraceHeaderParserTrait |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var string The regex for parsing the sentry-trace header |
| 18 | + */ |
| 19 | + private static $sentryTraceparentHeaderRegex = '/^[ \\t]*(?<trace_id>[0-9a-f]{32})?-?(?<span_id>[0-9a-f]{16})?-?(?<sampled>[01])?[ \\t]*$/i'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Parses the sentry-trace and baggage headers and returns the extracted data. |
| 23 | + * |
| 24 | + * @param string $sentryTrace The sentry-trace header value |
| 25 | + * @param string $baggage The baggage header value |
| 26 | + * |
| 27 | + * @return array{ |
| 28 | + * traceId: TraceId|null, |
| 29 | + * parentSpanId: SpanId|null, |
| 30 | + * parentSampled: bool|null, |
| 31 | + * dynamicSamplingContext: DynamicSamplingContext|null, |
| 32 | + * sampleRand: float|null, |
| 33 | + * parentSamplingRate: float|null |
| 34 | + * } |
| 35 | + */ |
| 36 | + protected static function parseTraceAndBaggageHeaders(string $sentryTrace, string $baggage): array |
| 37 | + { |
| 38 | + $result = [ |
| 39 | + 'traceId' => null, |
| 40 | + 'parentSpanId' => null, |
| 41 | + 'parentSampled' => null, |
| 42 | + 'dynamicSamplingContext' => null, |
| 43 | + 'sampleRand' => null, |
| 44 | + 'parentSamplingRate' => null, |
| 45 | + ]; |
| 46 | + |
| 47 | + $hasSentryTrace = false; |
| 48 | + |
| 49 | + if (preg_match(self::$sentryTraceparentHeaderRegex, $sentryTrace, $matches)) { |
| 50 | + if (!empty($matches['trace_id'])) { |
| 51 | + $result['traceId'] = new TraceId($matches['trace_id']); |
| 52 | + $hasSentryTrace = true; |
| 53 | + } |
| 54 | + |
| 55 | + if (!empty($matches['span_id'])) { |
| 56 | + $result['parentSpanId'] = new SpanId($matches['span_id']); |
| 57 | + $hasSentryTrace = true; |
| 58 | + } |
| 59 | + |
| 60 | + if (isset($matches['sampled'])) { |
| 61 | + $result['parentSampled'] = $matches['sampled'] === '1'; |
| 62 | + $hasSentryTrace = true; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + $samplingContext = DynamicSamplingContext::fromHeader($baggage); |
| 67 | + |
| 68 | + if ($hasSentryTrace && !$samplingContext->hasEntries()) { |
| 69 | + // The request comes from an old SDK which does not support Dynamic Sampling. |
| 70 | + // Propagate the Dynamic Sampling Context as is, but frozen, even without sentry-* entries. |
| 71 | + $samplingContext->freeze(); |
| 72 | + $result['dynamicSamplingContext'] = $samplingContext; |
| 73 | + } |
| 74 | + |
| 75 | + if ($hasSentryTrace && $samplingContext->hasEntries()) { |
| 76 | + // The baggage header contains Dynamic Sampling Context data from an upstream SDK. |
| 77 | + // Propagate this Dynamic Sampling Context. |
| 78 | + $result['dynamicSamplingContext'] = $samplingContext; |
| 79 | + } |
| 80 | + |
| 81 | + // Store the propagated traces sample rate |
| 82 | + if ($samplingContext->has('sample_rate')) { |
| 83 | + $result['parentSamplingRate'] = (float) $samplingContext->get('sample_rate'); |
| 84 | + } |
| 85 | + |
| 86 | + // Store the propagated trace sample rand or generate a new one |
| 87 | + if ($samplingContext->has('sample_rand')) { |
| 88 | + $result['sampleRand'] = (float) $samplingContext->get('sample_rand'); |
| 89 | + } else { |
| 90 | + if ($samplingContext->has('sample_rate') && $result['parentSampled'] !== null) { |
| 91 | + if ($result['parentSampled'] === true) { |
| 92 | + // [0, rate) |
| 93 | + $result['sampleRand'] = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (float) $samplingContext->get('sample_rate'), 6); |
| 94 | + } else { |
| 95 | + // [rate, 1) |
| 96 | + $result['sampleRand'] = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (1 - (float) $samplingContext->get('sample_rate')) + (float) $samplingContext->get('sample_rate'), 6); |
| 97 | + } |
| 98 | + } elseif ($result['parentSampled'] !== null) { |
| 99 | + // [0, 1) |
| 100 | + $result['sampleRand'] = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 6); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return $result; |
| 105 | + } |
| 106 | +} |
0 commit comments