-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathHistogram.php
143 lines (124 loc) · 3.57 KB
/
Histogram.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace Prometheus;
use InvalidArgumentException;
use Prometheus\Storage\Adapter;
class Histogram extends Collector
{
const TYPE = 'histogram';
/**
* @var float[]|null
*/
private $buckets;
/**
* @param Adapter $adapter
* @param string $namespace
* @param string $name
* @param string $help
* @param string[] $labels
* @param float[]|null $buckets
*/
public function __construct(
Adapter $adapter,
string $namespace,
string $name,
string $help,
array $labels = [],
array $buckets = null
) {
parent::__construct($adapter, $namespace, $name, $help, $labels);
if (null === $buckets) {
$buckets = self::getDefaultBuckets();
}
if (0 === count($buckets)) {
throw new InvalidArgumentException("Histogram must have at least one bucket.");
}
for ($i = 0; $i < count($buckets) - 1; $i++) {
if ($buckets[$i] >= $buckets[$i + 1]) {
throw new InvalidArgumentException(
"Histogram buckets must be in increasing order: " .
$buckets[$i] . " >= " . $buckets[$i + 1]
);
}
}
if (in_array('le', $labels, true)) {
throw new \InvalidArgumentException("Histogram cannot have a label named 'le'.");
}
$this->buckets = $buckets;
}
/**
* List of default buckets suitable for typical web application latency metrics
*
* @return float[]
*/
public static function getDefaultBuckets(): array
{
return [
0.005,
0.01,
0.025,
0.05,
0.075,
0.1,
0.25,
0.5,
0.75,
1.0,
2.5,
5.0,
7.5,
10.0,
];
}
/**
* @param float $start
* @param float $growthFactor
* @param int $numberOfBuckets
*
* @return float[]
*/
public static function exponentialBuckets(float $start, float $growthFactor, int $numberOfBuckets): array
{
if ($numberOfBuckets < 1) {
throw new InvalidArgumentException('Number of buckets must be a positive integer');
}
if ($start <= 0) {
throw new InvalidArgumentException('The starting position of a set of buckets must be a positive integer');
}
if ($growthFactor <= 1) {
throw new InvalidArgumentException('The growth factor must greater than 1');
}
$buckets = [];
for ($i = 0; $i < $numberOfBuckets; $i++) {
$buckets[$i] = $start;
$start *= $growthFactor;
}
return $buckets;
}
/**
* @param double $value e.g. 123
* @param string[] $labels e.g. ['status', 'opcode']
*/
public function observe(float $value, array $labels = []): void
{
$this->assertLabelsAreDefinedCorrectly($labels);
$this->storageAdapter->updateHistogram(
[
'value' => $value,
'name' => $this->getName(),
'help' => $this->getHelp(),
'type' => $this->getType(),
'labelNames' => $this->getLabelNames(),
'labelValues' => $labels,
'buckets' => $this->buckets,
]
);
}
/**
* @return string
*/
public function getType(): string
{
return self::TYPE;
}
}