目前springboot项目比较流行,对于spring mvc的集成prometheus介绍的比较少,正好最近做了个项目,把流程和遇到的坑整理下, 如能帮助大家就最好了。话不多少,直接上流程。

第一步新增依赖:

<prometheus.version>0.2.0</prometheus.version>

<dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient</artifactId> <version>${prometheus.version}</version> </dependency> <!-- Hotspot JVM metrics--> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_hotspot</artifactId> <version>${prometheus.version}</version> </dependency> <!-- Exposition servlet--> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_servlet</artifactId> <version>${prometheus.version}</version> </dependency> <!-- Pushgateway exposition--> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_pushgateway</artifactId> <version>${prometheus.version}</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_spring_web</artifactId> <version>${prometheus.version}</version> </dependency>

第二步:在web.xml新增servlet

<servlet>

<servlet-name>PrometheusServlet</servlet-name>

<servlet-class>io.prometheus.client.exporter.MetricsServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>PrometheusServlet</servlet-name>

<url-pattern>/metrics</url-pattern>

</servlet-mapping>

第三步:新增拦截器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<Histogram.Timer> timerThreadLocal;

@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/schema/mvc

http://www.springframework/schema/mvc/spring-mvc-3.0.xsd

<mvc:interceptors>

<bean class="com.xx.config.PrometheusMetricsInterceptor"/>

</mvc:interceptors>

第六步:applicationcontext.xml新增配置

<bean id="prometheusConfig" class="com.ly.dc.tcyarn.config.PrometheusConfig" init-method="initialize" />

相关报错经验:

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

新增依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.5.2</version>
</dependency>

最后查看prometheus页面,看到如下配置说明put数据成功

更多推荐

spring mvc集成普罗米修斯(prometheus)最新教程