Skip to content

Commit c6f5961

Browse files
Taylor WicksellDave Syer
authored andcommitted
fix for gh-1331 MetricFilter now supports uri template variables when available
1 parent 0c6a0bd commit c6f5961

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.core.annotation.Order;
4040
import org.springframework.util.StopWatch;
4141
import org.springframework.web.filter.OncePerRequestFilter;
42+
import org.springframework.web.servlet.HandlerMapping;
4243
import org.springframework.web.util.UrlPathHelper;
4344

4445
/**
@@ -86,6 +87,10 @@ protected void doFilterInternal(HttpServletRequest request,
8687
}
8788
finally {
8889
stopWatch.stop();
90+
if(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE) != null)
91+
{
92+
suffix = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString().replaceAll("[{}]", "-");
93+
}
8994
String gaugeKey = getKey("response" + suffix);
9095
MetricFilterAutoConfiguration.this.gaugeService.submit(gaugeKey,
9196
stopWatch.getTotalTimeMillis());

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

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure;
1818

19+
import static org.hamcrest.Matchers.equalTo;
20+
import static org.junit.Assert.assertThat;
21+
import static org.mockito.BDDMockito.willAnswer;
22+
import static org.mockito.Matchers.anyDouble;
23+
import static org.mockito.Matchers.eq;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.verify;
26+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28+
1929
import javax.servlet.Filter;
2030
import javax.servlet.FilterChain;
2131

@@ -29,21 +39,18 @@
2939
import org.springframework.context.annotation.Configuration;
3040
import org.springframework.mock.web.MockHttpServletRequest;
3141
import org.springframework.mock.web.MockHttpServletResponse;
32-
33-
import static org.hamcrest.Matchers.equalTo;
34-
import static org.junit.Assert.assertThat;
35-
import static org.mockito.BDDMockito.willAnswer;
36-
import static org.mockito.Matchers.anyDouble;
37-
import static org.mockito.Matchers.eq;
38-
import static org.mockito.Mockito.mock;
39-
import static org.mockito.Mockito.verify;
42+
import org.springframework.test.web.servlet.MockMvc;
43+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
44+
import org.springframework.web.bind.annotation.RequestMapping;
45+
import org.springframework.web.bind.annotation.RestController;
4046

4147
/**
4248
* Tests for {@link MetricFilterAutoConfiguration}.
4349
*
4450
* @author Phillip Webb
4551
*/
4652
public class MetricFilterAutoConfigurationTests {
53+
4754

4855
@Test
4956
public void recordsHttpInteractions() throws Exception {
@@ -67,6 +74,21 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
6774
anyDouble());
6875
context.close();
6976
}
77+
78+
@Test
79+
public void recordsHttpInteractionsWithTemplateVariable() throws Exception {
80+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
81+
Config.class, MetricFilterAutoConfiguration.class);
82+
Filter filter = context.getBean(Filter.class);
83+
MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()).addFilter(filter).build();
84+
mvc.perform(get("/templateVarTest/foo"))
85+
.andExpect(status().isOk());
86+
87+
verify(context.getBean(CounterService.class)).increment("status.200.templateVarTest.-someVariable-");
88+
verify(context.getBean(GaugeService.class)).submit(eq("response.templateVarTest.-someVariable-"),
89+
anyDouble());
90+
context.close();
91+
}
7092

7193
@Test
7294
public void skipsFilterIfMissingServices() throws Exception {
@@ -88,6 +110,19 @@ public CounterService counterService() {
88110
public GaugeService gaugeService() {
89111
return mock(GaugeService.class);
90112
}
113+
91114
}
92115

93116
}
117+
118+
119+
@RestController
120+
class MetricFilterTestController
121+
{
122+
123+
@RequestMapping("templateVarTest/{someVariable}")
124+
public String testTemplateVariableResolution(String someVariable)
125+
{
126+
return someVariable;
127+
}
128+
}

0 commit comments

Comments
 (0)