Passed
Branch develop (c85352)
by Stan
02:28
created

Summary::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 Summary extends Metric implements Observable
12
{
13
    use TracksExecutionTime;
14
15
    /**
16
     * @var float[]
17
     */
18
    protected $quantiles = [
19
        .01,
20
        .05,
21
        .5,
22
        .9,
23
        .99,
24
    ];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 14
    public function __construct()
30
    {
31 14
        parent::__construct();
32
33 14
        foreach ($this->labels as $label) {
34 12
            if (preg_match('/^quantile$/', $label)) {
35 12
                throw new LabelException('The label `quantile` is used internally to designate summary quantiles.');
36
            }
37
        }
38
39 13
        foreach ($this->quantiles as $quantile) {
40 13
            if ($quantile < 0 || $quantile > 1) {
41 13
                throw new PrometheusException('Quantiles have to be in the range between 0 and 1.');
42
            }
43
        }
44
45 12
        sort($this->quantiles);
46 12
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 5
    public function type(): string
52
    {
53 5
        return 'summary';
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     *
59
     * @return self
60
     */
61 5
    public function observe(float $value, array $labels = []): Observable
62
    {
63 5
        static::$storage->observe($this, $value, $labels);
64
65 5
        return $this;
66
    }
67
68
    /**
69
     * @return Collection
70
     */
71 9
    public function quantiles(): Collection
72
    {
73 9
        return new Collection($this->quantiles);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function track(float $value, array $labels = []): void
80
    {
81
        $this->observe($value, $labels);
82
    }
83
}
84