diff --git a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/CSMMetrics.java b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/CSMMetrics.java index def0d7ff40..ebbec4de66 100644 --- a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/CSMMetrics.java +++ b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/CSMMetrics.java @@ -62,7 +62,7 @@ public class CSMMetrics { public CSMMetrics() { int numCmdTypes = ContainerProtos.Type.values().length; this.opsLatency = new MutableRate[numCmdTypes]; - this.registry = new MetricsRegistry(CSMMetrics.class.getName()); + this.registry = new MetricsRegistry(CSMMetrics.class.getSimpleName()); for (int i = 0; i < numCmdTypes; i++) { opsLatency[i] = registry.newRate( ContainerProtos.Type.forNumber(i + 1).toString(), diff --git a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/PrometheusMetricsSink.java b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/PrometheusMetricsSink.java index 52532f1723..df25cfc9a3 100644 --- a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/PrometheusMetricsSink.java +++ b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/PrometheusMetricsSink.java @@ -23,7 +23,6 @@ import java.io.Writer; import java.util.HashMap; import java.util.Map; -import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; @@ -47,8 +46,8 @@ public class PrometheusMetricsSink implements MetricsSink { */ private Map metricLines = new HashMap<>(); - private static final Pattern UPPER_CASE_SEQ = - Pattern.compile("([A-Z]*)([A-Z])"); + private static final Pattern SPLIT_PATTERN = + Pattern.compile("(? 0) { - replacement = "_" + m.group(1).toLowerCase() + replacement; - } - m.appendReplacement(sb, replacement); - } - m.appendTail(sb); - - //always prefixed with "_" - return sb.toString().substring(1); - } - - private String upperFirst(String name) { - if (Character.isLowerCase(name.charAt(0))) { - return Character.toUpperCase(name.charAt(0)) + name.substring(1); - } else { - return name; - } + String baseName = StringUtils.capitalize(recordName) + + StringUtils.capitalize(metricName); + baseName = baseName.replace('-', '_'); + String[] parts = SPLIT_PATTERN.split(baseName); + return String.join("_", parts).toLowerCase(); } @Override diff --git a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/TestPrometheusMetricsSink.java b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/TestPrometheusMetricsSink.java index 0a8eb676d8..a1a9a55500 100644 --- a/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/TestPrometheusMetricsSink.java +++ b/hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/TestPrometheusMetricsSink.java @@ -30,7 +30,7 @@ import org.junit.Assert; import org.junit.Test; -import static org.apache.commons.codec.CharEncoding.UTF_8; +import static java.nio.charset.StandardCharsets.UTF_8; /** * Test prometheus Sink. @@ -59,10 +59,11 @@ public void testPublish() throws IOException { writer.flush(); //THEN - System.out.println(stream.toString(UTF_8)); + String writtenMetrics = stream.toString(UTF_8.name()); + System.out.println(writtenMetrics); Assert.assertTrue( "The expected metric line is missing from prometheus metrics output", - stream.toString(UTF_8).contains( + writtenMetrics.contains( "test_metrics_num_bucket_create_fails{context=\"dfs\"") ); @@ -71,7 +72,7 @@ public void testPublish() throws IOException { } @Test - public void testNaming() throws IOException { + public void testNamingCamelCase() { PrometheusMetricsSink sink = new PrometheusMetricsSink(); Assert.assertEquals("rpc_time_some_metrics", @@ -82,18 +83,35 @@ public void testNaming() throws IOException { Assert.assertEquals("rpc_time_small", sink.prometheusName("RpcTime", "small")); + } + @Test + public void testNamingRocksDB() { //RocksDB metrics are handled differently. - + PrometheusMetricsSink sink = new PrometheusMetricsSink(); Assert.assertEquals("rocksdb_om.db_num_open_connections", sink.prometheusName("Rocksdb_om.db", "num_open_connections")); } + @Test + public void testNamingPipeline() { + PrometheusMetricsSink sink = new PrometheusMetricsSink(); + + String recordName = "SCMPipelineMetrics"; + String metricName = "NumBlocksAllocated-" + + "RATIS-THREE-47659e3d-40c9-43b3-9792-4982fc279aba"; + Assert.assertEquals( + "scm_pipeline_metrics_" + + "num_blocks_allocated_" + + "ratis_three_47659e3d_40c9_43b3_9792_4982fc279aba", + sink.prometheusName(recordName, metricName)); + } + /** * Example metric pojo. */ @Metrics(about = "Test Metrics", context = "dfs") - public static class TestMetrics { + private static class TestMetrics { @Metric private MutableCounterLong numBucketCreateFails;