diff --git a/core/Controller/OpenMetricsController.php b/core/Controller/OpenMetricsController.php index 7ed388e7eac..8c57eb1a663 100644 --- a/core/Controller/OpenMetricsController.php +++ b/core/Controller/OpenMetricsController.php @@ -83,10 +83,10 @@ class OpenMetricsController extends Controller { $elapsed = (string)(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']); yield <<assertEquals('200', $response->getStatus()); $this->assertEquals('application/openmetrics-text; version=1.0.0; charset=utf-8', $response->getHeaders()['Content-Type']); $expected = <<assertNotEmpty($this->exporter->name()); $this->assertNotEmpty($this->metrics); } - public function testValidNames(): void { + public function testValidExporterName(): void { + $exporterName = $this->exporter->name(); + $this->assertMatchesRegularExpression('/^[a-z_:][a-z0-9_:]*$/i', $exporterName, ); + + $unit = $this->exporter->unit(); + if ($unit === '') { + return; + } + // Unit name must follow metric name format + $this->assertMatchesRegularExpression('/^[a-z_:][a-z0-9_:]*$/i', $unit); + // Unit name must be a suffix in exporter name + $this->assertMatchesRegularExpression( + '/(^|_)' . $unit . '$/', + $exporterName, + 'Metric name "' . $exporterName . '" must contains unit "' . $unit . '" as a suffix', + ); + } + + public function testValidLabelKey(): void { $labelNames = []; foreach ($this->metrics as $metric) { foreach ($metric->labels as $label => $value) {