Prometheus 普罗米修斯监控
Posted a java coder
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Prometheus 普罗米修斯监控相关的知识,希望对你有一定的参考价值。
周末在家无聊 看新闻 看到关于监控的东西 拿来玩玩 试一下 感觉还蛮有意思 特此记录一下
这里只介绍客户端的配置
1:首先在POM中添加依赖
<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>
2:在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>
3:添加拦截器class
public class WebInterceptor extends HandlerInterceptorAdapter { private static Logger logger = Logger.getLogger(WebInterceptor.class); private static final String SYSTEM_ID= "BLOAN-GJJ"; private static final String APP_ID= "gjj-ics-credit"; private static final String TYPE = "java"; 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(SYSTEM_ID, APP_ID, TYPE, 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; } } }
4:配置初始化的类
@Component 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..."); } }
5:springmvc.xml中 添加拦截器
<mvc:interceptor> <bean class="com.pingan.credit.interceptor.WebInterceptor"/> </mvc:interceptor>
6:spring-application.xml 初始化 初始化类中的方法
<bean id="prometheusConfig" class="com.pingan.credit.service.PrometheusConfig" init-method="initialize" />
启动tomcat 以及你的project 然后进入localhost:8080/metrics就可以看到JVM的信息了
以上是关于Prometheus 普罗米修斯监控的主要内容,如果未能解决你的问题,请参考以下文章