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

Commit 2359025

Browse files
authored
Merge pull request #23 from Jimdo/fix_sorting_issue
Ensure all metrics are sorted by label value
2 parents 24cc0de + 32124fe commit 2359025

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/Prometheus/RenderTextFormat.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ class RenderTextFormat
1313
*/
1414
public function render(array $metrics)
1515
{
16+
usort($metrics, function(MetricFamilySamples $a, MetricFamilySamples $b)
17+
{
18+
return strcmp($a->getName(), $b->getName());
19+
});
20+
1621
$lines = array();
22+
1723
foreach ($metrics as $metric) {
1824
$lines[] = "# HELP " . $metric->getName() . " {$metric->getHelp()}";
1925
$lines[] = "# TYPE " . $metric->getName() . " {$metric->getType()}";

src/Prometheus/Storage/Redis.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public function collect()
6767
$metrics = $this->collectHistograms();
6868
$metrics = array_merge($metrics, $this->collectGauges());
6969
$metrics = array_merge($metrics, $this->collectCounters());
70-
array_multisort($metrics);
7170
return array_map(
7271
function (array $metric) {
7372
return new MetricFamilySamples($metric);
@@ -283,6 +282,9 @@ private function collectGauges()
283282
'value' => $value
284283
);
285284
}
285+
usort($gauge['samples'], function($a, $b){
286+
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
287+
});
286288
$gauges[] = $gauge;
287289
}
288290
return $gauges;
@@ -306,6 +308,9 @@ private function collectCounters()
306308
'value' => $value
307309
);
308310
}
311+
usort($counter['samples'], function($a, $b){
312+
return strcmp(implode("", $a['labelValues']), implode("", $b['labelValues']));
313+
});
309314
$counters[] = $counter;
310315
}
311316
return $counters;

tests/Test/Prometheus/CollectorRegistryTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function itShouldSaveCountersInRedis()
6161
$metric = $registry->registerCounter('test', 'some_metric', 'this is for testing', array('foo', 'bar'));
6262
$metric->incBy(2, array('lalal', 'lululu'));
6363
$registry->getCounter('test', 'some_metric', array('foo', 'bar'))->inc(array('lalal', 'lululu'));
64+
$registry->getCounter('test', 'some_metric', array('foo', 'bar'))->inc(array('lalal', 'lvlvlv'));
6465

6566
$registry = new CollectorRegistry($this->newRedisAdapter());
6667
$this->assertThat(
@@ -69,6 +70,7 @@ public function itShouldSaveCountersInRedis()
6970
# HELP test_some_metric this is for testing
7071
# TYPE test_some_metric counter
7172
test_some_metric{foo="lalal",bar="lululu"} 3
73+
test_some_metric{foo="lalal",bar="lvlvlv"} 1
7274
7375
EOF
7476
)
@@ -83,6 +85,7 @@ public function itShouldSaveHistogramsInRedis()
8385
$registry = new CollectorRegistry($this->redisAdapter);
8486
$metric = $registry->registerHistogram('test', 'some_metric', 'this is for testing', array('foo', 'bar'), array(0.1, 1, 5, 10));
8587
$metric->observe(2, array('lalal', 'lululu'));
88+
$registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('lalal', 'lvlvlv'));
8689
$registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(13, array('lalal', 'lululu'));
8790
$registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('lalal', 'lululu'));
8891
$registry->getHistogram('test', 'some_metric', array('foo', 'bar'))->observe(7.1, array('gnaaha', 'hihihi'));
@@ -107,6 +110,13 @@ public function itShouldSaveHistogramsInRedis()
107110
test_some_metric_bucket{foo="lalal",bar="lululu",le="+Inf"} 3
108111
test_some_metric_count{foo="lalal",bar="lululu"} 3
109112
test_some_metric_sum{foo="lalal",bar="lululu"} 22.1
113+
test_some_metric_bucket{foo="lalal",bar="lvlvlv",le="0.1"} 0
114+
test_some_metric_bucket{foo="lalal",bar="lvlvlv",le="1"} 0
115+
test_some_metric_bucket{foo="lalal",bar="lvlvlv",le="5"} 0
116+
test_some_metric_bucket{foo="lalal",bar="lvlvlv",le="10"} 1
117+
test_some_metric_bucket{foo="lalal",bar="lvlvlv",le="+Inf"} 1
118+
test_some_metric_count{foo="lalal",bar="lvlvlv"} 1
119+
test_some_metric_sum{foo="lalal",bar="lvlvlv"} 7.1
110120
111121
EOF
112122
)

0 commit comments

Comments
 (0)