@@ -29,15 +29,20 @@ final class PropagationContext
2929 private $ parentSpanId ;
3030
3131 /**
32- * @var DynamicSamplingContext |null The dynamic sampling context
32+ * @var bool |null The parent's sampling decision
3333 */
34- private $ dynamicSamplingContext ;
34+ private $ parentSampled ;
3535
3636 /**
3737 * @var float|null
3838 */
3939 private $ sampleRand ;
4040
41+ /**
42+ * @var DynamicSamplingContext|null The dynamic sampling context
43+ */
44+ private $ dynamicSamplingContext ;
45+
4146 private function __construct ()
4247 {
4348 }
@@ -49,10 +54,10 @@ public static function fromDefaults(): self
4954 $ context ->traceId = TraceId::generate ();
5055 $ context ->spanId = SpanId::generate ();
5156 $ context ->parentSpanId = null ;
52- $ context ->dynamicSamplingContext = null ;
53-
57+ $ context ->parentSampled = null ;
5458 // TODO check if this is precise enough
5559 $ context ->sampleRand = round (mt_rand (0 , mt_getrandmax () - 1 ) / mt_getrandmax (), 6 );
60+ $ context ->dynamicSamplingContext = null ;
5661
5762 return $ context ;
5863 }
@@ -167,6 +172,18 @@ public function setDynamicSamplingContext(DynamicSamplingContext $dynamicSamplin
167172 return $ this ;
168173 }
169174
175+ public function getSampleRand (): ?float
176+ {
177+ return $ this ->sampleRand ;
178+ }
179+
180+ public function setSampleRand (?float $ sampleRand ): self
181+ {
182+ $ this ->sampleRand = $ sampleRand ;
183+
184+ return $ this ;
185+ }
186+
170187 // TODO add same logic as in TransactionContext
171188 private static function parseTraceparentAndBaggage (string $ traceparent , string $ baggage ): self
172189 {
@@ -183,6 +200,11 @@ private static function parseTraceparentAndBaggage(string $traceparent, string $
183200 $ context ->parentSpanId = new SpanId ($ matches ['span_id ' ]);
184201 $ hasSentryTrace = true ;
185202 }
203+
204+ if (isset ($ matches ['sampled ' ])) {
205+ $ context ->parentSampled = $ matches ['sampled ' ] === '1 ' ;
206+ $ hasSentryTrace = true ;
207+ }
186208 } elseif (preg_match (self ::W3C_TRACEPARENT_HEADER_REGEX , $ traceparent , $ matches )) {
187209 if (!empty ($ matches ['trace_id ' ])) {
188210 $ context ->traceId = new TraceId ($ matches ['trace_id ' ]);
@@ -193,6 +215,11 @@ private static function parseTraceparentAndBaggage(string $traceparent, string $
193215 $ context ->parentSpanId = new SpanId ($ matches ['span_id ' ]);
194216 $ hasSentryTrace = true ;
195217 }
218+
219+ if (isset ($ matches ['sampled ' ])) {
220+ $ context ->parentSampled = $ matches ['sampled ' ] === '01 ' ;
221+ $ hasSentryTrace = true ;
222+ }
196223 }
197224
198225 $ samplingContext = DynamicSamplingContext::fromHeader ($ baggage );
@@ -210,6 +237,25 @@ private static function parseTraceparentAndBaggage(string $traceparent, string $
210237 $ context ->dynamicSamplingContext = $ samplingContext ;
211238 }
212239
240+ // Store the propagated trace sample rand or generate a new one
241+ if ($ samplingContext ->has ('sample_rand ' )) {
242+ // TODO check for 1e13 etc.
243+ $ context ->sampleRand = (float ) $ samplingContext ->get ('sample_rand ' );
244+ } else {
245+ if ($ samplingContext ->has ('sample_rate ' ) && $ context ->parentSampled !== null ) {
246+ if ($ context ->parentSampled === true ) {
247+ // [0, rate)
248+ $ context ->sampleRand = round (mt_rand (0 , mt_getrandmax () - 1 ) / mt_getrandmax () * (float ) $ samplingContext ->get ('sample_rate ' ), 6 );
249+ } else {
250+ // [rate, 1]
251+ $ context ->sampleRand = round (mt_rand (0 , mt_getrandmax () - 1 ) / mt_getrandmax () * (1 - (float ) $ samplingContext ->get ('sample_rate ' )) + (float ) $ samplingContext ->get ('sample-rate ' ), 6 );
252+ }
253+ } elseif ($ context ->parentSampled !== null ) {
254+ // [0, 1)
255+ $ context ->sampleRand = round (mt_rand (0 , mt_getrandmax () - 1 ) / mt_getrandmax (), 6 );
256+ }
257+ }
258+
213259 return $ context ;
214260 }
215261}
0 commit comments