summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Metrics/Exemplar/FixedSizeReservoir.php
blob: 479292a4c1b4d8184194769be11382d8a2ee5798 (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
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Metrics\Exemplar;

use OpenTelemetry\Context\ContextInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use function random_int;

final class FixedSizeReservoir implements ExemplarReservoirInterface
{
    private BucketStorage $storage;
    private int $size;
    private int $measurements = 0;

    public function __construct(int $size = 4)
    {
        $this->storage = new BucketStorage($size);
        $this->size = $size;
    }

    public function offer($index, $value, AttributesInterface $attributes, ContextInterface $context, int $timestamp): void
    {
        $bucket = random_int(0, $this->measurements);
        $this->measurements++;
        if ($bucket < $this->size) {
            $this->storage->store($bucket, $index, $value, $attributes, $context, $timestamp);
        }
    }

    public function collect(array $dataPointAttributes): array
    {
        $this->measurements = 0;

        return $this->storage->collect($dataPointAttributes);
    }
}