Histogram   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 70
ccs 16
cts 18
cp 0.8889
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 4
A type() 0 3 1
A buckets() 0 3 1
A observe() 0 5 1
A track() 0 3 1
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 18
    public function __construct()
33
    {
34 18
        parent::__construct();
35
36 18
        foreach ($this->labels as $label) {
37 16
            if (preg_match('/^le$/', $label)) {
38 1
                throw new LabelException('The label `le` is used internally to designate buckets.');
39
            }
40
        }
41
42 17
        if (count($this->buckets) < 1) {
43 1
            throw new PrometheusException('Histograms must contain at least one bucket.');
44
        }
45
46 16
        sort($this->buckets);
47 16
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 5
    public function type(): string
53
    {
54 5
        return 'histogram';
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 5
    public function observe(float $value, array $labels = []): static
61
    {
62 5
        static::$storage->observe($this, $value, $labels);
63
64 5
        return $this;
65
    }
66
67
    /**
68
     * @return Collection
69
     */
70 13
    public function buckets(): Collection
71
    {
72 13
        return new Collection($this->buckets);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function track(float $value, array $labels = []): void
79
    {
80
        $this->observe($value, $labels);
81
    }
82
}
83