Skip to content

Commit 481fe15

Browse files
committed
fix(openmetrics): fix label names and add test for them
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent d9d1d04 commit 481fe15

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

lib/private/OpenMetrics/Exporters/InstanceInfo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public function metrics(): Generator {
5252
yield new Metric(
5353
1,
5454
[
55-
'full version' => $this->serverVersion->getHumanVersion(),
56-
'major version' => (string)$this->serverVersion->getVersion()[0],
55+
'full_version' => $this->serverVersion->getHumanVersion(),
56+
'major_version' => (string)$this->serverVersion->getVersion()[0],
5757
'build' => $this->serverVersion->getBuild(),
5858
'installed' => $this->systemConfig->getValue('installed', false) ? '1' : '0',
5959
],

tests/lib/OpenMetrics/Exporters/ExporterTestCase.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,30 @@ protected function setUp(): void {
2525
$this->metrics = iterator_to_array($this->exporter->metrics());
2626
}
2727

28-
public function testNotEmptyData() {
28+
public function testNotEmptyData(): void {
2929
$this->assertNotEmpty($this->exporter->name());
3030
$this->assertNotEmpty($this->metrics);
3131
}
3232

33-
protected function assertLabelsAre(array $expectedLabels) {
33+
public function testValidNames(): void {
34+
$labelNames = [];
35+
foreach ($this->metrics as $metric) {
36+
foreach ($metric->labels as $label => $value) {
37+
$labelNames[$label] = $label;
38+
}
39+
}
40+
41+
if (empty($labelNames)) {
42+
$this->expectNotToPerformAssertions();
43+
return;
44+
}
45+
foreach ($labelNames as $label) {
46+
$this->assertMatchesRegularExpression('/^[a-z_][a-z0-9_]*$/i', $label);
47+
}
48+
49+
}
50+
51+
protected function assertLabelsAre(array $expectedLabels): void {
3452
$foundLabels = [];
3553
foreach ($this->metrics as $metric) {
3654
$foundLabels[] = $metric->labels;

tests/lib/OpenMetrics/Exporters/InstanceInfoTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function testMetrics(): void {
3333
$this->assertCount(1, $this->metrics);
3434
$metric = array_pop($this->metrics);
3535
$this->assertSame([
36-
'full version' => '33.13.17 Gold',
37-
'major version' => '33',
36+
'full_version' => '33.13.17 Gold',
37+
'major_version' => '33',
3838
'build' => 'dev',
3939
'installed' => '0',
4040
], $metric->labels);

0 commit comments

Comments
 (0)