Skip to content

Commit

Permalink
feat: allows to include or exclude metrics labels
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaeyaert committed Aug 3, 2022
1 parent 55aa0a5 commit f39f612
Show file tree
Hide file tree
Showing 24 changed files with 725 additions and 45 deletions.
2 changes: 1 addition & 1 deletion gravitee-node-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-cache</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-certificates/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-certificates</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-cluster/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-cluster</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-container</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-jetty</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-kubernetes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-kubernetes</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-license/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-license</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-management</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-monitoring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-monitoring</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-notifier/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<artifactId>gravitee-node</artifactId>
<groupId>io.gravitee.node</groupId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node-plugins</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-plugins-service</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-plugins</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-reporter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-reporter</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion gravitee-node-tracing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-tracing</artifactId>
Expand Down
25 changes: 24 additions & 1 deletion gravitee-node-vertx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
<parent>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>1.24.3</version>
<version>1.25.0-8218-prometheus-labels-SNAPSHOT</version>
</parent>

<artifactId>gravitee-node-vertx</artifactId>
<name>Gravitee.io - Node - Vert.x Support</name>

<properties>
<mockito-inline.version>3.12.4</mockito-inline.version>
</properties>

<dependencies>
<!-- Gravitee.io -->
<dependency>
Expand Down Expand Up @@ -81,5 +85,24 @@
<artifactId>spring-context</artifactId>
<scope>provided</scope>
</dependency>

<!-- Tests -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito-inline.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,34 @@
*/
package io.gravitee.node.vertx;

import static java.util.stream.Collectors.*;
import static java.util.stream.StreamSupport.stream;

import io.gravitee.common.util.EnvironmentUtils;
import io.gravitee.node.api.Node;
import io.gravitee.node.tracing.vertx.LazyVertxTracerFactory;
import io.gravitee.node.vertx.metrics.ExcludeTagsFilter;
import io.gravitee.node.vertx.metrics.RenameVertxFilter;
import io.gravitee.node.vertx.verticle.factory.SpringVerticleFactory;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
import io.micrometer.core.instrument.binder.system.FileDescriptorMetrics;
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
import io.micrometer.core.instrument.config.MeterFilter;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.tracing.TracingOptions;
import io.vertx.micrometer.*;
import io.vertx.micrometer.backends.BackendRegistries;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

/**
Expand All @@ -65,6 +67,10 @@ public class VertxFactory implements FactoryBean<Vertx> {
@Autowired
private LazyVertxTracerFactory vertxTracerFactory;

private Set<Label> metricsLabels;

private Map<String, Set<Label>> metricsExcludedLabelsByCategory;

@Override
public Vertx getObject() throws Exception {
LOGGER.debug("Creating a new instance of Vert.x");
Expand Down Expand Up @@ -92,6 +98,10 @@ public Vertx getObject() throws Exception {
.commonTags("application", node.application())
.commonTags("instance", node.hostname());

metricsExcludedLabelsByCategory.forEach((category, labels) ->
registry.config().meterFilter(new ExcludeTagsFilter(category, labels.stream().map(String::valueOf).collect(toList())))
);

new FileDescriptorMetrics().bindTo(registry);
new ClassLoaderMetrics().bindTo(registry);
new JvmMemoryMetrics().bindTo(registry);
Expand Down Expand Up @@ -128,14 +138,8 @@ private void configureMetrics(VertxOptions options) {
}

// Read labels
Set<String> labels = loadLabels();
if (labels != null && !labels.isEmpty()) {
Set<Label> micrometerLabels = labels.stream().map(label -> Label.valueOf(label.toUpperCase())).collect(Collectors.toSet());
micrometerMetricsOptions.setLabels(micrometerLabels);
} else {
// Defaults to
micrometerMetricsOptions.setLabels(EnumSet.of(Label.LOCAL, Label.HTTP_METHOD, Label.HTTP_CODE));
}
this.loadMetricLabels();
micrometerMetricsOptions.setLabels(metricsLabels);

boolean prometheusEnabled = environment.getProperty("services.metrics.prometheus.enabled", Boolean.class, true);
if (prometheusEnabled) {
Expand All @@ -160,7 +164,74 @@ public boolean isSingleton() {
return true;
}

private Set<String> loadLabels() {
private void loadMetricLabels() {
final Map<String, Set<Label>> includedLabelsByCategory = readConfiguredLabelsByCategory("include");

// Include labels takes precedence over defined labels.
Set<String> labels = readConfiguredLabels();
if (labels != null && !labels.isEmpty()) {
metricsLabels = labels.stream().map(this::toLabel).collect(Collectors.toSet());
} else {
metricsLabels = EnumSet.of(Label.LOCAL, Label.HTTP_METHOD, Label.HTTP_CODE);
}

// If a label is activated for a specific category, it must be added globally and then manually excluded for all other categories :-(
includedLabelsByCategory.forEach((category, includedLabels) -> metricsLabels.addAll(includedLabels));

metricsExcludedLabelsByCategory = readConfiguredLabelsByCategory("exclude");

// Identify the labels to exclude for each category.
for (Map.Entry<String, Set<Label>> labelsByCategory : includedLabelsByCategory.entrySet()) {
final String includedCategory = labelsByCategory.getKey();
final Set<Label> includedCategoryLabels = labelsByCategory.getValue();

// Get the domains where these labels are not included (ie: domain on which to explicitly exclude this label).
Arrays
.stream(MetricsDomain.values())
.map(MetricsDomain::toCategory)
.filter(otherCategory -> !otherCategory.equalsIgnoreCase(includedCategory))
.forEach(otherCategory -> {
if (includedLabelsByCategory.containsKey(otherCategory)) {
final Set<Label> otherCategoryLabels = includedLabelsByCategory.get(otherCategory);
includedCategoryLabels.forEach(label -> {
if (!otherCategoryLabels.contains(label)) {
// Label not explicitly included for this category, add it to the exclusion list.
metricsExcludedLabelsByCategory.computeIfAbsent(otherCategory, key -> new HashSet<>()).add(label);
}
});
} else {
// Directly exclude all the labels on the current category.
includedCategoryLabels.forEach(label ->
metricsExcludedLabelsByCategory.computeIfAbsent(otherCategory, key -> new HashSet<>()).add(label)
);
}
});
}
}

private Map<String, Set<Label>> readConfiguredLabelsByCategory(final String type) {
return Arrays
.stream(MetricsDomain.values())
.map(MetricsDomain::toCategory)
.flatMap(category ->
EnvironmentUtils
.getPropertiesStartingWith((ConfigurableEnvironment) environment, "services.metrics." + type + "." + category)
.entrySet()
.stream()
)
.collect(
Collectors.groupingBy(
e -> e.getKey().replaceAll("^services\\.metrics\\." + type + "\\." + "(.*)\\[\\d+]$", "$1"),
Collectors.mapping(e -> toLabel((String) e.getValue()), Collectors.<Label, Set<Label>>toCollection(HashSet::new))
)
);
}

private Label toLabel(String label) {
return Label.valueOf(label.toUpperCase());
}

private Set<String> readConfiguredLabels() {
LOGGER.debug("Looking for metrics labels...");
Set<String> labels = null;

Expand All @@ -182,18 +253,6 @@ private Set<String> loadLabels() {
return labels;
}

private class RenameVertxFilter implements MeterFilter {

@Override
public Meter.Id map(Meter.Id id) {
if (id.getName().startsWith("vertx.")) {
return id.withName(id.getName().substring(6));
}

return id;
}
}

private VertxOptions getVertxOptions() {
VertxOptions options = new VertxOptions();

Expand Down
Loading

0 comments on commit f39f612

Please sign in to comment.