首页  

prometheus java 客户端使用     所属分类 prometheus 浏览量 3316
https://prometheus.io/docs/instrumenting/clientlibs/

https://blog.csdn.net/frog4/article/details/84579165




<dependency>
  <groupId>io.prometheus</groupId>
  <artifactId>simpleclient</artifactId>
  <version>0.6.0</version>
</dependency>

<dependency>
  <groupId>io.prometheus</groupId>
  <artifactId>simpleclient_servlet</artifactId>
  <version>0.6.0</version>
</dependency>






web.xml 增加 MetricsServlet 暴露指标信息,供 server抓取



<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>/metrics</url-pattern>
</servlet-mapping>




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机制