Merge pull request #10906 from jkschneider/micrometer-rc3
* gh-10906: Polish Micrometer 1.0.0-rc.3 upgrade Upgrade to Micrometer 1.0.0-rc.3pull/10921/merge
commit
144625022c
@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2017 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.actuate.autoconfigure.metrics.export;
|
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
|
|
||||||
import org.springframework.core.convert.converter.Converter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A {@link Converter} to create a {@link Duration} from a {@link String}.
|
|
||||||
*
|
|
||||||
* @author Jon Schneider
|
|
||||||
* @author Andy Wilkinson
|
|
||||||
* @since 2.0.0
|
|
||||||
*/
|
|
||||||
@ConfigurationPropertiesBinding
|
|
||||||
public class StringToDurationConverter implements Converter<String, Duration> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Duration convert(String source) {
|
|
||||||
return Duration.parse(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.actuate.autoconfigure.metrics.export.simple;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import io.micrometer.core.instrument.simple.SimpleConfig;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.autoconfigure.metrics.export.PropertiesConfigAdapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adapter to convert {@link SimpleProperties} to a {@link SimpleConfig}.
|
||||||
|
*
|
||||||
|
* @author Jon Schneider
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
public class SimplePropertiesConfigAdapter extends
|
||||||
|
PropertiesConfigAdapter<SimpleProperties, SimpleConfig> implements SimpleConfig {
|
||||||
|
private static final SimpleConfig DEFAULTS = (key) -> null;
|
||||||
|
|
||||||
|
public SimplePropertiesConfigAdapter(SimpleProperties properties) {
|
||||||
|
super(properties, DEFAULTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String get(String k) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean enabled() {
|
||||||
|
return get(SimpleProperties::getEnabled, SimpleConfig::enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Duration step() {
|
||||||
|
return get(SimpleProperties::getStep, SimpleConfig::step);
|
||||||
|
}
|
||||||
|
}
|
@ -1,52 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2017 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.actuate.metrics.web.servlet;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Intercepts incoming HTTP requests and records metrics about execution time and results.
|
|
||||||
*
|
|
||||||
* @author Jon Schneider
|
|
||||||
* @since 2.0.0
|
|
||||||
*/
|
|
||||||
public class MetricsHandlerInterceptor extends HandlerInterceptorAdapter {
|
|
||||||
|
|
||||||
private final WebMvcMetrics webMvcMetrics;
|
|
||||||
|
|
||||||
public MetricsHandlerInterceptor(WebMvcMetrics webMvcMetrics) {
|
|
||||||
this.webMvcMetrics = webMvcMetrics;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
|
||||||
Object handler) throws Exception {
|
|
||||||
this.webMvcMetrics.preHandle(request, handler);
|
|
||||||
return super.preHandle(request, response, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
|
||||||
Object handler, Exception ex) throws Exception {
|
|
||||||
this.webMvcMetrics.record(request, response, ex);
|
|
||||||
super.afterCompletion(request, response, handler, ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.actuate.metrics.web.servlet;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
import org.springframework.web.servlet.HandlerExecutionChain;
|
||||||
|
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
|
||||||
|
import org.springframework.web.servlet.handler.MatchableHandlerMapping;
|
||||||
|
import org.springframework.web.util.NestedServletException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Intercepts incoming HTTP requests and records metrics about Spring MVC execution time
|
||||||
|
* and results.
|
||||||
|
*
|
||||||
|
* @author Jon Schneider
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
|
public class WebMvcMetricsFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory
|
||||||
|
.getLogger(WebMvcMetricsFilter.class);
|
||||||
|
|
||||||
|
private final WebMvcMetrics webMvcMetrics;
|
||||||
|
|
||||||
|
private final HandlerMappingIntrospector mappingIntrospector;
|
||||||
|
|
||||||
|
public WebMvcMetricsFilter(WebMvcMetrics webMvcMetrics,
|
||||||
|
HandlerMappingIntrospector mappingIntrospector) {
|
||||||
|
this.webMvcMetrics = webMvcMetrics;
|
||||||
|
this.mappingIntrospector = mappingIntrospector;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean shouldNotFilterAsyncDispatch() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request,
|
||||||
|
HttpServletResponse response, FilterChain filterChain)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
HandlerExecutionChain handlerExecutionChain = getHandlerExecutionChain(request);
|
||||||
|
Object handler = (handlerExecutionChain == null ? null
|
||||||
|
: handlerExecutionChain.getHandler());
|
||||||
|
filterWithMetrics(request, response, filterChain, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
private HandlerExecutionChain getHandlerExecutionChain(HttpServletRequest request) {
|
||||||
|
try {
|
||||||
|
MatchableHandlerMapping matchableHandlerMapping = this.mappingIntrospector
|
||||||
|
.getMatchableHandlerMapping(request);
|
||||||
|
return (matchableHandlerMapping == null ? null
|
||||||
|
: matchableHandlerMapping.getHandler(request));
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Unable to time request", ex);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void filterWithMetrics(HttpServletRequest request,
|
||||||
|
HttpServletResponse response, FilterChain filterChain, Object handler)
|
||||||
|
throws IOException, ServletException, NestedServletException {
|
||||||
|
this.webMvcMetrics.preHandle(request, handler);
|
||||||
|
try {
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
// When an async operation is complete, the whole filter gets called again
|
||||||
|
// with isAsyncStarted = false
|
||||||
|
if (!request.isAsyncStarted()) {
|
||||||
|
this.webMvcMetrics.record(request, response, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (NestedServletException ex) {
|
||||||
|
this.webMvcMetrics.record(request, response, ex.getCause());
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
53
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/MetricsHandlerInterceptorAutoTimedTests.java → spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilterAutoTimedTests.java
53
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/MetricsHandlerInterceptorAutoTimedTests.java → spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilterAutoTimedTests.java
Loading…
Reference in New Issue