Skip to content

Commit f211751

Browse files
committed
Added adaptive sampling
1 parent 03b59bb commit f211751

4 files changed

Lines changed: 56 additions & 8 deletions

File tree

src/Sampler/AdaptiveSampler.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Jaeger\Sampler;
5+
6+
class AdaptiveSampler implements SamplerInterface
7+
{
8+
private $rateLimit;
9+
10+
private $probabilistic;
11+
12+
public function __construct(SamplerInterface $rateLimit, SamplerInterface $probabilistic)
13+
{
14+
$this->rateLimit = $rateLimit;
15+
$this->probabilistic = $probabilistic;
16+
}
17+
18+
public function decide(int $tracerId, string $operationName, string $debugId): SamplerResult
19+
{
20+
$rateLimitResult = $this->rateLimit->decide($tracerId, $operationName, $debugId);
21+
if ($rateLimitResult->isSampled()) {
22+
return new SamplerResult(
23+
true,
24+
$rateLimitResult->getFlags(),
25+
array_merge([new SamplerTypeTag('adaptive'),], $rateLimitResult->getTags())
26+
);
27+
}
28+
29+
$probabilisticResult = $this->probabilistic->decide($tracerId, $operationName, $debugId);
30+
if ($probabilisticResult->isSampled()) {
31+
return new SamplerResult(
32+
true,
33+
$rateLimitResult->getFlags(),
34+
array_merge([new SamplerTypeTag('adaptive'),], $rateLimitResult->getTags())
35+
);
36+
}
37+
38+
return new SamplerResult(
39+
false,
40+
0,
41+
[
42+
new SamplerTypeTag('adaptive'),
43+
new SamplerDecisionTag(false),
44+
new SamplerFlagsTag(0x00),
45+
]
46+
);
47+
}
48+
}

src/Sampler/ConstSampler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class ConstSampler extends AbstractSampler
77
{
88
private $debugEnabled;
99

10-
public function __construct(bool $debugEnabled)
10+
public function __construct($debugEnabled)
1111
{
12-
$this->debugEnabled = $debugEnabled;
12+
$this->debugEnabled = (bool)$debugEnabled;
1313
}
1414

15-
public function doDecide(int $traceId, string $operationName): SamplerResult
15+
public function doDecide(int $tracerId, string $operationName): SamplerResult
1616
{
1717
if (false === $this->debugEnabled) {
1818
return new SamplerResult(

src/Sampler/ProbabilisticSampler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public function __construct(float $rate)
1515
$this->threshold = 0.5 * $rate * PHP_INT_MAX;
1616
}
1717

18-
public function doDecide(int $traceId, string $operationName): SamplerResult
18+
public function doDecide(int $tracerId, string $operationName): SamplerResult
1919
{
20-
if (abs($traceId) > $this->threshold) {
20+
if (abs($tracerId) > $this->threshold) {
2121
return new SamplerResult(
2222
false,
2323
0x00,

src/Sampler/RateLimitingSampler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public function __construct(float $rate, GeneratorInterface $generator)
1515
$this->generator = $generator;
1616
}
1717

18-
public function doDecide(int $traceId, string $operationName): SamplerResult
18+
public function doDecide(int $tracerId, string $operationName): SamplerResult
1919
{
20-
$key = $this->generator->generate($traceId, $operationName);
20+
$key = $this->generator->generate($tracerId, $operationName);
2121
if (false !== ($current = apcu_add($key, sprintf('%s:%d', time(), 1), 1 / $this->rate))) {
2222
return new SamplerResult(
2323
true, 0x01, [
@@ -31,7 +31,7 @@ public function doDecide(int $traceId, string $operationName): SamplerResult
3131

3232
while (true) {
3333
if (false === ($current = apcu_fetch($key))) {
34-
return $this->doDecide($traceId, $operationName);
34+
return $this->doDecide($tracerId, $operationName);
3535
}
3636
list ($timestamp, $count) = explode(':', $current);
3737
if ($count / (time() - $timestamp) > $this->rate) {

0 commit comments

Comments
 (0)