|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
namespace Triadev\PrometheusExporter; |
|
3
|
|
|
|
|
4
|
|
|
use Prometheus\PushGateway; |
|
5
|
|
|
use Triadev\PrometheusExporter\Contract\PrometheusExporterContract; |
|
6
|
|
|
use Prometheus\CollectorRegistry; |
|
7
|
|
|
use Prometheus\MetricFamilySamples; |
|
8
|
|
|
use Prometheus\Exception\MetricNotFoundException; |
|
9
|
|
|
|
|
10
|
|
|
class PrometheusExporter implements PrometheusExporterContract |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
|
|
|
|
|
13
|
|
|
* @var CollectorRegistry |
|
14
|
|
|
*/ |
|
15
|
|
|
private $registry; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* LpeManager constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param CollectorRegistry $registry |
|
|
|
|
|
|
21
|
2 |
|
*/ |
|
22
|
|
|
public function __construct(CollectorRegistry $registry) |
|
|
|
|
|
|
23
|
2 |
|
{ |
|
|
|
|
|
|
24
|
2 |
|
$this->registry = $registry; |
|
25
|
|
|
} |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Get metric family samples |
|
29
|
|
|
* |
|
30
|
|
|
* @return MetricFamilySamples[] |
|
31
|
2 |
|
*/ |
|
32
|
|
|
public function getMetricFamilySamples() |
|
33
|
2 |
|
{ |
|
|
|
|
|
|
34
|
|
|
return $this->registry->getMetricFamilySamples(); |
|
35
|
|
|
} |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
|
|
|
|
|
38
|
|
|
* @inheritdoc |
|
39
|
2 |
|
*/ |
|
|
|
|
|
|
40
|
|
|
public function incCounter($name, $help, $namespace = null, array $labelKeys = [], array $labelValues = []) |
|
|
|
|
|
|
41
|
2 |
|
{ |
|
|
|
|
|
|
42
|
|
|
$namespace = $this->getNamespace($namespace); |
|
43
|
|
|
|
|
44
|
2 |
|
try { |
|
45
|
2 |
|
$counter = $this->registry->getCounter($namespace, $name); |
|
46
|
2 |
|
} catch (MetricNotFoundException $e) { |
|
47
|
|
|
$counter = $this->registry->registerCounter($namespace, $name, $help, $labelKeys); |
|
48
|
|
|
} |
|
49
|
2 |
|
|
|
50
|
|
|
$counter->inc($labelValues); |
|
51
|
|
|
|
|
52
|
|
|
$this->pushGateway($this->registry, 'inc'); |
|
53
|
|
|
} |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
|
|
|
|
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
|
|
|
|
|
58
|
|
|
public function incByCounter( |
|
59
|
|
|
$name, |
|
60
|
|
|
$help, |
|
61
|
|
|
$value, |
|
62
|
|
|
$namespace = null, |
|
|
|
|
|
|
63
|
|
|
array $labelKeys = [], |
|
|
|
|
|
|
64
|
|
|
array $labelValues = [] |
|
|
|
|
|
|
65
|
|
|
) { |
|
|
|
|
|
|
66
|
|
|
$namespace = $this->getNamespace($namespace); |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
$counter = $this->registry->getCounter($namespace, $name); |
|
70
|
|
|
} catch (MetricNotFoundException $e) { |
|
71
|
|
|
$counter = $this->registry->registerCounter($namespace, $name, $help, $labelKeys); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$counter->incBy($value, $labelValues); |
|
75
|
|
|
|
|
76
|
|
|
$this->pushGateway($this->registry, 'incBy'); |
|
77
|
|
|
} |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
|
|
|
|
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
|
|
|
|
|
82
|
|
|
public function setGauge($name, $help, $value, $namespace = null, array $labelKeys = [], array $labelValues = []) |
|
|
|
|
|
|
83
|
|
|
{ |
|
|
|
|
|
|
84
|
|
|
$namespace = $this->getNamespace($namespace); |
|
85
|
|
|
|
|
86
|
|
|
try { |
|
87
|
|
|
$gauge = $this->registry->getGauge($namespace, $name); |
|
88
|
|
|
} catch (MetricNotFoundException $e) { |
|
89
|
|
|
$gauge = $this->registry->registerGauge($namespace, $name, $help, $labelKeys); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$gauge->set($value, $labelValues); |
|
93
|
|
|
|
|
94
|
|
|
$this->pushGateway($this->registry, 'gauge'); |
|
95
|
|
|
} |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
|
|
|
|
|
98
|
|
|
* @inheritdoc |
|
99
|
|
|
*/ |
|
|
|
|
|
|
100
|
|
|
public function incGauge($name, $help, $namespace = null, array $labelKeys = [], array $labelValues = []) |
|
|
|
|
|
|
101
|
|
|
{ |
|
|
|
|
|
|
102
|
|
|
$namespace = $this->getNamespace($namespace); |
|
103
|
|
|
|
|
104
|
|
|
try { |
|
105
|
|
|
$gauge = $this->registry->getGauge($namespace, $name); |
|
106
|
|
|
} catch (MetricNotFoundException $e) { |
|
107
|
|
|
$gauge = $this->registry->registerGauge($namespace, $name, $help, $labelKeys); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$gauge->inc($labelValues); |
|
111
|
|
|
|
|
112
|
|
|
$this->pushGateway($this->registry, 'inc'); |
|
113
|
|
|
} |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
|
|
|
|
|
116
|
|
|
* @inheritdoc |
|
117
|
|
|
*/ |
|
|
|
|
|
|
118
|
|
|
public function incByGauge( |
|
119
|
|
|
$name, |
|
120
|
|
|
$help, |
|
121
|
|
|
$value, |
|
122
|
|
|
$namespace = null, |
|
|
|
|
|
|
123
|
|
|
array $labelKeys = [], |
|
|
|
|
|
|
124
|
|
|
array $labelValues = [] |
|
|
|
|
|
|
125
|
|
|
) { |
|
|
|
|
|
|
126
|
|
|
$namespace = $this->getNamespace($namespace); |
|
127
|
|
|
|
|
128
|
|
|
try { |
|
129
|
|
|
$gauge = $this->registry->getGauge($namespace, $name); |
|
130
|
|
|
} catch (MetricNotFoundException $e) { |
|
131
|
|
|
$gauge = $this->registry->registerGauge($namespace, $name, $help, $labelKeys); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$gauge->incBy($value, $labelValues); |
|
135
|
|
|
|
|
136
|
|
|
$this->pushGateway($this->registry, 'incBy'); |
|
137
|
|
|
} |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
|
|
|
|
|
140
|
|
|
* @inheritdoc |
|
141
|
|
|
*/ |
|
|
|
|
|
|
142
|
|
|
public function setHistogram( |
|
143
|
|
|
$name, |
|
144
|
|
|
$help, |
|
145
|
|
|
$value, |
|
146
|
|
|
$namespace = null, |
|
|
|
|
|
|
147
|
|
|
array $labelKeys = [], |
|
|
|
|
|
|
148
|
|
|
array $labelValues = [], |
|
|
|
|
|
|
149
|
|
|
?array $buckets = null |
|
|
|
|
|
|
150
|
|
|
) { |
|
|
|
|
|
|
151
|
|
|
$namespace = $this->getNamespace($namespace); |
|
152
|
|
|
|
|
153
|
|
|
try { |
|
154
|
|
|
$histogram = $this->registry->getHistogram($namespace, $name); |
|
155
|
|
|
} catch (MetricNotFoundException $e) { |
|
156
|
|
|
$histogram = $this->registry->registerHistogram($namespace, $name, $help, $labelKeys, $buckets); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$histogram->observe($value, $labelValues); |
|
160
|
|
|
|
|
161
|
|
|
$this->pushGateway($this->registry, 'histogram'); |
|
162
|
|
|
} |
|
|
|
|
|
|
163
|
2 |
|
|
|
164
|
|
|
private function getNamespace(?string $namespace = null) : string |
|
|
|
|
|
|
165
|
2 |
|
{ |
|
|
|
|
|
|
166
|
2 |
|
if (!$namespace) { |
|
|
|
|
|
|
167
|
|
|
$namespace = config('prometheus-exporter.namespace'); |
|
168
|
|
|
} |
|
169
|
2 |
|
|
|
170
|
|
|
return $namespace; |
|
171
|
|
|
} |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
private function pushGateway(CollectorRegistry $registry, string $job, ?array $groupingKey = null) |
|
|
|
|
|
|
174
|
|
|
{ |
|
|
|
|
|
|
175
|
|
|
if (config('prometheus-exporter.adapter') == 'push') { |
|
|
|
|
|
|
176
|
|
|
$pushGateway = new PushGateway(config('prometheus-exporter.push_gateway.address')); |
|
177
|
|
|
|
|
178
|
|
|
$pushGateway->push( |
|
179
|
|
|
$registry, |
|
180
|
|
|
$job, |
|
181
|
|
|
$groupingKey |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
|
|
|
|
|
185
|
|
|
} |
|
|
|
|
|
|
186
|
|
|
|