周末在家无聊 看新闻 看到关于监控的东西 拿来玩玩 试一下 感觉还蛮有意思 特此记录一下
这里只介绍客户端的配置
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 {</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Logger logger = Logger.getLogger(WebInterceptor.<span style="color: #0000ff;">class</span><span style="color: #000000;">); </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> String SYSTEM_ID= "BLOAN-GJJ"<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> String APP_ID= "gjj-ics-credit"<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> String TYPE = "java"<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">final</span> Histogram requestLatency =<span style="color: #000000;"> Histogram.build() .name(</span>"service_requests_latency_seconds"<span style="color: #000000;">) .help(</span>"Request latency in seconds."<span style="color: #000000;">) .labelNames(</span>"systemId", "appId", "type", "name", "method"<span style="color: #000000;">).register(); </span><span style="color: #0000ff;">private</span> ThreadLocal<Histogram.Timer><span style="color: #000000;"> timerThreadLocal; @Override </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">boolean</span> preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">super</span><span style="color: #000000;">.preHandle(request, response, handler); } @Override </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception { String name </span>= <span style="color: #0000ff;">this</span><span style="color: #000000;">.getName(request, handler).toLowerCase(); String method </span>=<span style="color: #000000;"> request.getMethod().toUpperCase(); timerThreadLocal </span>= <span style="color: #0000ff;">new</span> ThreadLocal<><span style="color: #000000;">(); timerThreadLocal.set(requestLatency.labels(SYSTEM_ID, APP_ID, TYPE, name, method).startTimer()); </span><span style="color: #0000ff;">super</span><span style="color: #000000;">.postHandle(request, response, handler, modelAndView); } @Override </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception { </span><span style="color: #0000ff;">super</span><span style="color: #000000;">.afterCompletion(request, response, handler, ex); </span><span style="color: #0000ff;">if</span> (timerThreadLocal.get() != <span style="color: #0000ff;">null</span><span style="color: #000000;">) { timerThreadLocal.get().observeDuration(); } } @Override </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception { </span><span style="color: #0000ff;">super</span><span style="color: #000000;">.afterConcurrentHandlingStarted(request, response, handler); } </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String getName(HttpServletRequest request, Object handler) { String name </span>= ""<span style="color: #000000;">; </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">if</span> (handler != <span style="color: #0000ff;">null</span> && handler <span style="color: #0000ff;">instanceof</span><span style="color: #000000;"> HandlerMethod) { HandlerMethod method </span>=<span style="color: #000000;"> (HandlerMethod) handler; String className </span>=<span style="color: #000000;"> ((HandlerMethod) handler).getBeanType().getName(); name </span>= className + "." +<span style="color: #000000;"> method.getMethod().getName(); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { name </span>=<span style="color: #000000;"> request.getRequestURI(); } } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception ex) { logger.error(</span>"getName"<span style="color: #000000;">, ex); } </span><span style="color: #0000ff;">finally</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> name; } }
}
4:配置初始化的类
@Component public class PrometheusConfig {</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Logger logger = Logger.getLogger(PrometheusConfig.<span style="color: #0000ff;">class</span><span style="color: #000000;">); @PostConstruct </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> initialize() { logger.info(</span>"prometheus init..."<span style="color: #000000;">); DefaultExports.initialize(); logger.info(</span>"prometheus has been initialized..."<span style="color: #000000;">); }
}
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" />
7:添加controller
@Controller public class MetricsController { private CollectorRegistry registry;</span><span style="color: #008000;">/**</span><span style="color: #008000;"> * Construct a MetricsServlet for the default registry. </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span><span style="color: #000000;"> MetricsController() { </span><span style="color: #0000ff;">this</span><span style="color: #000000;">(CollectorRegistry.defaultRegistry); } </span><span style="color: #008000;">/**</span><span style="color: #008000;"> * Construct a MetricsServlet for the given registry. </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span><span style="color: #000000;"> MetricsController(CollectorRegistry registry) { </span><span style="color: #0000ff;">this</span>.registry =<span style="color: #000000;"> registry; } @RequestMapping(value</span>="/metrics",method=<span style="color: #000000;"> RequestMethod.GET) </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> metrics(<span style="color: #0000ff;">final</span><span style="color: #000000;"> HttpServletRequest req, </span><span style="color: #0000ff;">final</span> HttpServletResponse resp) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> ServletException, IOException { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(TextFormat.CONTENT_TYPE_004); </span><span style="color: #0000ff;">try</span> (Writer writer =<span style="color: #000000;"> resp.getWriter()) { TextFormat.write004(writer, registry.filteredMetricFamilySamples(parse(req))); writer.flush(); } } </span><span style="color: #0000ff;">private</span> Set<String><span style="color: #000000;"> parse(HttpServletRequest req) { String[] includedParam </span>= req.getParameterValues("name[]"<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span> (includedParam == <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> Collections.emptySet(); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> HashSet<><span style="color: #000000;">(Arrays.asList(includedParam)); } }
}
启动tomcat 以及你的project 然后进入localhost:8080/metrics就可以看到JVM的信息了
原文地址:https://www.cnblogs.com/showme1942/p/7457451.html