Skip to content

Commit b7d38b9

Browse files
committed
WIP
1 parent b86ff90 commit b7d38b9

File tree

4 files changed

+85
-13
lines changed

4 files changed

+85
-13
lines changed

src/State/Hub.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public function startTransaction(TransactionContext $context, array $customSampl
279279

280280
$sampleSource = 'config:traces_sampler';
281281
} else {
282-
$parentSampleRate = (float) $context->getMetadata()->getDynamicSamplingContext()->get('sample_rate');
282+
$parentSampleRate = $context->getMetadata()->getParentSamplingRate();
283283
if ($parentSampleRate !== null) {
284284
$sampleRate = $parentSampleRate;
285285
$sampleSource = 'parent';
@@ -300,7 +300,6 @@ public function startTransaction(TransactionContext $context, array $customSampl
300300
return $transaction;
301301
}
302302

303-
// TODO Why are we doing this?
304303
$transaction->getMetadata()->setSamplingRate($sampleRate);
305304

306305
if ($sampleRate === 0.0) {
@@ -311,7 +310,6 @@ public function startTransaction(TransactionContext $context, array $customSampl
311310
return $transaction;
312311
}
313312

314-
$sampleRand = $context->getMetadata()->getSampleRand();
315313
$transaction->setSampled($sampleRand < $sampleRate);
316314
}
317315

src/Tracing/PropagationContext.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Tracing/TransactionContext.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,23 @@ private static function parseTraceAndBaggage(string $sentryTrace, string $baggag
198198
$context->getMetadata()->setDynamicSamplingContext($samplingContext);
199199
}
200200

201+
// Store the propagated traces sample rate
202+
if ($samplingContext->has('sample_rate')) {
203+
$context->getMetadata()->setParentSamplingRate((float) $samplingContext->get('sample_rate'));
204+
}
205+
206+
// Store the propagated trace sample rand or generate a new one
201207
if ($samplingContext->has('sample_rand')) {
202208
// TODO check for 1e13 etc.
203209
$context->getMetadata()->setSampleRand((float) $samplingContext->get('sample_rand'));
204210
} else {
205-
if ($samplingContext->has('sample-rate') && $context->parentSampled !== null) {
211+
if ($samplingContext->has('sample_rate') && $context->parentSampled !== null) {
206212
if ($context->parentSampled === true) {
207213
// [0, rate)
208-
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * $samplingContext->get('sample-rate'), 6));
214+
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (float) $samplingContext->get('sample_rate'), 6));
209215
} else {
210216
// [rate, 1]
211-
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (1 - $samplingContext->get('sample-rate')) + $samplingContext->get('sample-rate'), 6));
217+
$context->getMetadata()->setSampleRand(round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() * (1 - (float) $samplingContext->get('sample_rate')) + (float) $samplingContext->get('sample-rate'), 6));
212218
}
213219
} elseif ($context->parentSampled !== null) {
214220
// [0, 1)

src/Tracing/TransactionMetadata.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ final class TransactionMetadata
2222
private $source;
2323

2424
/**
25-
* @var float|null
25+
* @var float|int|null
26+
*/
27+
private $parentSamplingRate;
28+
29+
/**
30+
* @var float|int|null
2631
*/
2732
private $sampleRand;
2833

@@ -32,18 +37,23 @@ final class TransactionMetadata
3237
* @param float|int|null $samplingRate The sampling rate
3338
* @param DynamicSamplingContext|null $dynamicSamplingContext The Dynamic Sampling Context
3439
* @param TransactionSource|null $source The transaction source
40+
* @param float|null $parentSamplingRate The parent sampling rate
41+
* @param float|null $sampleRand The trace sample rand
3542
*/
3643
public function __construct(
3744
$samplingRate = null,
3845
?DynamicSamplingContext $dynamicSamplingContext = null,
39-
?TransactionSource $source = null
46+
?TransactionSource $source = null,
47+
?float $parentSamplingRate = null,
48+
?float $sampleRand = null
4049
) {
4150
$this->samplingRate = $samplingRate;
4251
$this->dynamicSamplingContext = $dynamicSamplingContext;
4352
$this->source = $source ?? TransactionSource::custom();
53+
$this->parentSamplingRate = $parentSamplingRate;
4454

4555
// TODO check if this is precise enough
46-
$this->sampleRand = round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 6);
56+
$this->sampleRand = $sampleRand ?? round(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 6);
4757
}
4858

4959
/**
@@ -64,6 +74,18 @@ public function setSamplingRate($samplingRate): self
6474
return $this;
6575
}
6676

77+
public function getParentSamplingRate(): ?float
78+
{
79+
return $this->parentSamplingRate;
80+
}
81+
82+
public function setParentSamplingRate(?float $parentSamplingRate): self
83+
{
84+
$this->parentSamplingRate = $parentSamplingRate;
85+
86+
return $this;
87+
}
88+
6789
public function getSampleRand(): ?float
6890
{
6991
return $this->sampleRand;

0 commit comments

Comments
 (0)