Completed
Push — develop ( 28a887...d46380 )
by Stan
02:57
created

Histogram::observe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Krenor\Prometheus\Metrics;
4
5
use Krenor\Prometheus\Contracts\Metric;
6
use Tightenco\Collect\Support\Collection;
7
use Krenor\Prometheus\Contracts\SamplesBuilder;
8
use Krenor\Prometheus\Exceptions\LabelException;
9
use Krenor\Prometheus\Contracts\Types\Observable;
10
use Krenor\Prometheus\Exceptions\PrometheusException;
11
use Krenor\Prometheus\Storage\Builders\HistogramSamplesBuilder;
12
13
abstract class Histogram extends Metric implements Observable
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 21
    public function __construct()
36
    {
37 21
        parent::__construct();
38
39 21
        foreach ($this->labels as $label) {
40 19
            if (preg_match('/^le$/', $label)) {
41 19
                throw new LabelException('The label `le` is used internally to designate buckets.');
42
            }
43
        }
44
45 20
        if (count($this->buckets) < 1) {
46 1
            throw new PrometheusException('Histograms must contain at least one bucket.');
47
        }
48
49 19
        sort($this->buckets);
50 19
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 6
    final public function builder(Collection $items): SamplesBuilder
56
    {
57 6
        return new HistogramSamplesBuilder($this, $items);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 5
    final public function type(): string
64
    {
65 5
        return 'histogram';
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 5
    public function observe(float $value, array $labels): self
72
    {
73 5
        static::$storage->observe($this, $value, $labels);
74
75 5
        return $this;
76
    }
77
78
    /**
79
     * @return Collection
80
     */
81 14
    public function buckets(): Collection
82
    {
83 14
        return new Collection($this->buckets);
84
    }
85
}
86