From dce4295685582d41bd22385c373b19da4818c1bc Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Fri, 14 Apr 2023 14:31:37 +0200 Subject: [PATCH] feat: allow to disable metric sort (#108) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Simon Podlipsky Co-authored-by: Lukas Kämmerling --- src/Prometheus/CollectorRegistry.php | 4 ++-- src/Prometheus/RegistryInterface.php | 2 +- src/Prometheus/Storage/APC.php | 21 ++++++++++++------- src/Prometheus/Storage/APCng.php | 22 +++++++++++++------- src/Prometheus/Storage/Adapter.php | 2 +- src/Prometheus/Storage/InMemory.php | 14 ++++++++----- src/Prometheus/Storage/Redis.php | 30 ++++++++++++++++++---------- src/Prometheus/Storage/RedisNg.php | 30 ++++++++++++++++++---------- 8 files changed, 80 insertions(+), 45 deletions(-) diff --git a/src/Prometheus/CollectorRegistry.php b/src/Prometheus/CollectorRegistry.php index e91a1a1f..3a8ee688 100644 --- a/src/Prometheus/CollectorRegistry.php +++ b/src/Prometheus/CollectorRegistry.php @@ -81,9 +81,9 @@ public function wipeStorage(): void /** * @return MetricFamilySamples[] */ - public function getMetricFamilySamples(): array + public function getMetricFamilySamples(bool $sortMetrics = true): array { - return $this->storageAdapter->collect(); + return $this->storageAdapter->collect($sortMetrics); } /** diff --git a/src/Prometheus/RegistryInterface.php b/src/Prometheus/RegistryInterface.php index a66ae5e6..28255a27 100644 --- a/src/Prometheus/RegistryInterface.php +++ b/src/Prometheus/RegistryInterface.php @@ -17,7 +17,7 @@ public function wipeStorage(): void; /** * @return MetricFamilySamples[] */ - public function getMetricFamilySamples(): array; + public function getMetricFamilySamples(bool $sortMetrics = true): array; /** * @param string $namespace e.g. cms diff --git a/src/Prometheus/Storage/APC.php b/src/Prometheus/Storage/APC.php index b099cc6a..1e4b24c8 100644 --- a/src/Prometheus/Storage/APC.php +++ b/src/Prometheus/Storage/APC.php @@ -40,11 +40,11 @@ public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX) /** * @return MetricFamilySamples[] */ - public function collect(): array + public function collect(bool $sortMetrics = true): array { $metrics = $this->collectHistograms(); - $metrics = array_merge($metrics, $this->collectGauges()); - $metrics = array_merge($metrics, $this->collectCounters()); + $metrics = array_merge($metrics, $this->collectGauges($sortMetrics)); + $metrics = array_merge($metrics, $this->collectCounters($sortMetrics)); $metrics = array_merge($metrics, $this->collectSummaries()); return $metrics; } @@ -249,7 +249,7 @@ private function metaData(array $data): array /** * @return MetricFamilySamples[] */ - private function collectCounters(): array + private function collectCounters(bool $sortMetrics = true): array { $counters = []; foreach (new APCuIterator('/^' . $this->prometheusPrefix . ':counter:.*:meta/') as $counter) { @@ -271,7 +271,11 @@ private function collectCounters(): array 'value' => $this->fromBinaryRepresentationAsInteger($value['value']), ]; } - $this->sortSamples($data['samples']); + + if ($sortMetrics) { + $this->sortSamples($data['samples']); + } + $counters[] = new MetricFamilySamples($data); } return $counters; @@ -280,7 +284,7 @@ private function collectCounters(): array /** * @return MetricFamilySamples[] */ - private function collectGauges(): array + private function collectGauges(bool $sortMetrics = true): array { $gauges = []; foreach (new APCuIterator('/^' . $this->prometheusPrefix . ':gauge:.*:meta/') as $gauge) { @@ -303,7 +307,10 @@ private function collectGauges(): array ]; } - $this->sortSamples($data['samples']); + if ($sortMetrics) { + $this->sortSamples($data['samples']); + } + $gauges[] = new MetricFamilySamples($data); } return $gauges; diff --git a/src/Prometheus/Storage/APCng.php b/src/Prometheus/Storage/APCng.php index 88ca0bcb..ed468675 100644 --- a/src/Prometheus/Storage/APCng.php +++ b/src/Prometheus/Storage/APCng.php @@ -73,11 +73,11 @@ public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX, /** * @return MetricFamilySamples[] */ - public function collect(): array + public function collect(bool $sortMetrics = true): array { $metrics = $this->collectHistograms(); - $metrics = array_merge($metrics, $this->collectGauges()); - $metrics = array_merge($metrics, $this->collectCounters()); + $metrics = array_merge($metrics, $this->collectGauges($sortMetrics)); + $metrics = array_merge($metrics, $this->collectCounters($sortMetrics)); $metrics = array_merge($metrics, $this->collectSummaries()); return $metrics; } @@ -444,7 +444,7 @@ private function buildPermutationTree(array $labelNames, array $labelValues): ar /** * @return MetricFamilySamples[] */ - private function collectCounters(): array + private function collectCounters(bool $sortMetrics = true): array { $counters = []; foreach ($this->getMetas('counter') as $counter) { @@ -466,7 +466,11 @@ private function collectCounters(): array 'value' => $this->convertIncrementalIntegerToFloat($value['value']), ]; } - $this->sortSamples($data['samples']); + + if ($sortMetrics) { + $this->sortSamples($data['samples']); + } + $counters[] = new MetricFamilySamples($data); } return $counters; @@ -557,7 +561,7 @@ private function getValues(string $type, array $metaData): array /** @phpstan-ig /** * @return MetricFamilySamples[] */ - private function collectGauges(): array + private function collectGauges(bool $sortMetrics = true): array { $gauges = []; foreach ($this->getMetas('gauge') as $gauge) { @@ -579,7 +583,11 @@ private function collectGauges(): array 'value' => $this->convertIncrementalIntegerToFloat($value['value']), ]; } - $this->sortSamples($data['samples']); + + if ($sortMetrics) { + $this->sortSamples($data['samples']); + } + $gauges[] = new MetricFamilySamples($data); } return $gauges; diff --git a/src/Prometheus/Storage/Adapter.php b/src/Prometheus/Storage/Adapter.php index fc39dd81..593b09a1 100644 --- a/src/Prometheus/Storage/Adapter.php +++ b/src/Prometheus/Storage/Adapter.php @@ -16,7 +16,7 @@ interface Adapter /** * @return MetricFamilySamples[] */ - public function collect(): array; + public function collect(bool $sortMetrics = true): array; /** * @param mixed[] $data diff --git a/src/Prometheus/Storage/InMemory.php b/src/Prometheus/Storage/InMemory.php index 9045b020..a79714c7 100644 --- a/src/Prometheus/Storage/InMemory.php +++ b/src/Prometheus/Storage/InMemory.php @@ -33,10 +33,10 @@ class InMemory implements Adapter /** * @return MetricFamilySamples[] */ - public function collect(): array + public function collect(bool $sortMetrics = true): array { - $metrics = $this->internalCollect($this->counters); - $metrics = array_merge($metrics, $this->internalCollect($this->gauges)); + $metrics = $this->internalCollect($this->counters, $sortMetrics); + $metrics = array_merge($metrics, $this->internalCollect($this->gauges, $sortMetrics)); $metrics = array_merge($metrics, $this->collectHistograms()); $metrics = array_merge($metrics, $this->collectSummaries()); return $metrics; @@ -215,7 +215,7 @@ protected function collectSummaries(): array * @param mixed[] $metrics * @return MetricFamilySamples[] */ - protected function internalCollect(array $metrics): array + protected function internalCollect(array $metrics, bool $sortMetrics = true): array { $result = []; foreach ($metrics as $metric) { @@ -237,7 +237,11 @@ protected function internalCollect(array $metrics): array 'value' => $value, ]; } - $this->sortSamples($data['samples']); + + if ($sortMetrics) { + $this->sortSamples($data['samples']); + } + $result[] = new MetricFamilySamples($data); } return $result; diff --git a/src/Prometheus/Storage/Redis.php b/src/Prometheus/Storage/Redis.php index 215f3544..f8f67431 100644 --- a/src/Prometheus/Storage/Redis.php +++ b/src/Prometheus/Storage/Redis.php @@ -169,12 +169,12 @@ private function valueKey(array $data): string * @return MetricFamilySamples[] * @throws StorageException */ - public function collect(): array + public function collect(bool $sortMetrics = true): array { $this->ensureOpenConnection(); $metrics = $this->collectHistograms(); - $metrics = array_merge($metrics, $this->collectGauges()); - $metrics = array_merge($metrics, $this->collectCounters()); + $metrics = array_merge($metrics, $this->collectGauges($sortMetrics)); + $metrics = array_merge($metrics, $this->collectCounters($sortMetrics)); $metrics = array_merge($metrics, $this->collectSummaries()); return array_map( function (array $metric): MetricFamilySamples { @@ -572,7 +572,7 @@ private function collectSummaries(): array /** * @return mixed[] */ - private function collectGauges(): array + private function collectGauges(bool $sortMetrics = true): array { $keys = $this->redis->sMembers(self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX); sort($keys); @@ -593,9 +593,13 @@ private function collectGauges(): array 'value' => $value, ]; } - usort($gauge['samples'], function ($a, $b): int { - return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues'])); - }); + + if ($sortMetrics) { + usort($gauge['samples'], function ($a, $b): int { + return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues'])); + }); + } + $gauges[] = $gauge; } return $gauges; @@ -604,7 +608,7 @@ private function collectGauges(): array /** * @return mixed[] */ - private function collectCounters(): array + private function collectCounters(bool $sortMetrics = true): array { $keys = $this->redis->sMembers(self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX); sort($keys); @@ -625,9 +629,13 @@ private function collectCounters(): array 'value' => $value, ]; } - usort($counter['samples'], function ($a, $b): int { - return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues'])); - }); + + if ($sortMetrics) { + usort($counter['samples'], function ($a, $b): int { + return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues'])); + }); + } + $counters[] = $counter; } return $counters; diff --git a/src/Prometheus/Storage/RedisNg.php b/src/Prometheus/Storage/RedisNg.php index 40061e2c..66cc1196 100644 --- a/src/Prometheus/Storage/RedisNg.php +++ b/src/Prometheus/Storage/RedisNg.php @@ -169,12 +169,12 @@ private function valueKey(array $data): string * @return MetricFamilySamples[] * @throws StorageException */ - public function collect(): array + public function collect(bool $sortMetrics = true): array { $this->ensureOpenConnection(); $metrics = $this->collectHistograms(); - $metrics = array_merge($metrics, $this->collectGauges()); - $metrics = array_merge($metrics, $this->collectCounters()); + $metrics = array_merge($metrics, $this->collectGauges($sortMetrics)); + $metrics = array_merge($metrics, $this->collectCounters($sortMetrics)); $metrics = array_merge($metrics, $this->collectSummaries()); return array_map( function (array $metric): MetricFamilySamples { @@ -571,7 +571,7 @@ private function collectSummaries(): array /** * @return mixed[] */ - private function collectGauges(): array + private function collectGauges(bool $sortMetrics = true): array { $keys = $this->redis->sMembers(self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX); sort($keys); @@ -589,9 +589,13 @@ private function collectGauges(): array 'value' => $value, ]; } - usort($gauge['samples'], function ($a, $b): int { - return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues'])); - }); + + if ($sortMetrics) { + usort($gauge['samples'], function ($a, $b): int { + return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues'])); + }); + } + $gauges[] = $gauge; } return $gauges; @@ -600,7 +604,7 @@ private function collectGauges(): array /** * @return mixed[] */ - private function collectCounters(): array + private function collectCounters(bool $sortMetrics = true): array { $keys = $this->redis->sMembers(self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX); sort($keys); @@ -618,9 +622,13 @@ private function collectCounters(): array 'value' => $value, ]; } - usort($counter['samples'], function ($a, $b): int { - return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues'])); - }); + + if ($sortMetrics) { + usort($counter['samples'], function ($a, $b): int { + return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues'])); + }); + } + $counters[] = $counter; } return $counters;