Skip to content

Commit 68acc41

Browse files
committed
Merge pull request #19981 from babjo
* pr/19981: Polish "Stop time web metrics when autotime is disabled" Stop time web metrics when autotime is disabled Closes gh-19981
2 parents fa239a0 + 10643b7 commit 68acc41

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/TestController.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

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

19+
import io.micrometer.core.annotation.Timed;
20+
1921
import org.springframework.web.bind.annotation.GetMapping;
2022
import org.springframework.web.bind.annotation.RestController;
2123

@@ -24,6 +26,7 @@
2426
*
2527
* @author Dmytro Nosan
2628
* @author Stephane Nicoll
29+
* @author Chanhyeong LEE
2730
*/
2831
@RestController
2932
public class TestController {
@@ -43,4 +46,10 @@ public String test2() {
4346
return "test2";
4447
}
4548

49+
@Timed
50+
@GetMapping("test3")
51+
public String test3() {
52+
return "test3";
53+
}
54+
4655
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import java.util.Collection;
1920
import java.util.Collections;
2021
import java.util.EnumSet;
2122

@@ -24,6 +25,7 @@
2425
import javax.servlet.http.HttpServletRequest;
2526
import javax.servlet.http.HttpServletResponse;
2627

28+
import io.micrometer.core.instrument.Meter;
2729
import io.micrometer.core.instrument.MeterRegistry;
2830
import io.micrometer.core.instrument.Tag;
2931
import io.micrometer.core.instrument.Timer;
@@ -64,12 +66,13 @@
6466
* @author Dmytro Nosan
6567
* @author Tadaya Tsuyukubo
6668
* @author Madhura Bhave
69+
* @author Chanhyeong LEE
6770
*/
6871
@ExtendWith(OutputCaptureExtension.class)
6972
class WebMvcMetricsAutoConfigurationTests {
7073

71-
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner().with(MetricsRun.simple())
72-
.withConfiguration(AutoConfigurations.of(WebMvcMetricsAutoConfiguration.class));
74+
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
75+
.with(MetricsRun.simple()).withConfiguration(AutoConfigurations.of(WebMvcMetricsAutoConfiguration.class));
7376

7477
@Test
7578
void backsOffWhenMeterRegistryIsMissing() {
@@ -157,6 +160,19 @@ void autoTimeRequestsCanBeConfigured() {
157160
});
158161
}
159162

163+
@Test
164+
void timerWorksWithTimedAnnotationsWhenAutoTimeRequestsIsFalse() {
165+
this.contextRunner.withUserConfiguration(TestController.class)
166+
.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class, WebMvcAutoConfiguration.class))
167+
.withPropertyValues("management.metrics.web.server.request.autotime.enabled=false").run((context) -> {
168+
MeterRegistry registry = getInitializedMeterRegistry(context, "/test3");
169+
Collection<Meter> meters = registry.get("http.server.requests").meters();
170+
assertThat(meters).hasSize(1);
171+
Meter meter = meters.iterator().next();
172+
assertThat(meter.getId().getTag("uri")).isEqualTo("/test3");
173+
});
174+
}
175+
160176
@Test
161177
@SuppressWarnings("rawtypes")
162178
void longTaskTimingInterceptorIsRegistered() {
@@ -168,12 +184,17 @@ void longTaskTimingInterceptorIsRegistered() {
168184
}
169185

170186
private MeterRegistry getInitializedMeterRegistry(AssertableWebApplicationContext context) throws Exception {
187+
return getInitializedMeterRegistry(context, "/test0", "/test1", "/test2");
188+
}
189+
190+
private MeterRegistry getInitializedMeterRegistry(AssertableWebApplicationContext context, String... urls)
191+
throws Exception {
171192
assertThat(context).hasSingleBean(FilterRegistrationBean.class);
172193
Filter filter = context.getBean(FilterRegistrationBean.class).getFilter();
173194
assertThat(filter).isInstanceOf(WebMvcMetricsFilter.class);
174195
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilters(filter).build();
175-
for (int i = 0; i < 3; i++) {
176-
mockMvc.perform(MockMvcRequestBuilders.get("/test" + i)).andExpect(status().isOk());
196+
for (String url : urls) {
197+
mockMvc.perform(MockMvcRequestBuilders.get(url)).andExpect(status().isOk());
177198
}
178199
return context.getBean(MeterRegistry.class);
179200
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilter.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,6 +48,7 @@
4848
*
4949
* @author Jon Schneider
5050
* @author Phillip Webb
51+
* @author Chanhyeong LEE
5152
* @since 2.0.0
5253
*/
5354
public class WebMvcMetricsFilter extends OncePerRequestFilter {
@@ -139,13 +140,15 @@ private void record(TimingContext timingContext, HttpServletRequest request, Htt
139140
Set<Timed> annotations = getTimedAnnotations(handler);
140141
Timer.Sample timerSample = timingContext.getTimerSample();
141142
if (annotations.isEmpty()) {
142-
Builder builder = this.autoTimer.builder(this.metricName);
143-
timerSample.stop(getTimer(builder, handler, request, response, exception));
144-
return;
145-
}
146-
for (Timed annotation : annotations) {
147-
Builder builder = Timer.builder(annotation, this.metricName);
148-
timerSample.stop(getTimer(builder, handler, request, response, exception));
143+
if (this.autoTimer.isEnabled()) {
144+
Builder builder = this.autoTimer.builder(this.metricName);
145+
timerSample.stop(getTimer(builder, handler, request, response, exception));
146+
}
147+
} else {
148+
for (Timed annotation : annotations) {
149+
Builder builder = Timer.builder(annotation, this.metricName);
150+
timerSample.stop(getTimer(builder, handler, request, response, exception));
151+
}
149152
}
150153
}
151154

0 commit comments

Comments
 (0)