目前springboot项目比较流行,对于spring mvc的集成prometheus介绍的比较少,正好最近做了个项目,把流程和遇到的坑整理下, 如能帮助大家就最好了。话不多少,直接上流程。
第一步新增依赖:
第二步:在web.xml新增servlet
第三步:新增拦截器class
package com.xx.xx.config;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
import io.prometheus.client.Summary;
import org.apache.log4j.Logger;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 郭飞
*/
public class PrometheusMetricsInterceptor extends HandlerInterceptorAdapter {
private static Logger logger = Logger.getLogger(PrometheusMetricsInterceptor.class);
private static final Histogram requestLatency = Histogram.build()
.name("service_requests_latency_seconds")
.help("Request latency in seconds.")
.labelNames("systemId", "appId", "type", "name", "method").register();
private ThreadLocal
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
String name = this.getName(request, handler).toLowerCase();
String method = request.getMethod().toUpperCase();
timerThreadLocal = new ThreadLocal<>();
timerThreadLocal.set(requestLatency.labels(name, method).startTimer());
super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
super.afterCompletion(request, response, handler, ex);
if (timerThreadLocal.get() != null) {
timerThreadLocal.get().observeDuration();
}
}
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
super.afterConcurrentHandlingStarted(request, response, handler);
}
private String getName(HttpServletRequest request, Object handler) {
String name = "";
try {
if (handler != null
&& handler instanceof HandlerMethod) {
HandlerMethod method = (HandlerMethod) handler;
String className = ((HandlerMethod) handler).getBeanType().getName();
name = className + "." + method.getMethod().getName();
} else {
name = request.getRequestURI();
}
} catch (Exception ex) {
logger.error("getName", ex);
} finally {
return name;
}
}
}
第四步:新增prometheus初始化配置
public class PrometheusConfig {
private static Logger logger = Logger.getLogger(PrometheusConfig.class);
@PostConstruct
public void initialize() {
logger.info("prometheus init...");
DefaultExports.initialize();
logger.info("prometheus has been initialized...");
}
}
第五步:在spring-mvc.xml中新增拦截器
注意在schemaLocation中新增,否则识别不了Mvc标签
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
第六步:applicationcontext.xml新增配置
相关报错经验:
1.java.lang.NoClassDefFoundError: org/springframework/core/ResolvableTypeProvider
spring-core包版本冲突,4.1.5.RELESASE升级为4.3.16.RELEASE,完美
2.Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/util/DefaultIndenter
新增依赖
com.fasterxml.jackson.core
jackson-core
2.5.2
最后查看prometheus页面,看到如下配置说明put数据成功