|
| 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 | +} |
0 commit comments