Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Merge pull request #23 from Jimdo/fix_sorting_issue
Browse files Browse the repository at this point in the history
Ensure all metrics are sorted by label value
  • Loading branch information
bracki authored Jul 21, 2016
2 parents 24cc0de + 32124fe commit 2359025
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Prometheus/RenderTextFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ class RenderTextFormat
*/
public function render(array $metrics)
{
usort($metrics, function(MetricFamilySamples $a, MetricFamilySamples $b)
{
return strcmp($a->getName(), $b->getName());
});

$lines = array();

foreach ($metrics as $metric) {
$lines[] = "# HELP " . $metric->getName() . " {$metric->getHelp()}";
$lines[] = "# TYPE " . $metric->getName() . " {$metric->getType()}";
Expand Down
7 changes: 6 additions & 1 deletion src/Prometheus/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public function collect()
$metrics = $this->collectHistograms();
$metrics = array_merge($metrics, $this->collectGauges());
$metrics = array_merge($metrics, $this->collectCounters());
array_multisort($metrics);
return array_map(
function (array $metric) {
return new MetricFamilySamples($metric);
Expand Down Expand Up @@ -283,6 +282,9 @@ private function collectGauges()
'value' => $value
);
}
usort($gauge['samples'], function($a, $b){
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
});
$gauges[] = $gauge;
}
return $gauges;
Expand All @@ -306,6 +308,9 @@ private function collectCounters()
'value' => $value
);
}
usort($counter['samples'], function($a, $b){
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
});
$counters[] = $counter;
}
return $counters;
Expand Down
10 changes: 10 additions & 0 deletions tests/Test/Prometheus/CollectorRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function itShouldSaveCountersInRedis()
$metric = $registry->registerCounter('test', 'some_metric', 'this is for testing', array('foo', 'bar'));
$metric->incBy(2, array('lalal', 'lululu'));
$registry->getCounter('test', 'some_metric', array('foo', 'bar'))->inc(array('lalal', 'lululu'));
$registry->getCounter('test', 'some_metric', array('foo', 'bar'))->inc(array('lalal', 'lvlvlv'));

$registry = new CollectorRegistry($this->newRedisAdapter());
$this->assertThat(
Expand All @@ -69,6 +70,7 @@ public function itShouldSaveCountersInRedis()
# HELP test_some_metric this is for testing
# TYPE test_some_metric counter
test_some_metric{foo="lalal",bar="lululu"} 3
test_some_metric{foo="lalal",bar="lvlvlv"} 1
EOF
)
Expand All @@ -83,6 +85,7 @@ public function itShouldSaveHistogramsInRedis()
$registry = new CollectorRegistry($this->redisAdapter);
$metric = $registry->registerHistogram('test', 'some_metric', 'this is for testing', array('foo', 'bar'), array(0.1, 1, 5, 10));
$metric->observe(2, array('lalal', 'lululu'));
$registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('lalal', 'lvlvlv'));
$registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(13, array('lalal', 'lululu'));
$registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('lalal', 'lululu'));
$registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('gnaaha', 'hihihi'));
Expand All @@ -107,6 +110,13 @@ public function itShouldSaveHistogramsInRedis()
test_some_metric_bucket{foo="lalal",bar="lululu",le="+Inf"} 3
test_some_metric_count{foo="lalal",bar="lululu"} 3
test_some_metric_sum{foo="lalal",bar="lululu"} 22.1
test_some_metric_bucket{foo="lalal",bar="lvlvlv",le="0.1"} 0
test_some_metric_bucket{foo="lalal",bar="lvlvlv",le="1"} 0
test_some_metric_bucket{foo="lalal",bar="lvlvlv",le="5"} 0
test_some_metric_bucket{foo="lalal",bar="lvlvlv",le="10"} 1
test_some_metric_bucket{foo="lalal",bar="lvlvlv",le="+Inf"} 1
test_some_metric_count{foo="lalal",bar="lvlvlv"} 1
test_some_metric_sum{foo="lalal",bar="lvlvlv"} 7.1
EOF
)
Expand Down

0 comments on commit 2359025

Please sign in to comment.