Skip to content
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

ZOOKEEPER-4795: add namespace support for prometheus metrics #2119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -110,6 +110,7 @@ public class PrometheusMetricsProvider implements MetricsProvider {
private int maxQueueSize = 1000000;
private long workerShutdownTimeoutMs = 1000;
private Optional<ExecutorService> executorOptional = Optional.empty();
private String namespace;

@Override
public void configure(Properties configuration) throws MetricsProviderLifeCycleException {
Expand All @@ -123,6 +124,7 @@ public void configure(Properties configuration) throws MetricsProviderLifeCycleE
configuration.getProperty(MAX_QUEUE_SIZE, "1000000"));
this.workerShutdownTimeoutMs = Long.parseLong(
configuration.getProperty(WORKER_SHUTDOWN_TIMEOUT_MS, "1000"));
this.namespace = configuration.getProperty("namespace", "");
}

@Override
Expand Down Expand Up @@ -376,6 +378,7 @@ public PrometheusCounter(String name) {
this.name = name;
this.inner = io.prometheus.client.Counter
.build(name, name)
.namespace(namespace)
.register(collectorRegistry);
}

Expand Down Expand Up @@ -408,6 +411,7 @@ public PrometheusLabelledCounter(final String name) {
this.inner = io.prometheus.client.Counter
.build(name, name)
.labelNames(LABELS)
.namespace(namespace)
.register(collectorRegistry);
}

Expand All @@ -433,6 +437,7 @@ public PrometheusGaugeWrapper(String name, Gauge gauge, io.prometheus.client.Gau
this.inner = prev != null ? prev
: io.prometheus.client.Gauge
.build(name, name)
.namespace(namespace)
.register(collectorRegistry);
}

Expand Down Expand Up @@ -465,6 +470,7 @@ private PrometheusLabelledGaugeWrapper(final String name,
this.inner = prev != null ? prev :
io.prometheus.client.Gauge
.build(name, name)
.namespace(namespace)
.labelNames(LABELS)
.register(collectorRegistry);
}
Expand Down Expand Up @@ -493,13 +499,15 @@ public PrometheusSummary(String name, MetricsContext.DetailLevel level) {
if (level == MetricsContext.DetailLevel.ADVANCED) {
this.inner = io.prometheus.client.Summary
.build(name, name)
.namespace(namespace)
.quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
.quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
.quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
.register(collectorRegistry);
} else {
this.inner = io.prometheus.client.Summary
.build(name, name)
.namespace(namespace)
.quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
.register(collectorRegistry);
}
Expand Down Expand Up @@ -530,6 +538,7 @@ public PrometheusLabelledSummary(String name, MetricsContext.DetailLevel level)
this.inner = io.prometheus.client.Summary
.build(name, name)
.labelNames(LABELS)
.namespace(namespace)
.quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
.quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
.quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
Expand All @@ -538,6 +547,7 @@ public PrometheusLabelledSummary(String name, MetricsContext.DetailLevel level)
this.inner = io.prometheus.client.Summary
.build(name, name)
.labelNames(LABELS)
.namespace(namespace)
.quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
.register(collectorRegistry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,53 @@ private void createAndRegisterGaugeSet(final String name,
provider.getRootContext().registerGaugeSet(name, gaugeSet);
}

@Test
public void testNamespace() throws Exception {
provider.stop();
provider = new PrometheusMetricsProvider();
Properties configuration = new Properties();
configuration.setProperty("httpPort", "0"); // ephemeral port
configuration.setProperty("exportJvmInfo", "false");
configuration.setProperty("namespace", "zk_test_namespace");
provider.configure(configuration);
provider.start();

Summary summary = provider.getRootContext()
.getSummary("cc", MetricsContext.DetailLevel.BASIC);
summary.add(10);
summary.add(10);
int[] count = {0};
provider.dump((k, v) -> {
count[0]++;
int value = ((Number) v).intValue();

switch (k) {
case "zk_test_namespace_cc{quantile=\"0.5\"}":
assertEquals(10, value);
break;
case "zk_test_namespace_cc_count":
assertEquals(2, value);
break;
case "zk_test_namespace_cc_sum":
assertEquals(20, value);
break;
default:
fail("unespected key " + k);
break;
}
}
);
assertEquals(3, count[0]);
count[0] = 0;

String res = callServlet();

assertThat(res, containsString("# TYPE zk_test_namespace_cc summary"));
assertThat(res, CoreMatchers.containsString("zk_test_namespace_cc_sum 20.0"));
assertThat(res, CoreMatchers.containsString("zk_test_namespace_cc_count 2.0"));
assertThat(res, CoreMatchers.containsString("zk_test_namespace_cc{quantile=\"0.5\",} 10.0"));
}

private void validateWithDump(final Map<String, Number> expectedMetrics) {
final Map<String, Object> returnedMetrics = new HashMap<>();
provider.dump(returnedMetrics::put);
Expand Down