prometheus使用三(自定義監控指標實現)


  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有沒有監控到

 

 已是up狀態,有監控到,點擊打開查看指標

 

 跟預期一樣,有指標,然后展示在grafana

自定義一個儀表盤,新建一個panel

 

 寫好表達式即可展示。同理寫出其它幾個指標,主要表達式為

平均響應時間 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))  等等 

或微調這些項得到最理想的展示

 

 還有其它很多用法,歡迎大家告知。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM