Use static Patterns in MetricsFilter

Update MetricsFilter to use static `Pattern` instances for regex
replacements rather than compiling them each time.

Fixes gh-3996
pull/3986/merge
Phillip Webb 9 years ago
parent 57a698f388
commit a168670a8d

@ -17,6 +17,11 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -52,6 +57,25 @@ final class MetricsFilter extends OncePerRequestFilter {
private final GaugeService gaugeService; private final GaugeService gaugeService;
private static final Set<PatternReplacer> STATUS_REPLACERS;
static {
Set<PatternReplacer> replacements = new LinkedHashSet<PatternReplacer>();
replacements.add(new PatternReplacer("[{}]", 0, "-"));
replacements.add(new PatternReplacer("**", Pattern.LITERAL, "-star-star-"));
replacements.add(new PatternReplacer("*", Pattern.LITERAL, "-star-"));
replacements.add(new PatternReplacer("/-", Pattern.LITERAL, "/"));
replacements.add(new PatternReplacer("-/", Pattern.LITERAL, "/"));
STATUS_REPLACERS = Collections.unmodifiableSet(replacements);
}
private static final Set<PatternReplacer> KEY_REPLACERS;
static {
Set<PatternReplacer> replacements = new LinkedHashSet<PatternReplacer>();
replacements.add(new PatternReplacer("/", Pattern.LITERAL, "."));
replacements.add(new PatternReplacer("..", Pattern.LITERAL, "."));
KEY_REPLACERS = Collections.unmodifiableSet(replacements);
}
MetricsFilter(CounterService counterService, GaugeService gaugeService) { MetricsFilter(CounterService counterService, GaugeService gaugeService) {
this.counterService = counterService; this.counterService = counterService;
this.gaugeService = gaugeService; this.gaugeService = gaugeService;
@ -105,11 +129,10 @@ final class MetricsFilter extends OncePerRequestFilter {
} }
private String fixSpecialCharacters(String value) { private String fixSpecialCharacters(String value) {
String result = value.replaceAll("[{}]", "-"); String result = value;
result = result.replace("**", "-star-star-"); for (PatternReplacer replacer : STATUS_REPLACERS) {
result = result.replace("*", "-star-"); result = replacer.apply(result);
result = result.replace("/-", "/"); }
result = result.replace("-/", "/");
if (result.endsWith("-")) { if (result.endsWith("-")) {
result = result.substring(0, result.length() - 1); result = result.substring(0, result.length() - 1);
} }
@ -131,15 +154,17 @@ final class MetricsFilter extends OncePerRequestFilter {
private String getKey(String string) { private String getKey(String string) {
// graphite compatible metric names // graphite compatible metric names
String value = string.replace("/", "."); String key = string;
value = value.replace("..", "."); for (PatternReplacer replacer : KEY_REPLACERS) {
if (value.endsWith(".")) { key = replacer.apply(key);
value = value + "root"; }
if (key.endsWith(".")) {
key = key + "root";
} }
if (value.startsWith("_")) { if (key.startsWith("_")) {
value = value.substring(1); key = key.substring(1);
} }
return value; return key;
} }
private void submitToGauge(String metricName, double value) { private void submitToGauge(String metricName, double value) {
@ -160,4 +185,22 @@ final class MetricsFilter extends OncePerRequestFilter {
} }
} }
private static class PatternReplacer {
private final Pattern pattern;
private final String replacement;
PatternReplacer(String regex, int flags, String replacement) {
this.pattern = Pattern.compile(regex, flags);
this.replacement = replacement;
}
public String apply(String input) {
return this.pattern.matcher(input).replaceAll(
Matcher.quoteReplacement(this.replacement));
}
}
} }

Loading…
Cancel
Save