mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
fix for gh-1331 MetricFilter now supports uri template variables when available
This commit is contained in:
parent
0c6a0bde5f
commit
c6f5961ad6
@ -39,6 +39,7 @@ import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.util.StopWatch;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
@ -86,6 +87,10 @@ public class MetricFilterAutoConfiguration {
|
||||
}
|
||||
finally {
|
||||
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);
|
||||
MetricFilterAutoConfiguration.this.gaugeService.submit(gaugeKey,
|
||||
stopWatch.getTotalTimeMillis());
|
||||
|
@ -16,6 +16,16 @@
|
||||
|
||||
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.FilterChain;
|
||||
|
||||
@ -29,14 +39,10 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
|
||||
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 org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Tests for {@link MetricFilterAutoConfiguration}.
|
||||
@ -44,6 +50,7 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class MetricFilterAutoConfigurationTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void recordsHttpInteractions() throws Exception {
|
||||
@ -67,6 +74,21 @@ public class MetricFilterAutoConfigurationTests {
|
||||
anyDouble());
|
||||
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
|
||||
public void skipsFilterIfMissingServices() throws Exception {
|
||||
@ -88,6 +110,19 @@ public class MetricFilterAutoConfigurationTests {
|
||||
public GaugeService gaugeService() {
|
||||
return mock(GaugeService.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@RestController
|
||||
class MetricFilterTestController
|
||||
{
|
||||
|
||||
@RequestMapping("templateVarTest/{someVariable}")
|
||||
public String testTemplateVariableResolution(String someVariable)
|
||||
{
|
||||
return someVariable;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user