Commit c6f5961a authored by Taylor Wicksell's avatar Taylor Wicksell Committed by Dave Syer

fix for gh-1331 MetricFilter now supports uri template variables when available

parent 0c6a0bde
...@@ -39,6 +39,7 @@ import org.springframework.core.Ordered; ...@@ -39,6 +39,7 @@ import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.UrlPathHelper; import org.springframework.web.util.UrlPathHelper;
/** /**
...@@ -86,6 +87,10 @@ public class MetricFilterAutoConfiguration { ...@@ -86,6 +87,10 @@ public class MetricFilterAutoConfiguration {
} }
finally { finally {
stopWatch.stop(); stopWatch.stop();
if(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE) != null)
{
suffix = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString().replaceAll("[{}]", "-");
}
String gaugeKey = getKey("response" + suffix); String gaugeKey = getKey("response" + suffix);
MetricFilterAutoConfiguration.this.gaugeService.submit(gaugeKey, MetricFilterAutoConfiguration.this.gaugeService.submit(gaugeKey,
stopWatch.getTotalTimeMillis()); stopWatch.getTotalTimeMillis());
......
...@@ -16,6 +16,16 @@ ...@@ -16,6 +16,16 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.Matchers.anyDouble;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import javax.servlet.Filter; import javax.servlet.Filter;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
...@@ -29,14 +39,10 @@ import org.springframework.context.annotation.Bean; ...@@ -29,14 +39,10 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.equalTo; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.junit.Assert.assertThat; import org.springframework.web.bind.annotation.RequestMapping;
import static org.mockito.BDDMockito.willAnswer; import org.springframework.web.bind.annotation.RestController;
import static org.mockito.Matchers.anyDouble;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link MetricFilterAutoConfiguration}. * Tests for {@link MetricFilterAutoConfiguration}.
...@@ -44,6 +50,7 @@ import static org.mockito.Mockito.verify; ...@@ -44,6 +50,7 @@ import static org.mockito.Mockito.verify;
* @author Phillip Webb * @author Phillip Webb
*/ */
public class MetricFilterAutoConfigurationTests { public class MetricFilterAutoConfigurationTests {
@Test @Test
public void recordsHttpInteractions() throws Exception { public void recordsHttpInteractions() throws Exception {
...@@ -67,6 +74,21 @@ public class MetricFilterAutoConfigurationTests { ...@@ -67,6 +74,21 @@ public class MetricFilterAutoConfigurationTests {
anyDouble()); anyDouble());
context.close(); context.close();
} }
@Test
public void recordsHttpInteractionsWithTemplateVariable() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
Config.class, MetricFilterAutoConfiguration.class);
Filter filter = context.getBean(Filter.class);
MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()).addFilter(filter).build();
mvc.perform(get("/templateVarTest/foo"))
.andExpect(status().isOk());
verify(context.getBean(CounterService.class)).increment("status.200.templateVarTest.-someVariable-");
verify(context.getBean(GaugeService.class)).submit(eq("response.templateVarTest.-someVariable-"),
anyDouble());
context.close();
}
@Test @Test
public void skipsFilterIfMissingServices() throws Exception { public void skipsFilterIfMissingServices() throws Exception {
...@@ -88,6 +110,19 @@ public class MetricFilterAutoConfigurationTests { ...@@ -88,6 +110,19 @@ public class MetricFilterAutoConfigurationTests {
public GaugeService gaugeService() { public GaugeService gaugeService() {
return mock(GaugeService.class); return mock(GaugeService.class);
} }
} }
} }
@RestController
class MetricFilterTestController
{
@RequestMapping("templateVarTest/{someVariable}")
public String testTemplateVariableResolution(String someVariable)
{
return someVariable;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment