Skip to content

Commit

Permalink
feat(metrics): Labels / tags can be configured on a per-label basis
Browse files Browse the repository at this point in the history
  • Loading branch information
brasseld committed Apr 29, 2019
1 parent c674e87 commit dda5b6f
Showing 1 changed file with 40 additions and 17 deletions.
Expand Up @@ -28,7 +28,10 @@
import io.micrometer.core.instrument.config.MeterFilter;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.micrometer.*;
import io.vertx.micrometer.Label;
import io.vertx.micrometer.MetricsDomain;
import io.vertx.micrometer.MicrometerMetricsOptions;
import io.vertx.micrometer.VertxPrometheusOptions;
import io.vertx.micrometer.backends.BackendRegistries;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -37,6 +40,10 @@
import org.springframework.core.env.Environment;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
Expand Down Expand Up @@ -99,20 +106,15 @@ private void configureMetrics(VertxOptions options) {
MetricsDomain.EVENT_BUS))
.setEnabled(true);

Match pathMatch = new Match()
.setLabel("path")
.setType(MatchType.REGEX)
.setValue(".*")
.setAlias("_");
Match remoteMatch = new Match()
.setLabel("remote")
.setType(MatchType.REGEX)
.setValue(".*")
.setAlias("_");

micrometerMetricsOptions
.addLabelMatch(pathMatch)
.addLabelMatch(remoteMatch);
// 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));
}

options.setMetricsOptions(micrometerMetricsOptions);

Expand All @@ -135,6 +137,28 @@ public boolean isSingleton() {
return true;
}

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

boolean found = true;
int idx = 0;

while (found) {
String label = environment.getProperty("services.metrics.labels[" + idx + "]");
found = (label != null);
if (found) {
if (labels == null) {
labels = new HashSet<>();
}
labels.add(label);
}
idx++;
}

return labels;
}

private class RenameVertxFilter implements MeterFilter {

@Override
Expand All @@ -146,5 +170,4 @@ public Meter.Id map(Meter.Id id) {
return id;
}
}
}

}

0 comments on commit dda5b6f

Please sign in to comment.