如何配置prometheus的Java客户端

prometheus是目前很火的监控系统,其本质上来说是一个时间序列数据库(TSDB),采用golang开发,支持多维度(标签),采用拉取模式,以丰富的export,完善的生态成为目前社区内的监控标配。

这篇文章不说别的,只说java客户端如何埋点。

推荐大家看官方文档:https://github.com/prometheus/client_java

第一步 引入Java客户端(gradle示例)
 

    compile 'io.prometheus:simpleclient:0.5.0'
    compile 'io.prometheus:simpleclient_hotspot:0.5.0'
    compile 'io.prometheus:simpleclient_servlet:0.5.0'
    compile 'io.prometheus:simpleclient_pushgateway:0.5.0'
    compile 'io.prometheus:simpleclient_spring_web:0.5.0'
    compile group: 'io.prometheus', name: 'simpleclient_spring_boot', version: '0.5.0'


第二步 引入新的endpoint
如果是spring mvc项目:

<servlet>
   <servlet-name>MetricsServlet</servlet-name>
   <servlet-class>io.prometheus.client.exporter.MetricsServlet</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>MetricsServlet</servlet-name>
   <url-pattern>/prometheus</url-pattern>
</servlet-mapping>


如果是springboot项目:

在application.java入口类中加入注解@EnablePrometheusEndpoint

@SpringBootApplication
@EnablePrometheusEndpoint
public class SpringbootdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }
}


注意,如果有shiro或者openidFilter等拦截,需要将/prometheus加入到开放列表中

第三步 打开JVM监控参数


在服务器启动类中增加如下代码,则打开了JVM监控参数

DefaultExports.initialize();


第四步 埋点的四种写法


prometheus支持四种数据结构的监控:

计数器

 //Count的用法
    public final static Counter httpRequestsTotal = Counter.build()
            .name("http_requests_total")
            .help("Total number of http requests by response status code")
            .labelNames("标签1", "标签2")
            .register();
    public void doCount(){
        //增加
        httpRequestsTotal.labels("标签1", "标签2").inc();

        //业务代码....
    }


仪表盘
   

 //仪表盘的用法
    public final static Gauge gauge = Gauge.build()
            .name("thread_num")
            .help("线程池激活数")
            .labelNames("标签1", "标签2")
            .register();

    public void doGauge(){
        //增加
        gauge.labels("标签1", "标签2").inc();
        //减少
        gauge.labels("标签1", "标签2").dec();

    }


直方图
   

//直方图的用法
    public final static Histogram httpRequestDurationMs = Histogram.build()
            .name("http_request_duration_milliseconds")
            .help("Http request latency histogram")
            .exponentialBuckets(25, 2, 7)
            .labelNames("标签1", "标签2")
            .register();
    public void doHistogram(){

        //第一种写法,使用计时器计算延迟
        //声明timer
        Histogram.Timer timer = httpRequestDurationMs.labels("标签1", "标签2").startTimer();

        //业务代码....

        //计时结束
        timer.observeDuration();

        //第二种写法:直接记录值
        //业务代码....

        httpRequestDurationMs.labels("标签1", "标签2").observe(10);
    }


合计
   

 //总计的用法
    public final static  Summary summary = Summary.build()
            .name("http_request_duration_milliseconds")
            .help("Http request latency histogram")
            .labelNames("标签1", "标签2")
            .register();

    public void doSummary(){
        //第一种写法,使用计时器计算延迟
        //声明timer
        Summary.Timer timer = summary.labels("标签1", "标签2").startTimer();
        //计时结束
        timer.observeDuration();

        //第二种写法:直接记录值
        summary.labels("标签1", "标签2").observe(10);
    }


第五步 访问endpoint验证端点


访问http://yourapplication/prometheus,有如下类似数据返回即为埋点成功


--------------------- 
作者:frog4 
来源:CSDN 
原文:https://blog.csdn.net/frog4/article/details/84579165 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值