Skip to content

Commit

Permalink
Prevent illegal metric names - code review
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Stäber <[email protected]>
  • Loading branch information
fstab committed May 16, 2024
1 parent c82ab1f commit bf242dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit bf242dc

Please sign in to comment.