mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
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
This commit is contained in:
parent
437fb75424
commit
deef784403
@ -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 {
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
@ -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 {
|
||||
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…
Reference in New Issue
Block a user