Skip to content

HDDS-1811. Prometheus metrics are broken #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
}
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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\"")
);

Expand All @@ -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",
Expand All @@ -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;
Expand Down