HDDS-1867. Invalid Prometheus metric name from JvmMetrics

Closes #1172
This commit is contained in:
Doroszlai, Attila 2019-07-29 10:27:55 +02:00 committed by Márton Elek
parent 02bd02b5af
commit 902ff4a2f6
No known key found for this signature in database
GPG Key ID: D51EA8F00EE79B28
2 changed files with 16 additions and 2 deletions

View File

@ -49,6 +49,9 @@ public class PrometheusMetricsSink implements MetricsSink {
private static final Pattern SPLIT_PATTERN =
Pattern.compile("(?<!(^|[A-Z_]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])");
private static final Pattern REPLACE_PATTERN =
Pattern.compile("[^a-zA-Z0-9]+");
public PrometheusMetricsSink() {
}
@ -101,9 +104,9 @@ public class PrometheusMetricsSink implements MetricsSink {
String baseName = StringUtils.capitalize(recordName)
+ StringUtils.capitalize(metricName);
baseName = baseName.replace('-', '_');
String[] parts = SPLIT_PATTERN.split(baseName);
return String.join("_", parts).toLowerCase();
String result = String.join("_", parts).toLowerCase();
return REPLACE_PATTERN.matcher(result).replaceAll("_");
}
@Override

View File

@ -107,6 +107,17 @@ public class TestPrometheusMetricsSink {
sink.prometheusName(recordName, metricName));
}
@Test
public void testNamingSpaces() {
PrometheusMetricsSink sink = new PrometheusMetricsSink();
String recordName = "JvmMetrics";
String metricName = "GcTimeMillisG1 Young Generation";
Assert.assertEquals(
"jvm_metrics_gc_time_millis_g1_young_generation",
sink.prometheusName(recordName, metricName));
}
/**
* Example metric pojo.
*/