springmvc集成普罗⽶修斯(prometheus)最新新⼿教程⽬前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>
第⼆步:在l新增servlet
<servlet>
<servlet-name>PrometheusServlet</servlet-name>
<servlet-class>io.porter.MetricsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PrometheusServlet</servlet-name>
<url-pattern>/metrics</url-pattern>
</servlet-mapping>
第三步:新增class
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.hod.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(PrometheusMetricsInterceptor.class);
private static final Histogram requestLatency = Histogram.build()
.name("service_requests_latency_seconds")
.
help("Request latency in seconds.")
springmvc常用标签.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 = Name(request, handler).toLowerCase();
String method = Method().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 (() != null) {
<().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().getName();
} else {
name = RequestURI();
}
} catch (Exception ex) {
<("getName", ex);
} finally {
return name;
}
}
}
第四步:新增prometheus初始化配置
public class PrometheusConfig {
private static Logger logger = Logger(PrometheusConfig.class);
@PostConstruct
public void initialize() {
logger.info("");
DefaultExports.initialize();
logger.info("prometheus has ");
}
}
第五步:在l中新增
注意在schemaLocation中新增,否则识别不了Mvc标签
/schema/mvc
/schema/mvc/spring-mvc-3.0.xsd
<mvc:interceptors>
<bean class="fig.PrometheusMetricsInterceptor"/>
</mvc:interceptors>
第六步:l新增配置
<bean id="prometheusConfig" class="com.fig.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.</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.2</version>
</dependency>
最后查看prometheus页⾯,看到如下配置说明put数据成功
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论