prometheus使用三(自定义监控指标实现)

摘要:
Prometheus提供了一系列导出,以帮助收集各种容器和中间件的运行指标,但有时我们也需要更灵活的监控指标。本文介绍了用户定义的监控指标。本文用于监控Dubbo服务提供商的调用指标,包括调用次数、p99等

  prometheus提供了一系列的export帮助采集各种容器和中间件的运行指标,但有时我们还需要更灵活的监控指标,介绍一下自定义监控指标

       本文用来监控dubbo的服务提供者的被调用指标,包括调用次数,p99等。

       首先引入jar包

<dependency>

<groupId>io.prometheus</groupId>

<artifactId>simpleclient_httpserver</artifactId>

<version>0.6.0</version>
</dependency>

<dependency>

<groupId>io.prometheus</groupId>

<artifactId>simpleclient_pushgateway</artifactId>

<version>0.7.0</version>
</dependency>

写一个util工具类,里面有四大基本统计类型的用法

import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
import io.prometheus.client.Summary;
import org.springframework.stereotype.Component;

@Component
public class PrometheusUtil {


static final Gauge gauge = Gauge.build()
.name("requests_count")
.labelNames("getCount") //key

.help("requests_count").register(); //累加的count值


//用法: 代码前加入 gauge.labels("get").inc(); //+1 get,value
// gauge.labels("get").dec(); //-1


Summary receivedBytes = Summary.build()
.name("requests_size_bytes")
.labelNames("summaryBytes")
.help("Request size in bytes.").register(); //http请求的字节数

//用法: 方法后执行 receivedBytes.observe(req.size());


static final Summary summaryTime = Summary.build()
.name("requests_summary_seconds")
.labelNames("summaryTime")
.help("Request latency in seconds.").register(); //请求处理的时间

 

static final Histogram hisTimer=

Histogram.build()
.name("histogram")
.labelNames("histogram")
.help("request histogram")
.exponentialBuckets(1,1.2,10).register(); //处理p99


//count+

public static void inc(String lable){

gauge.labels(lable).inc();
}


//请求处理时间开始

public static Summary.Timer summaryTimeStart(String lable){
Summary.Timer timer= summaryTime.labels(lable).startTimer();

return timer;
}


//请求处理时间结束

public static void summaryStop(Summary.Timer timer){
timer.observeDuration();
}


//p99开始

public static Histogram.Timer histogramStart(String lable){
Histogram.Timer timer=hisTimer.labels(lable).startTimer();

return timer;
}


//p99结束

public static void histogramStop(Histogram.Timer timer){
timer.observeDuration();
}

}

 

然后在dubbo服务提供者启动类里开个端口,用来让prometheus访问

public class Provider {

public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/provider.xml");

//模拟调用dubbo service,方便调试,实际使用中不需要

DemoServiceImpl service = context.getBean(DemoServiceImpl.class);

for (int i=0;i<5;i++){
String sayHello = service.sayHello("prometheus");
System.out.println(sayHello);
}

HTTPServer server=new HTTPServer(8080);

context.start();
System.in.read();
}
}

service层如下

@Component

public class DemoServiceImpl implements Service{

@Override
public String sayHello(String name) throws InterruptedException, IOException {
PrometheusUtil.inc("sayHello");

Histogram.Timer timer = PrometheusUtil.histogramStart("sayHello");
Summary.Timer timer1 = PrometheusUtil.summaryTimeStart("sayHello");

  try{
    Random random = new Random(1);
    Thread.sleep(500 + random.nextInt(3000));
   }catch (Exception e){
    System.out.println(e.getMessage());
   }finally {
   PrometheusUtil.histogramStop(timer);
PrometheusUtil.summaryStop(timer1);
}

return "Hello " + name;
}

}

然后在prometheus的prometheus.yml上配置一个job,参数prometheus二中方法配置即可。

然后启动dubbo,再看prometheus的targets有没有监控到

prometheus使用三(自定义监控指标实现)第1张

 已是up状态,有监控到,点击打开查看指标

prometheus使用三(自定义监控指标实现)第2张

 跟预期一样,有指标,然后展示在grafana

自定义一个仪表盘,新建一个panel

prometheus使用三(自定义监控指标实现)第3张

 写好表达式即可展示。同理写出其它几个指标,主要表达式为

平均响应时间 rate(histogram_sum[5m])/rate(histogram_count[5m])

平均qps increase(histogram_count[5m])/300

p90或p99 histogram_quantile(0.9, sum(rate(histogram_bucket[5m])) by (le))  等等 

或微调这些项得到最理想的展示

prometheus使用三(自定义监控指标实现)第4张

 还有其它很多用法,欢迎大家告知。

免责声明:文章转载自《prometheus使用三(自定义监控指标实现)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Oracle 递归查询UVM基础之--------uvm_root下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

prometheus使用四(alertmanager&amp;amp;grafana告警及服务发现)

一.prometheus告警 prometheus有了监控和展示后,我们可以看到各种指标,但没有告警的话还是不方便。 (1)alertmanager告警   1.下载安装 下载地址:https://prometheus.io/download/ tar -xvf alertmanager-0.20.0.linux-amd64.tar.gz alertnat...

使用Prometheus监控bind9的DNS服务

首先编译bind_exporter,编译方式参见bind_exporter 创建一个systemd配置文件来运行bind_exporter vi /etc/systemd/system/bind_exporter.service 内容如下,注意此处的用户和组使用与named程序相同的用户和组“named”。--web.listen-address为对...

K8s容器资源限制

在K8s中定义Pod中运行容器有两个维度的限制:1. 资源需求:即运行Pod的节点必须满足运行Pod的最基本需求才能运行Pod。如: Pod运行至少需要2G内存,1核CPU2. 资源限额:即运行Pod期间,可能内存使用量会增加,那最多能使用多少内存,这就是资源限额。 # kubectl describe node node1.zcf.com...

Grafana+Prometheus:容器化运行Grafana+Prometheus

Grafana官网地址:Grafana 官方文档:Grafana文档 Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知。它主要有以下六大特点: 1、展示方式:快速灵活的客户端图表,面板插件有许多不同方式的可视化指标和日志,官方库中具有丰富的仪表盘插件,比如热图、折线图、图表等多种展示方式; 2、数...

k8s全栈监控之metrics-server和prometheus

一、概述 使用metric-server收集数据给k8s集群内使用,如kubectl,hpa,scheduler等 使用prometheus-operator部署prometheus,存储监控数据 使用kube-state-metrics收集k8s集群内资源对象数据 使用node_exporter收集集群中各节点的数据 使用prometheus收集api...

Prommetheus 插件监控 ES

转载自:https://blog.csdn.net/u010453363/article/details/76689435/ 用prometheus主要是监控elk中的elasticsearch和logstash prometheus的clicent收集数据,并提供http服务,由prometheus server主动pull数据。   1.1 elast...