Browse Source

Blitz some more special characters from the metric names

When MVC path matchers are used as metric keys, they can still contain
invalid characters and patterns (like asterisks). This change removes
some more special characters and also tidies up the names a bit so
no key part starts or ends with "-" (which is ugly).

Fixes gh-1528
pull/1588/head
Dave Syer 11 years ago
parent
commit
deef784403
  1. 16
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfiguration.java
  2. 8
      spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfigurationTests.java

16
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfiguration.java

@ -101,7 +101,7 @@ public class MetricFilterAutoConfiguration { @@ -101,7 +101,7 @@ public class MetricFilterAutoConfiguration {
// not convertible
}
if (bestMatchingPattern != null) {
suffix = bestMatchingPattern.toString().replaceAll("[{}]", "-");
suffix = fixSpecialCharacters(bestMatchingPattern.toString());
}
else if (httpStatus.is4xxClientError()) {
suffix = UNKNOWN_PATH_SUFFIX;
@ -114,6 +114,20 @@ public class MetricFilterAutoConfiguration { @@ -114,6 +114,20 @@ public class MetricFilterAutoConfiguration {
}
}
private String fixSpecialCharacters(String value) {
String result = value.replaceAll("[{}]", "-");
result = result.replace("**", "-star-star-");
result = result.replace("*", "-star-");
result = result.replace("/-", "/");
if (result.endsWith("-")) {
result = result.substring(0, result.length() - 1);
}
if (result.startsWith("-")) {
result = result.substring(1);
}
return result;
}
private int getStatus(HttpServletResponse response) {
try {
return response.getStatus();

8
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfigurationTests.java

@ -89,9 +89,9 @@ public class MetricFilterAutoConfigurationTests { @@ -89,9 +89,9 @@ public class MetricFilterAutoConfigurationTests {
mvc.perform(get("/templateVarTest/foo")).andExpect(status().isOk());
verify(context.getBean(CounterService.class)).increment(
"status.200.templateVarTest.-someVariable-");
"status.200.templateVarTest.someVariable");
verify(context.getBean(GaugeService.class)).submit(
eq("response.templateVarTest.-someVariable-"), anyDouble());
eq("response.templateVarTest.someVariable"), anyDouble());
context.close();
}
@ -106,9 +106,9 @@ public class MetricFilterAutoConfigurationTests { @@ -106,9 +106,9 @@ public class MetricFilterAutoConfigurationTests {
mvc.perform(get("/knownPath/foo")).andExpect(status().isNotFound());
verify(context.getBean(CounterService.class)).increment(
"status.404.knownPath.-someVariable-");
"status.404.knownPath.someVariable");
verify(context.getBean(GaugeService.class)).submit(
eq("response.knownPath.-someVariable-"), anyDouble());
eq("response.knownPath.someVariable"), anyDouble());
context.close();
}

Loading…
Cancel
Save