【metrics】将dropwizard metrics收集到 prometheus

本文档介绍了如何将Dropwizard Metrics无缝对接到Prometheus监控系统中,避免引入InfluxDB,直接使用Prometheus的数据源。首先,你需要添加`simpleclient_dropwizard`依赖。然后,通过`DropwizardExports`将MetricRegistry注册到Prometheus的默认收集器。此外,文章还展示了如何提供一个HTTP接口来格式化输出Prometheus能识别的数据,并提供了通过Pushgateway推送数据到Prometheus的选项。最后,总结了Prometheus对Dropwizard Metrics的转换逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

原先用 dropwizard metrics 都是自己看json的没有接入一个图形化监控的服务,网上查到都是 dropwizard metrics+InfluxDB的方式接入grafana,由于新项目走的prometheus数据源,不想再增加维护复杂度了,发现prometheus有支持dropwizard,这里记录一下集成步骤


步骤

1.引入依赖

dropwizard metric 的依赖这里就不写了

		<!-- dropwizard metric to prometheus -->
        <dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_dropwizard</artifactId>
            <version>yourversion</version>
        </dependency>

2.代码

将你项目里的 MetricRegistry 通过 DropwizardExports 注册到 prometheus的默认收集器

// 导入
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;

// 逻辑
CollectorRegistry.defaultRegistry.register(new DropwizardExports(metrics));

我用的是prometheus server提供的http采集的方式,所以需要提供格式化输出的接口
tips:也可以通过pushgateway的方式推送到prometheus server

通过 prometheus 的 TextFormat 对数据进行格式化并写入Writer

// 导入
import io.prometheus.client.exporter.common.TextFormat;

// 逻辑
List<String> nameList = hp.getRequestParams("name");
Set<String> includedParam = nameList.isEmpty() ? Collections.emptySet() : new HashSet<>(nameList);
StringWriter stringWriter = new StringWriter();
TextFormat.write004(stringWriter, CollectorRegistry.defaultRegistry.filteredMetricFamilySamples(includedParam));

总结

prometheus提供了对dropwizard metrics的转换逻辑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值