HDDS-1811. Prometheus metrics are broken.

Signed-off-by: Anu Engineer <aengineer@apache.org>
This commit is contained in:
Doroszlai, Attila 2019-07-17 23:29:56 +02:00 committed by Anu Engineer
parent cdc36fe286
commit c958eddcf4
3 changed files with 33 additions and 33 deletions

View File

@ -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(),

View File

@ -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<String, String> metricLines = new HashMap<>();
private static final Pattern UPPER_CASE_SEQ =
Pattern.compile("([A-Z]*)([A-Z])");
private static final Pattern SPLIT_PATTERN =
Pattern.compile("(?<!(^|[A-Z_]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])");
public PrometheusMetricsSink() {
}
@ -88,7 +87,7 @@ public void putMetrics(MetricsRecord metricsRecord) {
}
/**
* Convert CamelCase based namess to lower-case names where the separator
* Convert CamelCase based names to lower-case names where the separator
* is the underscore, to follow prometheus naming conventions.
*/
public String prometheusName(String recordName,
@ -99,29 +98,12 @@ public String prometheusName(String recordName,
recordName.startsWith(ROCKSDB_CONTEXT_PREFIX)) {
return recordName.toLowerCase() + "_" + metricName.toLowerCase();
}
String baseName = upperFirst(recordName) + upperFirst(metricName);
Matcher m = UPPER_CASE_SEQ.matcher(baseName);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String replacement = "_" + m.group(2).toLowerCase();
if (m.group(1).length() > 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

View File

@ -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;