Skip to content

fix for gh-1331 MetricFilter now supports uri template variables when av... #1333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.util.StopWatch;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.UrlPathHelper;

/**
Expand All @@ -55,6 +57,7 @@
public class MetricFilterAutoConfiguration {

private static final int UNDEFINED_HTTP_STATUS = 999;
private static final String UNKNOWN_PATH_SUFFIX = "/unknownPath";

@Autowired
private CounterService counterService;
Expand Down Expand Up @@ -86,6 +89,14 @@ protected void doFilterInternal(HttpServletRequest request,
}
finally {
stopWatch.stop();
if(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE) != null)
{
suffix = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString().replaceAll("[{}]", "-");
}
else if(getStatus(response) == HttpStatus.NOT_FOUND.value())
{
suffix=UNKNOWN_PATH_SUFFIX;
}
String gaugeKey = getKey("response" + suffix);
MetricFilterAutoConfiguration.this.gaugeService.submit(gaugeKey,
stopWatch.getTotalTimeMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@

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.times;
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;

Expand All @@ -27,23 +38,24 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
* Tests for {@link MetricFilterAutoConfiguration}.
*
* @author Phillip Webb
*/
public class MetricFilterAutoConfigurationTests {


@Test
public void recordsHttpInteractions() throws Exception {
Expand All @@ -67,6 +79,54 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
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 recordsKnown404HttpInteractionsAsSingleMetricWithPathAndTemplateVariable() 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("/knownPath/foo"))
.andExpect(status().isNotFound());

verify(context.getBean(CounterService.class)).increment("status.404.knownPath.-someVariable-");
verify(context.getBean(GaugeService.class)).submit(eq("response.knownPath.-someVariable-"),
anyDouble());
context.close();
}

@Test
public void records404HttpInteractionsAsSingleMetric() 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("/unknownPath/1"))
.andExpect(status().isNotFound());

mvc.perform(get("/unknownPath/2"))
.andExpect(status().isNotFound());

verify(context.getBean(CounterService.class), times(2)).increment("status.404.unknownPath");
verify(context.getBean(GaugeService.class), times(2)).submit(eq("response.unknownPath"),
anyDouble());
context.close();
}

@Test
public void skipsFilterIfMissingServices() throws Exception {
Expand All @@ -88,6 +148,27 @@ public CounterService counterService() {
public GaugeService gaugeService() {
return mock(GaugeService.class);
}

}

}


@RestController
class MetricFilterTestController
{

@RequestMapping("templateVarTest/{someVariable}")
public String testTemplateVariableResolution(@PathVariable String someVariable)
{
return someVariable;
}

@RequestMapping("knownPath/{someVariable}")
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public String testKnownPathWith404Response(@PathVariable String someVariable)
{
return someVariable;
}
}