Passed
Push — develop ( 8c6599...c9bdac )
by Stan
04:29
created

Histogram::track()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Krenor\Prometheus\Metrics;
4
5
use Tightenco\Collect\Support\Collection;
6
use Krenor\Prometheus\Exceptions\LabelException;
7
use Krenor\Prometheus\Contracts\Types\Observable;
8
use Krenor\Prometheus\Exceptions\PrometheusException;
9
use Krenor\Prometheus\Metrics\Concerns\TracksExecutionTime;
10
11
abstract class Histogram extends Metric implements Observable
12
{
13
    use TracksExecutionTime;
14
15
    /**
16
     * @var float[]
17
     */
18
    protected $buckets = [
19
        .005,
20
        .01,
21
        .025,
22
        .05,
23
        .1,
24
        .25,
25
        .5,
26
        1,
27
        2.5,
28
        5,
29
        10,
30
    ];
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 18
    public function __construct()
36
    {
37 18
        parent::__construct();
38
39 18
        foreach ($this->labels as $label) {
40 16
            if (preg_match('/^le$/', $label)) {
41 16
                throw new LabelException('The label `le` is used internally to designate buckets.');
42
            }
43
        }
44
45 17
        if (count($this->buckets) < 1) {
46 1
            throw new PrometheusException('Histograms must contain at least one bucket.');
47
        }
48
49 16
        sort($this->buckets);
50 16
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 5
    public function type(): string
56
    {
57 5
        return 'histogram';
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @return self
64
     */
65 5
    public function observe(float $value, array $labels = []): Observable
66
    {
67 5
        static::$storage->observe($this, $value, $labels);
68
69 5
        return $this;
70
    }
71
72
    /**
73
     * @return Collection
74
     */
75 13
    public function buckets(): Collection
76
    {
77 13
        return new Collection($this->buckets);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function track(float $value, array $labels = []): void
84
    {
85
        $this->observe($value, $labels);
86
    }
87
}
88