如何配置prometheus的Java客户端

9 篇文章 1 订阅

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,有如下类似数据返回即为埋点成功

image-20181128094541890

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Prometheus是一个开源的监控系统和时间序列数据库,它可以用于记录和查询各种应用程序的指标数据。Prometheus提供了多种客户端库,其中包括Java客户端库,用于在Java应用程序中集成和使用PrometheusPrometheus Java客户端库是一个用于在Java应用程序中生成和暴露指标数据的库。它提供了一组简单的API,可以用于定义和注册指标,以及在应用程序中收集和导出指标数据。该库还提供了一些注解和工具,可以帮助开发人员更轻松地将指标集成到他们的应用程序中。 使用Prometheus Java客户端库,您可以轻松地将自定义指标集成到您的Java应用程序中,并使用Prometheus服务器进行监控和查询。您可以定义各种类型的指标,例如计数器、直方图和摘要,并使用适当的方法来更新和导出这些指标。 以下是使用Prometheus Java客户端库的一些基本步骤: 1. 添加依赖:在您的Java项目中添加Prometheus Java客户端库的依赖。 2. 定义指标:使用库提供的API定义您想要监控的指标。 3. 注册指标:将指标注册到Prometheus客户端。 4. 更新指标:在应用程序中根据需要更新指标的值。 5. 导出指标:将指标数据导出到Prometheus服务器。 通过将Prometheus Java客户端库集成到您的Java应用程序中,您可以方便地监控和查询各种应用程序的指标数据,并根据需要进行报警和分析。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值