summaryrefslogtreecommitdiff
path: root/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/Sampler/RateLimitingSampler.php
blob: d0ea0f1805a8fcd8a222eb3abc72d41ddbed8f03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php

namespace Jaeger\Sampler;

use Jaeger\Util\RateLimiter;

use const Jaeger\SAMPLER_PARAM_TAG_KEY;
use const Jaeger\SAMPLER_TYPE_RATE_LIMITING;
use const Jaeger\SAMPLER_TYPE_TAG_KEY;

class RateLimitingSampler implements SamplerInterface
{
    /**
     * @var RateLimiter
     */
    private $rateLimiter;

    /**
     * A list of the sampler tags.
     *
     * @var array
     */
    private $tags = [];

    public function __construct($maxTracesPerSecond, RateLimiter $rateLimiter)
    {
        $this->tags = [
            SAMPLER_TYPE_TAG_KEY => SAMPLER_TYPE_RATE_LIMITING,
            SAMPLER_PARAM_TAG_KEY => $maxTracesPerSecond,
        ];

        $maxTracesPerNanosecond = $maxTracesPerSecond / 1000000000.0;
        $this->rateLimiter = $rateLimiter;
        $this->rateLimiter->initialize($maxTracesPerNanosecond, $maxTracesPerSecond > 1.0 ? 1.0 : $maxTracesPerSecond);
    }

    /**
     * Whether or not the new trace should be sampled.
     *
     * Implementations should return an array in the format [$decision, $tags].
     *
     * @param string $traceId The traceId on the span.
     * @param string $operation The operation name set on the span.
     * @return array
     */
    public function isSampled(string $traceId = '', string $operation = '')
    {
        return [$this->rateLimiter->checkCredit(1.0), $this->tags];
    }

    /**
     * {@inheritdoc}
     *
     * Only implemented to satisfy the sampler interface.
     *
     * @return void
     */
    public function close()
    {
        // nothing to do
    }
}