• 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>
    web.xml

    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&lt;Histogram.Timer&gt;<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&lt;&gt;<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>
                    &amp;&amp; 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;
        }
    }
    

    }

    View Code

    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;">);
    }
    

    }

    PrometheusConfig.class

    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&lt;String&gt;<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&lt;&gt;<span style="color: #000000;">(Arrays.asList(includedParam));
        }
    }
    

    }

    复制代码

    启动tomcat 以及你的project 然后进入localhost:8080/metrics就可以看到JVM的信息了

    原文地址:https://www.cnblogs.com/showme1942/p/7457451.html
  • 相关阅读:
    如何提升程序员的工作效率?
    MacOS 上网络故障诊断
    阅读混淆过的Android代码的确不易
    复旦投毒案落下帷幕
    正确把握深度和广度
    Freemarker的数据模型使用
    xilink se14.7 win10闪退
    浅谈 pid的原理与差异
    win10系统激活
    stm8 同时使用dac和adc 采集异常,电平异常
  • 原文地址:https://www.cnblogs.com/jpfss/p/11887467.html
Copyright © 2020-2023  润新知