forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetricsService.php
More file actions
226 lines (204 loc) · 6.51 KB
/
MetricsService.php
File metadata and controls
226 lines (204 loc) · 6.51 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace ProcessMaker\Services;
use Exception;
use ProcessMaker\Facades\Metrics;
use Prometheus\CollectorRegistry;
use Prometheus\Counter;
use Prometheus\Gauge;
use Prometheus\Histogram;
use Prometheus\RenderTextFormat;
use Prometheus\Storage\Redis;
use RuntimeException;
class MetricsService
{
/**
* The CollectorRegistry instance used by the MetricsService.
*
* @var CollectorRegistry
*/
private $collectionRegistry;
/**
* The namespace used by the MetricsService.
*
* @var string
*/
private $namespace;
/**
* Initializes the MetricsService with a CollectorRegistry using the provided storage adapter.
*
* @param mixed $adapter The storage adapter to use (e.g., Redis).
*/
public function __construct(private $adapter = null)
{
$this->namespace = config('app.prometheus_namespace', 'app');
try {
// Set up Redis as the adapter if none is provided
if ($adapter === null) {
$adapter = Redis::fromExistingConnection(app('redis')->client());
}
$this->collectionRegistry = new CollectorRegistry($adapter);
} catch (Exception $e) {
throw new RuntimeException('Error initializing the metrics adapter: ' . $e->getMessage());
}
}
/**
* Get the collection registry.
*
* @return CollectorRegistry The collection registry instance.
*/
public function getCollectionRegistry(): CollectorRegistry
{
return $this->collectionRegistry;
}
/**
* Registers or retrieves a counter metric.
*
* @param string $name The name of the counter.
* @param string|null $help The help text of the counter.
* @param array $labels The labels of the counter.
* @return Counter The registered or retrieved counter.
*/
public function counter(string $name, string $help = null, array $labels = []): Counter
{
$help = $help ?? $name;
return $this->collectionRegistry->getOrRegisterCounter(
$this->namespace,
$name,
$help,
$labels
);
}
/**
* Registers or retrieves a gauge metric.
*
* @param string $name The name of the gauge.
* @param string|null $help The help text of the gauge.
* @param array $labels The labels of the gauge.
* @return Gauge The registered or retrieved gauge.
*/
public function gauge(string $name, string $help = null, array $labels = []): Gauge
{
$help = $help ?? $name;
return $this->collectionRegistry->getOrRegisterGauge(
$this->namespace,
$name,
$help,
$labels
);
}
/**
* Registers or retrieves a histogram metric.
*
* @param string $name The name of the histogram.
* @param string|null $help The help text of the histogram.
* @param array $labels The labels of the histogram.
* @param array $buckets The buckets of the histogram.
* @return Histogram The registered or retrieved histogram.
*/
public function histogram(string $name, string $help = null, array $labels = [], array $buckets = [0.1, 1, 5, 10]): Histogram
{
$help = $help ?? $name;
return $this->collectionRegistry->getOrRegisterHistogram(
$this->namespace,
$name,
$help,
$labels,
$buckets
);
}
/**
* Sets a gauge metric to a specific value.
*
* @param string $name The name of the gauge.
* @param float $value The value to set the gauge to.
* @param array $labelValues The values of the labels for the gauge.
*/
public function setGauge(string $name, float $value, array $labelValues = []): void
{
$gauge = $this->collectionRegistry->getGauge($this->namespace, $name);
$gauge->set($value, $labelValues);
}
/**
* Renders the metrics in the Prometheus text format.
*
* @return string The rendered metrics.
*/
public function renderMetrics(): string
{
$renderer = new RenderTextFormat();
$metrics = $this->collectionRegistry->getMetricFamilySamples();
return $renderer->render($metrics);
}
/**
* Increments a counter metric by 1.
*
* @param string $name The name of the counter.
* @param string|null $help The help text of the counter.
* @param array $labels The labels of the counter.
*
* @return void
*/
public function counterInc(string $name, string $help = null, array $labels = []): void
{
// Add system labels
$labels = $this->addSystemLabels($labels);
$labelKeys = array_keys($labels);
Metrics::counter($name, $help, $labelKeys)->inc($labels);
}
/**
* Histogram observation.
*
* @param string $name The name of the histogram.
* @param string|null $help The help text of the histogram.
* @param array $labels The labels of the histogram.
* @param array $buckets The buckets of the histogram.
* @param float $executionTime The execution
*
* @return void
*/
public function histogramObserve(string $name, string $help = null, array $labels = [], array $buckets = [0.1, 1, 5, 10], float $executionTime = 0): void
{
// Add system labels
$labels = $this->addSystemLabels($labels);
$labelKeys = array_keys($labels);
Metrics::histogram(
$name,
$help,
$labelKeys,
$buckets
)->observe(
$executionTime,
$labels
);
}
/**
* Add system labels to the provided labels.
*
* @param array $labels The labels to add system labels to.
*
* @return array The keys of the labels.
*/
public function addSystemLabels(array $labels)
{
// Add system labels
$labels['app_version'] = $this->getApplicationVersion();
$labels['app_name'] = config('app.name');
$labels['app_custom_label'] = config('app.prometheus_custom_label');
return $labels;
}
public function clearMetrics(): void
{
$this->collectionRegistry->wipeStorage();
}
/**
* Gets the version of the application.
*
* @return string The version of the application.
*/
private function getApplicationVersion()
{
$root = base_path('composer.json');
$composer_json_path = json_decode(file_get_contents($root));
return $composer_json_path->version ?? '4.0.0';
}
}