Skip to content
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 @@ -16,6 +16,8 @@

package org.springframework.boot.actuate.autoconfigure.metrics.web;

import io.micrometer.core.annotation.Timed;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -24,6 +26,7 @@
*
* @author Dmytro Nosan
* @author Stephane Nicoll
* @author Chanhyeong LEE
*/
@RestController
public class TestController {
Expand All @@ -43,4 +46,10 @@ public String test2() {
return "test2";
}

@Timed
@GetMapping("test3")
public String test3() {
return "test2";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.actuate.autoconfigure.metrics.web.servlet;

import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;

Expand All @@ -24,6 +25,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Timer;
Expand Down Expand Up @@ -64,6 +66,7 @@
* @author Dmytro Nosan
* @author Tadaya Tsuyukubo
* @author Madhura Bhave
* @author Chanhyeong LEE
*/
@ExtendWith(OutputCaptureExtension.class)
class WebMvcMetricsAutoConfigurationTests {
Expand Down Expand Up @@ -157,6 +160,19 @@ void autoTimeRequestsCanBeConfigured() {
});
}

@Test
void timerWorksWithTimedAnnotationsWhenAutoTimeRequestsIsFalse() {
this.contextRunner.withUserConfiguration(TestController.class)
.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class, WebMvcAutoConfiguration.class))
.withPropertyValues("management.metrics.web.server.request.autotime.enabled=false").run((context) -> {
MeterRegistry registry = getInitializedMeterRegistry(context, "/test3");
Collection<Meter> meters = registry.get("http.server.requests").meters();
assertThat(meters).hasSize(1);
Meter meter = meters.iterator().next();
assertThat(meter.getId().getTag("uri")).isEqualTo("/test3");
});
}

@Test
@SuppressWarnings("rawtypes")
void longTaskTimingInterceptorIsRegistered() {
Expand All @@ -167,13 +183,17 @@ void longTaskTimingInterceptorIsRegistered() {
.contains(LongTaskTimingHandlerInterceptor.class));
}

private MeterRegistry getInitializedMeterRegistry(AssertableWebApplicationContext context) throws Exception {
private MeterRegistry getInitializedMeterRegistry(AssertableWebApplicationContext context, String... urls)
throws Exception {
if (urls.length == 0) {
urls = new String[] { "/test0", "/test1", "/test2" };
}
assertThat(context).hasSingleBean(FilterRegistrationBean.class);
Filter filter = context.getBean(FilterRegistrationBean.class).getFilter();
assertThat(filter).isInstanceOf(WebMvcMetricsFilter.class);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilters(filter).build();
for (int i = 0; i < 3; i++) {
mockMvc.perform(MockMvcRequestBuilders.get("/test" + i)).andExpect(status().isOk());
for (String url : urls) {
mockMvc.perform(MockMvcRequestBuilders.get(url)).andExpect(status().isOk());
}
return context.getBean(MeterRegistry.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
*
* @author Jon Schneider
* @author Phillip Webb
* @author Chanhyeong LEE
* @since 2.0.0
*/
public class WebMvcMetricsFilter extends OncePerRequestFilter {
Expand Down Expand Up @@ -123,8 +124,10 @@ private void record(TimingContext timingContext, HttpServletRequest request, Htt
Set<Timed> annotations = getTimedAnnotations(handler);
Timer.Sample timerSample = timingContext.getTimerSample();
if (annotations.isEmpty()) {
Builder builder = this.autoTimer.builder(this.metricName);
timerSample.stop(getTimer(builder, handler, request, response, exception));
if (this.autoTimer.isEnabled()) {
Builder builder = this.autoTimer.builder(this.metricName);
timerSample.stop(getTimer(builder, handler, request, response, exception));
}
return;
}
for (Timed annotation : annotations) {
Expand Down