Skip to content

Commit deef784

Browse files
author
Dave Syer
committed
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
1 parent 437fb75 commit deef784

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected void doFilterInternal(HttpServletRequest request,
101101
// not convertible
102102
}
103103
if (bestMatchingPattern != null) {
104-
suffix = bestMatchingPattern.toString().replaceAll("[{}]", "-");
104+
suffix = fixSpecialCharacters(bestMatchingPattern.toString());
105105
}
106106
else if (httpStatus.is4xxClientError()) {
107107
suffix = UNKNOWN_PATH_SUFFIX;
@@ -114,6 +114,20 @@ else if (httpStatus.is4xxClientError()) {
114114
}
115115
}
116116

117+
private String fixSpecialCharacters(String value) {
118+
String result = value.replaceAll("[{}]", "-");
119+
result = result.replace("**", "-star-star-");
120+
result = result.replace("*", "-star-");
121+
result = result.replace("/-", "/");
122+
if (result.endsWith("-")) {
123+
result = result.substring(0, result.length() - 1);
124+
}
125+
if (result.startsWith("-")) {
126+
result = result.substring(1);
127+
}
128+
return result;
129+
}
130+
117131
private int getStatus(HttpServletResponse response) {
118132
try {
119133
return response.getStatus();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public void recordsHttpInteractionsWithTemplateVariable() throws Exception {
8989
mvc.perform(get("/templateVarTest/foo")).andExpect(status().isOk());
9090

9191
verify(context.getBean(CounterService.class)).increment(
92-
"status.200.templateVarTest.-someVariable-");
92+
"status.200.templateVarTest.someVariable");
9393
verify(context.getBean(GaugeService.class)).submit(
94-
eq("response.templateVarTest.-someVariable-"), anyDouble());
94+
eq("response.templateVarTest.someVariable"), anyDouble());
9595
context.close();
9696
}
9797

@@ -106,9 +106,9 @@ public void recordsKnown404HttpInteractionsAsSingleMetricWithPathAndTemplateVari
106106
mvc.perform(get("/knownPath/foo")).andExpect(status().isNotFound());
107107

108108
verify(context.getBean(CounterService.class)).increment(
109-
"status.404.knownPath.-someVariable-");
109+
"status.404.knownPath.someVariable");
110110
verify(context.getBean(GaugeService.class)).submit(
111-
eq("response.knownPath.-someVariable-"), anyDouble());
111+
eq("response.knownPath.someVariable"), anyDouble());
112112
context.close();
113113
}
114114

0 commit comments

Comments
 (0)