@ -32,7 +32,7 @@ import io.micrometer.core.instrument.Timer.Builder;
import io.micrometer.core.instrument.Timer.Sample ;
import io.micrometer.core.instrument.Timer.Sample ;
import org.springframework.boot.actuate.metrics.AutoTimer ;
import org.springframework.boot.actuate.metrics.AutoTimer ;
import org.springframework.boot.actuate.metrics. web.method.HandlerMethod TimedAnnotations;
import org.springframework.boot.actuate.metrics. annotation. TimedAnnotations;
import org.springframework.boot.web.servlet.error.ErrorAttributes ;
import org.springframework.boot.web.servlet.error.ErrorAttributes ;
import org.springframework.http.HttpStatus ;
import org.springframework.http.HttpStatus ;
import org.springframework.web.filter.OncePerRequestFilter ;
import org.springframework.web.filter.OncePerRequestFilter ;
@ -42,8 +42,8 @@ import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.util.NestedServletException ;
import org.springframework.web.util.NestedServletException ;
/ * *
/ * *
* Intercepts incoming HTTP requests and records metrics about Spring MVC execution time
* Intercepts incoming HTTP requests handled by Spring MVC handlers and records metrics
* a nd results .
* a bout execution time a nd results .
*
*
* @author Jon Schneider
* @author Jon Schneider
* @author Phillip Webb
* @author Phillip Webb
@ -128,27 +128,24 @@ public class WebMvcMetricsFilter extends OncePerRequestFilter {
private void record ( TimingContext timingContext , HttpServletRequest request , HttpServletResponse response ,
private void record ( TimingContext timingContext , HttpServletRequest request , HttpServletResponse response ,
Throwable exception ) {
Throwable exception ) {
Object handler = getHandler ( request ) ;
Object handler = getHandler ( request ) ;
Set < Timed > annotations = ( handler instanceof HandlerMethod )
Set < Timed > annotations = getTimedAnnotations ( handler ) ;
? HandlerMethodTimedAnnotations . get ( ( HandlerMethod ) handler ) : Collections . emptySet ( ) ;
Timer . Sample timerSample = timingContext . getTimerSample ( ) ;
Timer . Sample timerSample = timingContext . getTimerSample ( ) ;
if ( annotations . isEmpty ( ) ) {
AutoTimer . apply ( this . autoTimer , this . metricName , annotations ,
if ( this . autoTimer . isEnabled ( ) ) {
( builder ) - > timerSample . stop ( getTimer ( builder , handler , request , response , exception ) ) ) ;
Builder builder = this . autoTimer . builder ( this . metricName ) ;
timerSample . stop ( getTimer ( builder , handler , request , response , exception ) ) ;
}
}
else {
for ( Timed annotation : annotations ) {
Builder builder = Timer . builder ( annotation , this . metricName ) ;
timerSample . stop ( getTimer ( builder , handler , request , response , exception ) ) ;
}
}
}
}
private Object getHandler ( HttpServletRequest request ) {
private Object getHandler ( HttpServletRequest request ) {
return request . getAttribute ( HandlerMapping . BEST_MATCHING_HANDLER_ATTRIBUTE ) ;
return request . getAttribute ( HandlerMapping . BEST_MATCHING_HANDLER_ATTRIBUTE ) ;
}
}
private Set < Timed > getTimedAnnotations ( Object handler ) {
if ( handler instanceof HandlerMethod ) {
HandlerMethod handlerMethod = ( HandlerMethod ) handler ;
return TimedAnnotations . get ( handlerMethod . getMethod ( ) , handlerMethod . getBeanType ( ) ) ;
}
return Collections . emptySet ( ) ;
}
private Timer getTimer ( Builder builder , Object handler , HttpServletRequest request , HttpServletResponse response ,
private Timer getTimer ( Builder builder , Object handler , HttpServletRequest request , HttpServletResponse response ,
Throwable exception ) {
Throwable exception ) {
return builder . tags ( this . tagsProvider . getTags ( request , response , handler , exception ) ) . register ( this . registry ) ;
return builder . tags ( this . tagsProvider . getTags ( request , response , handler , exception ) ) . register ( this . registry ) ;