HADOOP-16495. Fix invalid metric types in PrometheusMetricsSink (#1244)

This commit is contained in:
Akira Ajisaka 2019-08-14 12:24:03 +09:00 committed by GitHub
parent e6d240dc91
commit 0f8add8a60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View File

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

View File

@ -99,6 +99,28 @@ public void testNamingPipeline() {
sink.prometheusName(recordName, metricName)); sink.prometheusName(recordName, metricName));
} }
@Test
public void testNamingPeriods() {
PrometheusMetricsSink sink = new PrometheusMetricsSink();
String recordName = "org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.FsDatasetImpl";
String metricName = "DfsUsed";
Assert.assertEquals(
"org_apache_hadoop_hdfs_server_datanode_fsdataset_impl_fs_dataset_impl_dfs_used",
sink.prometheusName(recordName, metricName));
}
@Test
public void testNamingWhitespaces() {
PrometheusMetricsSink sink = new PrometheusMetricsSink();
String recordName = "JvmMetrics";
String metricName = "GcCount" + "G1 Old Generation";
Assert.assertEquals(
"jvm_metrics_gc_count_g1_old_generation",
sink.prometheusName(recordName, metricName));
}
/** /**
* Example metric pojo. * Example metric pojo.
*/ */