Skip to content

Commit dce4295

Browse files
simPodLKaemmerling
andauthored
feat: allow to disable metric sort (#108)
Signed-off-by: Simon Podlipsky <simon@podlipsky.net> Co-authored-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
1 parent 144022e commit dce4295

File tree

8 files changed

+80
-45
lines changed

8 files changed

+80
-45
lines changed

src/Prometheus/CollectorRegistry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public function wipeStorage(): void
8181
/**
8282
* @return MetricFamilySamples[]
8383
*/
84-
public function getMetricFamilySamples(): array
84+
public function getMetricFamilySamples(bool $sortMetrics = true): array
8585
{
86-
return $this->storageAdapter->collect();
86+
return $this->storageAdapter->collect($sortMetrics);
8787
}
8888

8989
/**

src/Prometheus/RegistryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function wipeStorage(): void;
1717
/**
1818
* @return MetricFamilySamples[]
1919
*/
20-
public function getMetricFamilySamples(): array;
20+
public function getMetricFamilySamples(bool $sortMetrics = true): array;
2121

2222
/**
2323
* @param string $namespace e.g. cms

src/Prometheus/Storage/APC.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX)
4040
/**
4141
* @return MetricFamilySamples[]
4242
*/
43-
public function collect(): array
43+
public function collect(bool $sortMetrics = true): array
4444
{
4545
$metrics = $this->collectHistograms();
46-
$metrics = array_merge($metrics, $this->collectGauges());
47-
$metrics = array_merge($metrics, $this->collectCounters());
46+
$metrics = array_merge($metrics, $this->collectGauges($sortMetrics));
47+
$metrics = array_merge($metrics, $this->collectCounters($sortMetrics));
4848
$metrics = array_merge($metrics, $this->collectSummaries());
4949
return $metrics;
5050
}
@@ -249,7 +249,7 @@ private function metaData(array $data): array
249249
/**
250250
* @return MetricFamilySamples[]
251251
*/
252-
private function collectCounters(): array
252+
private function collectCounters(bool $sortMetrics = true): array
253253
{
254254
$counters = [];
255255
foreach (new APCuIterator('/^' . $this->prometheusPrefix . ':counter:.*:meta/') as $counter) {
@@ -271,7 +271,11 @@ private function collectCounters(): array
271271
'value' => $this->fromBinaryRepresentationAsInteger($value['value']),
272272
];
273273
}
274-
$this->sortSamples($data['samples']);
274+
275+
if ($sortMetrics) {
276+
$this->sortSamples($data['samples']);
277+
}
278+
275279
$counters[] = new MetricFamilySamples($data);
276280
}
277281
return $counters;
@@ -280,7 +284,7 @@ private function collectCounters(): array
280284
/**
281285
* @return MetricFamilySamples[]
282286
*/
283-
private function collectGauges(): array
287+
private function collectGauges(bool $sortMetrics = true): array
284288
{
285289
$gauges = [];
286290
foreach (new APCuIterator('/^' . $this->prometheusPrefix . ':gauge:.*:meta/') as $gauge) {
@@ -303,7 +307,10 @@ private function collectGauges(): array
303307
];
304308
}
305309

306-
$this->sortSamples($data['samples']);
310+
if ($sortMetrics) {
311+
$this->sortSamples($data['samples']);
312+
}
313+
307314
$gauges[] = new MetricFamilySamples($data);
308315
}
309316
return $gauges;

src/Prometheus/Storage/APCng.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX,
7373
/**
7474
* @return MetricFamilySamples[]
7575
*/
76-
public function collect(): array
76+
public function collect(bool $sortMetrics = true): array
7777
{
7878
$metrics = $this->collectHistograms();
79-
$metrics = array_merge($metrics, $this->collectGauges());
80-
$metrics = array_merge($metrics, $this->collectCounters());
79+
$metrics = array_merge($metrics, $this->collectGauges($sortMetrics));
80+
$metrics = array_merge($metrics, $this->collectCounters($sortMetrics));
8181
$metrics = array_merge($metrics, $this->collectSummaries());
8282
return $metrics;
8383
}
@@ -444,7 +444,7 @@ private function buildPermutationTree(array $labelNames, array $labelValues): ar
444444
/**
445445
* @return MetricFamilySamples[]
446446
*/
447-
private function collectCounters(): array
447+
private function collectCounters(bool $sortMetrics = true): array
448448
{
449449
$counters = [];
450450
foreach ($this->getMetas('counter') as $counter) {
@@ -466,7 +466,11 @@ private function collectCounters(): array
466466
'value' => $this->convertIncrementalIntegerToFloat($value['value']),
467467
];
468468
}
469-
$this->sortSamples($data['samples']);
469+
470+
if ($sortMetrics) {
471+
$this->sortSamples($data['samples']);
472+
}
473+
470474
$counters[] = new MetricFamilySamples($data);
471475
}
472476
return $counters;
@@ -557,7 +561,7 @@ private function getValues(string $type, array $metaData): array /** @phpstan-ig
557561
/**
558562
* @return MetricFamilySamples[]
559563
*/
560-
private function collectGauges(): array
564+
private function collectGauges(bool $sortMetrics = true): array
561565
{
562566
$gauges = [];
563567
foreach ($this->getMetas('gauge') as $gauge) {
@@ -579,7 +583,11 @@ private function collectGauges(): array
579583
'value' => $this->convertIncrementalIntegerToFloat($value['value']),
580584
];
581585
}
582-
$this->sortSamples($data['samples']);
586+
587+
if ($sortMetrics) {
588+
$this->sortSamples($data['samples']);
589+
}
590+
583591
$gauges[] = new MetricFamilySamples($data);
584592
}
585593
return $gauges;

src/Prometheus/Storage/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface Adapter
1616
/**
1717
* @return MetricFamilySamples[]
1818
*/
19-
public function collect(): array;
19+
public function collect(bool $sortMetrics = true): array;
2020

2121
/**
2222
* @param mixed[] $data

src/Prometheus/Storage/InMemory.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class InMemory implements Adapter
3333
/**
3434
* @return MetricFamilySamples[]
3535
*/
36-
public function collect(): array
36+
public function collect(bool $sortMetrics = true): array
3737
{
38-
$metrics = $this->internalCollect($this->counters);
39-
$metrics = array_merge($metrics, $this->internalCollect($this->gauges));
38+
$metrics = $this->internalCollect($this->counters, $sortMetrics);
39+
$metrics = array_merge($metrics, $this->internalCollect($this->gauges, $sortMetrics));
4040
$metrics = array_merge($metrics, $this->collectHistograms());
4141
$metrics = array_merge($metrics, $this->collectSummaries());
4242
return $metrics;
@@ -215,7 +215,7 @@ protected function collectSummaries(): array
215215
* @param mixed[] $metrics
216216
* @return MetricFamilySamples[]
217217
*/
218-
protected function internalCollect(array $metrics): array
218+
protected function internalCollect(array $metrics, bool $sortMetrics = true): array
219219
{
220220
$result = [];
221221
foreach ($metrics as $metric) {
@@ -237,7 +237,11 @@ protected function internalCollect(array $metrics): array
237237
'value' => $value,
238238
];
239239
}
240-
$this->sortSamples($data['samples']);
240+
241+
if ($sortMetrics) {
242+
$this->sortSamples($data['samples']);
243+
}
244+
241245
$result[] = new MetricFamilySamples($data);
242246
}
243247
return $result;

src/Prometheus/Storage/Redis.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ private function valueKey(array $data): string
169169
* @return MetricFamilySamples[]
170170
* @throws StorageException
171171
*/
172-
public function collect(): array
172+
public function collect(bool $sortMetrics = true): array
173173
{
174174
$this->ensureOpenConnection();
175175
$metrics = $this->collectHistograms();
176-
$metrics = array_merge($metrics, $this->collectGauges());
177-
$metrics = array_merge($metrics, $this->collectCounters());
176+
$metrics = array_merge($metrics, $this->collectGauges($sortMetrics));
177+
$metrics = array_merge($metrics, $this->collectCounters($sortMetrics));
178178
$metrics = array_merge($metrics, $this->collectSummaries());
179179
return array_map(
180180
function (array $metric): MetricFamilySamples {
@@ -572,7 +572,7 @@ private function collectSummaries(): array
572572
/**
573573
* @return mixed[]
574574
*/
575-
private function collectGauges(): array
575+
private function collectGauges(bool $sortMetrics = true): array
576576
{
577577
$keys = $this->redis->sMembers(self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
578578
sort($keys);
@@ -593,9 +593,13 @@ private function collectGauges(): array
593593
'value' => $value,
594594
];
595595
}
596-
usort($gauge['samples'], function ($a, $b): int {
597-
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
598-
});
596+
597+
if ($sortMetrics) {
598+
usort($gauge['samples'], function ($a, $b): int {
599+
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
600+
});
601+
}
602+
599603
$gauges[] = $gauge;
600604
}
601605
return $gauges;
@@ -604,7 +608,7 @@ private function collectGauges(): array
604608
/**
605609
* @return mixed[]
606610
*/
607-
private function collectCounters(): array
611+
private function collectCounters(bool $sortMetrics = true): array
608612
{
609613
$keys = $this->redis->sMembers(self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
610614
sort($keys);
@@ -625,9 +629,13 @@ private function collectCounters(): array
625629
'value' => $value,
626630
];
627631
}
628-
usort($counter['samples'], function ($a, $b): int {
629-
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
630-
});
632+
633+
if ($sortMetrics) {
634+
usort($counter['samples'], function ($a, $b): int {
635+
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
636+
});
637+
}
638+
631639
$counters[] = $counter;
632640
}
633641
return $counters;

src/Prometheus/Storage/RedisNg.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ private function valueKey(array $data): string
169169
* @return MetricFamilySamples[]
170170
* @throws StorageException
171171
*/
172-
public function collect(): array
172+
public function collect(bool $sortMetrics = true): array
173173
{
174174
$this->ensureOpenConnection();
175175
$metrics = $this->collectHistograms();
176-
$metrics = array_merge($metrics, $this->collectGauges());
177-
$metrics = array_merge($metrics, $this->collectCounters());
176+
$metrics = array_merge($metrics, $this->collectGauges($sortMetrics));
177+
$metrics = array_merge($metrics, $this->collectCounters($sortMetrics));
178178
$metrics = array_merge($metrics, $this->collectSummaries());
179179
return array_map(
180180
function (array $metric): MetricFamilySamples {
@@ -571,7 +571,7 @@ private function collectSummaries(): array
571571
/**
572572
* @return mixed[]
573573
*/
574-
private function collectGauges(): array
574+
private function collectGauges(bool $sortMetrics = true): array
575575
{
576576
$keys = $this->redis->sMembers(self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
577577
sort($keys);
@@ -589,9 +589,13 @@ private function collectGauges(): array
589589
'value' => $value,
590590
];
591591
}
592-
usort($gauge['samples'], function ($a, $b): int {
593-
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
594-
});
592+
593+
if ($sortMetrics) {
594+
usort($gauge['samples'], function ($a, $b): int {
595+
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
596+
});
597+
}
598+
595599
$gauges[] = $gauge;
596600
}
597601
return $gauges;
@@ -600,7 +604,7 @@ private function collectGauges(): array
600604
/**
601605
* @return mixed[]
602606
*/
603-
private function collectCounters(): array
607+
private function collectCounters(bool $sortMetrics = true): array
604608
{
605609
$keys = $this->redis->sMembers(self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
606610
sort($keys);
@@ -618,9 +622,13 @@ private function collectCounters(): array
618622
'value' => $value,
619623
];
620624
}
621-
usort($counter['samples'], function ($a, $b): int {
622-
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
623-
});
625+
626+
if ($sortMetrics) {
627+
usort($counter['samples'], function ($a, $b): int {
628+
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
629+
});
630+
}
631+
624632
$counters[] = $counter;
625633
}
626634
return $counters;

0 commit comments

Comments
 (0)