diff --git a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricMetadata.java b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricMetadata.java index b9e577e2c..8a393cf95 100644 --- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricMetadata.java +++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/MetricMetadata.java @@ -107,7 +107,7 @@ private void validate() { } if (hasUnit()) { if (!name.endsWith("_" + unit) && !name.endsWith("." + unit)) { - throw new IllegalArgumentException("'" + name + "': Illegal metric name. The name must end with _" + unit + "." + throw new IllegalArgumentException("'" + name + "': Illegal metric name. If the unit is non-null, the name must end with the unit: _" + unit + "." + " Call " + PrometheusNaming.class.getSimpleName() + ".sanitizeMetricName(name, unit) to avoid this error."); } } diff --git a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricMetadataTest.java b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricMetadataTest.java index 77a328873..f9e9a18d6 100644 --- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricMetadataTest.java +++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/MetricMetadataTest.java @@ -54,4 +54,19 @@ public void testSanitizationWeirdCornerCase() { public void testSanitizeEmptyString() { sanitizeMetricName(""); } + + @Test(expected = IllegalArgumentException.class) + public void testUnitSuffixRequired() { + new MetricMetadata("my_counter", "help", Unit.SECONDS); + } + + @Test + public void testUnitSuffixAdded() { + new MetricMetadata(sanitizeMetricName("my_counter", Unit.SECONDS), "help", Unit.SECONDS); + } + + @Test + public void testUnitNotDuplicated() { + Assert.assertEquals("my_counter_bytes", sanitizeMetricName("my_counter_bytes", Unit.BYTES)); + } }