|
32 | 32 | import io.micrometer.core.instrument.Timer.Sample;
|
33 | 33 |
|
34 | 34 | import org.springframework.boot.actuate.metrics.AutoTimer;
|
35 |
| -import org.springframework.boot.actuate.metrics.web.method.HandlerMethodTimedAnnotations; |
| 35 | +import org.springframework.boot.actuate.metrics.annotation.TimedAnnotations; |
36 | 36 | import org.springframework.boot.web.servlet.error.ErrorAttributes;
|
37 | 37 | import org.springframework.http.HttpStatus;
|
38 | 38 | import org.springframework.web.filter.OncePerRequestFilter;
|
|
42 | 42 | import org.springframework.web.util.NestedServletException;
|
43 | 43 |
|
44 | 44 | /**
|
45 |
| - * Intercepts incoming HTTP requests and records metrics about Spring MVC execution time |
46 |
| - * and results. |
| 45 | + * Intercepts incoming HTTP requests handled by Spring MVC handlers and records metrics |
| 46 | + * about execution time and results. |
47 | 47 | *
|
48 | 48 | * @author Jon Schneider
|
49 | 49 | * @author Phillip Webb
|
@@ -128,27 +128,24 @@ private Throwable fetchException(HttpServletRequest request) {
|
128 | 128 | private void record(TimingContext timingContext, HttpServletRequest request, HttpServletResponse response,
|
129 | 129 | Throwable exception) {
|
130 | 130 | Object handler = getHandler(request);
|
131 |
| - Set<Timed> annotations = (handler instanceof HandlerMethod) |
132 |
| - ? HandlerMethodTimedAnnotations.get((HandlerMethod) handler) : Collections.emptySet(); |
| 131 | + Set<Timed> annotations = getTimedAnnotations(handler); |
133 | 132 | Timer.Sample timerSample = timingContext.getTimerSample();
|
134 |
| - if (annotations.isEmpty()) { |
135 |
| - if (this.autoTimer.isEnabled()) { |
136 |
| - Builder builder = this.autoTimer.builder(this.metricName); |
137 |
| - timerSample.stop(getTimer(builder, handler, request, response, exception)); |
138 |
| - } |
139 |
| - } |
140 |
| - else { |
141 |
| - for (Timed annotation : annotations) { |
142 |
| - Builder builder = Timer.builder(annotation, this.metricName); |
143 |
| - timerSample.stop(getTimer(builder, handler, request, response, exception)); |
144 |
| - } |
145 |
| - } |
| 133 | + AutoTimer.apply(this.autoTimer, this.metricName, annotations, |
| 134 | + (builder) -> timerSample.stop(getTimer(builder, handler, request, response, exception))); |
146 | 135 | }
|
147 | 136 |
|
148 | 137 | private Object getHandler(HttpServletRequest request) {
|
149 | 138 | return request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE);
|
150 | 139 | }
|
151 | 140 |
|
| 141 | + private Set<Timed> getTimedAnnotations(Object handler) { |
| 142 | + if (handler instanceof HandlerMethod) { |
| 143 | + HandlerMethod handlerMethod = (HandlerMethod) handler; |
| 144 | + return TimedAnnotations.get(handlerMethod.getMethod(), handlerMethod.getBeanType()); |
| 145 | + } |
| 146 | + return Collections.emptySet(); |
| 147 | + } |
| 148 | + |
152 | 149 | private Timer getTimer(Builder builder, Object handler, HttpServletRequest request, HttpServletResponse response,
|
153 | 150 | Throwable exception) {
|
154 | 151 | return builder.tags(this.tagsProvider.getTags(request, response, handler, exception)).register(this.registry);
|
|
0 commit comments