prometheus java 客户端使用
所属分类 prometheus
浏览量 3406
https://prometheus.io/docs/instrumenting/clientlibs/
https://blog.csdn.net/frog4/article/details/84579165
io.prometheus
simpleclient
0.6.0
io.prometheus
simpleclient_servlet
0.6.0
web.xml 增加 MetricsServlet 暴露指标信息,供 server抓取
MetricsServlet
io.prometheus.client.exporter.MetricsServlet
MetricsServlet
/metrics
prom server 默认使用 /metrics 抓取指标
springboot项目 使用注解暴露端点
@EnablePrometheusEndpoint
http://127.0.0.1:8080/metrics
prom server 查看 targets
/targets
指标定义封装
四种指标类型 Counter Gauge Summary Histogram
PromClientUtil
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
import io.prometheus.client.Summary;
public abstract class PromClientUtil {
public final static Counter invokeCount = buildInvokeCount();
public final static Gauge dataMapSize = buildDataMapSize();
public final static Summary dataMapSizeSummary = buildDataMapSizeSummary();
public final static Histogram dataMapSizeHistogram = buildDataMapSizeHistogram();
private static Counter buildInvokeCount(){
return Counter.build()
.name("invokeCount")
// 需要设置help细腻 ,否则启动报错
// java.lang.IllegalStateException: Help hasn't been set.
.help("invokeCount")
//.labelNames("label1", "label2")
.register();
}
private static Gauge buildDataMapSize(){
return Gauge.build()
.name("dataMapSize")
.help("data map size")
//.labelNames("label1", "label2")
.register();
}
private static Summary buildDataMapSizeSummary(){
Summary summary = Summary.build()
.name("dataMapSizeSummary")
.help("dataMapSizeSummary")
//.labelNames("标签1", "标签2")
.register();
return summary;
}
private static Histogram buildDataMapSizeHistogram(){
Histogram hist = Histogram.build()
.name("dataMapSizeHistogram")
.help("dataMapSizeHistogram")
//.exponentialBuckets(25, 2, 7)
//.labelNames("标签1", "标签2")
.register();
return hist;
}
}
代码埋点采集指标
PromClientUtil.invokeCount.inc();
PromClientUtil.dataMapSize.set(dataMapSize);
PromClientUtil.dataMapSizeSummary.observe(dataMapSize);
PromClientUtil.dataMapSizeHistogram.observe(dataMapSize);
上一篇
下一篇
软件架构的10个常见模式
prometheus术语
prometheus指标类型
prometheus使用jmx_exporter监控jvm
循环引用导致fastjson序列化与反序列化StackOverflowError
go语言defer机制