diff --git a/src/Prometheus/RenderTextFormat.php b/src/Prometheus/RenderTextFormat.php index 2b95ae9..95fe2b5 100644 --- a/src/Prometheus/RenderTextFormat.php +++ b/src/Prometheus/RenderTextFormat.php @@ -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()}"; diff --git a/src/Prometheus/Storage/Redis.php b/src/Prometheus/Storage/Redis.php index 9de1794..04757b2 100644 --- a/src/Prometheus/Storage/Redis.php +++ b/src/Prometheus/Storage/Redis.php @@ -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); @@ -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; @@ -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; diff --git a/tests/Test/Prometheus/CollectorRegistryTest.php b/tests/Test/Prometheus/CollectorRegistryTest.php index f394d41..ba25704 100644 --- a/tests/Test/Prometheus/CollectorRegistryTest.php +++ b/tests/Test/Prometheus/CollectorRegistryTest.php @@ -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( @@ -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 ) @@ -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')); @@ -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 )