Prometheus中Summary使用 java示例
时间: 2024-05-29 07:07:40 浏览: 17
Prometheus中的Summary是一种用于记录数据分布情况的指标类型,它会记录样本数、样本值总和、样本值的分位数等信息。下面是一个使用Java编写的Summary示例代码:
```java
import io.prometheus.client.Summary;
import io.prometheus.client.exporter.HTTPServer;
import java.io.IOException;
public class SummaryExample {
static final Summary requestsLatency =
Summary.build()
.name("requests_latency_seconds")
.help("Request latency in seconds.")
.quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error
.quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error
.quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error
.register();
public static void main(String[] args) throws IOException {
HTTPServer server = new HTTPServer(8080);
while (true) {
double latency = Math.random() * 10;
requestsLatency.observe(latency);
System.out.println("Latency: " + latency);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
在这个示例中,我们定义了一个名为`requests_latency_seconds`的Summary指标,并且设置了三个分位数:50%、90%和99%。在`main`函数中,我们随机生成一个请求的延迟时间,并将其记录到Summary中,然后休眠1秒钟。最后,我们启动了一个HTTPServer,以便可以使用Prometheus来抓取并处理这个指标。你可以根据自己的需要修改和扩展这个示例。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)