Passed
Push — develop ( cefedc...7e0ea6 )
by Stan
07:15
created

Histogram::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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