Skip to content

Commit

Permalink
feat: allow to disable metric sort (#108)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Podlipsky <[email protected]>
Co-authored-by: Lukas Kämmerling <[email protected]>
  • Loading branch information
simPod and LKaemmerling authored Apr 14, 2023
1 parent 144022e commit dce4295
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/Prometheus/CollectorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Prometheus/RegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 14 additions & 7 deletions src/Prometheus/Storage/APC.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -303,7 +307,10 @@ private function collectGauges(): array
];
}

$this->sortSamples($data['samples']);
if ($sortMetrics) {
$this->sortSamples($data['samples']);
}

$gauges[] = new MetricFamilySamples($data);
}
return $gauges;
Expand Down
22 changes: 15 additions & 7 deletions src/Prometheus/Storage/APCng.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Prometheus/Storage/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Adapter
/**
* @return MetricFamilySamples[]
*/
public function collect(): array;
public function collect(bool $sortMetrics = true): array;

/**
* @param mixed[] $data
Expand Down
14 changes: 9 additions & 5 deletions src/Prometheus/Storage/InMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
30 changes: 19 additions & 11 deletions src/Prometheus/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down
30 changes: 19 additions & 11 deletions src/Prometheus/Storage/RedisNg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit dce4295

Please sign in to comment.