spring boot 项目 prometheus 自定义指标收集和 grafana 查询--方法耗时分位数指标

news2024/9/22 9:32:37

auth

  1. @author JellyfishMIX - github / blog.jellyfishmix.com
  2. LICENSE LICENSE-2.0

说明

  1. 网上有很多 promehteus 和 grafana 配置,本文不再重复,只介绍自定义部分。
  2. 目前只介绍了分位数指标的收集和查询,常用于方法耗时的指标监控。

自定义指标收集

仅引入以下依赖,只能看到 spring actuator 相关指标,看不到自定义指标。

            <!-- spring-boot-actuator 依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
                <version>2.7.18</version>
            </dependency>
            <!-- prometheus 依赖,和 spring boot 版本需要搭配。spring boot 2.7 搭配 1.10.x 如需升级或降级 spring boot,此依赖 version 可以对应加减 0.1.0-->
            <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-registry-prometheus</artifactId>
                <version>1.10.6</version>
            </dependency>

application.properties 配置

根据需要自定义调整

spring.application.name=spring-boot-explore
server.port=8083
server.servlet.context-path=/explore
# ip:port/actuator/prometheus
management.server.port=9051
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}

自定义指标的收集需要引入额外依赖

            <!--自定义 prometheus 指标依赖-->
            <dependency>
                <groupId>io.prometheus</groupId>
                <artifactId>simpleclient</artifactId>
                <version>0.16.0</version>
            </dependency>
            <dependency>
                <groupId>io.prometheus</groupId>
                <artifactId>simpleclient_hotspot</artifactId>
                <version>0.16.0</version>
            </dependency>
            <dependency>
                <groupId>io.prometheus</groupId>
                <artifactId>simpleclient_servlet</artifactId>
                <version>0.16.0</version>
            </dependency>

指标收集接口

按照 prometheus 的约定,客户端需要暴露一个接口供收集自定义指标。

import io.prometheus.client.exporter.MetricsServlet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jellyfishmix
 * @date 2024/9/1 08:03
 */
@Controller
@RequestMapping("/prometheus")
public class PrometheusExportController extends MetricsServlet {

    @RequestMapping("/exportMetric")
    @ResponseBody
    public void exportMetric(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doGet(request, response);
    }
}

暴露后的自定义指标收集端口,路径是自己配置的:

image-20240901103532161

分位数指标

  1. prometheus 四种 metrics 类型中,如果不是对性能特别敏感的场景,推荐使用 summary。详情阅读:
    1. summary 和 histogram 指标的简单理解 https://blog.csdn.net/wtan825/article/details/94616813
    2. prometheus 四种 metric 类型介绍 https://prometheus.wang/promql/prometheus-metrics-types.html

使用 summary 监控方法耗时

import com.google.common.base.Stopwatch;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Summary;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author jellyfishmix
 * @date 2024/1/3 23:18
 */
@RequestMapping("/test")
@Controller
public class TestController {
    private static final CollectorRegistry DEFAULT_PROMETHEUS_REGISTRY = CollectorRegistry.defaultRegistry;
    private static final Summary DEMO_SUMMARY = Summary.build()
            .name("TestController_compute_summary_demo")
            .help("demo of summary")
            .labelNames("labelName1", "labelNameB")
            .quantile(0.5, 0.01)
            .quantile(0.90, 0.01)
            .quantile(0.99, 0.01)
            .register(DEFAULT_PROMETHEUS_REGISTRY);

    @RequestMapping("/saySummary")
    @ResponseBody
    public String saySummary() {
        Stopwatch stopwatch = Stopwatch.createStarted();
        simulateInterfaceCall();
        var costMillis = stopwatch.elapsed().toMillis();
        DEMO_SUMMARY.labels("abc", "123").observe(costMillis);
        return "hello summary";
    }

    private static void simulateInterfaceCall() {
        // 模拟接口调用的随机耗时
        int randomDelay = ThreadLocalRandom.current().nextInt(100, 1000);
        try {
            TimeUnit.MILLISECONDS.sleep(randomDelay);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
quantile 方法
  1. 说明一下 Summary.build().quantile() 方法。
  2. .50 分位,误差 0.01,会把 [.49, .51] 范围内的指标计入 .50 分位,由于 summary 会在客户端把指标数记录下来,因此允许的误差越多,可以节约的内存占用越多。
  3. 其他分位以此类推。
# .50 分位,误差 0.01
.quantile(0.5, 0.01)
# .90 分位,误差 0.01
.quantile(0.90, 0.01)
# .99 分位,误差 0.01
.quantile(0.99, 0.01)

quantile 方法的详细说明可见 io.prometheus.client.Summary 的类注释,这里摘抄一段:

The Summary class provides different utility methods for observing values, like observe(double), startTimer() and Summary. Timer. observeDuration(), time(Callable), etc.
By default, Summary metrics provide the count and the sum. For example, if you measure latencies of a REST service, the count will tell you how often the REST service was called, and the sum will tell you the total aggregated response time. You can calculate the average response time using a Prometheus query dividing sum / count.
In addition to count and sum, you can configure a Summary to provide quantiles:
  Summary requestLatency = Summary. build()
      .name("requests_latency_seconds")
      .help("Request latency in seconds.")
      .quantile(0.5, 0.01)    // 0.5 quantile (median) with 0.01 allowed error
      .quantile(0.95, 0.005)  // 0.95 quantile with 0.005 allowed error
      // ...
      .register();
  
As an example, a 0.95 quantile of 120ms tells you that 95% of the calls were faster than 120ms, and 5% of the calls were slower than 120ms.
Tracking exact quantiles require a large amount of memory, because all observations need to be stored in a sorted list. Therefore, we allow an error to significantly reduce memory usage.
In the example, the allowed error of 0.005 means that you will not get the exact 0.95 quantile, but anything between the 0.945 quantile and the 0.955 quantile.
Experiments show that the Summary typically needs to keep less than 100 samples to provide that precision, even if you have hundreds of millions of observations.
labelNames 方法

说明一下 Summary.build().labelNames() 方法,表示为此指标设置两个 label,分别命名为 labelName1 和 labelNameB,

.labelNames("labelName1", "labelNameB")

如果设置了 Summary.build().labelNames(),不能直接调用 summary.observe(),会抛 NullPointerException

  // Convenience methods.
  /**
   * Observe the given amount on the summary with no labels.
   * @param amt in most cases amt should be &gt;= 0. Negative values are supported, but you should read
   *            <a href="https://prometheus.io/docs/practices/histograms/#count-and-sum-of-observations">
   *            https://prometheus.io/docs/practices/histograms/#count-and-sum-of-observations</a> for
   *            implications and alternatives.
   */
  public void observe(double amt) {
    noLabelsChild.observe(amt);
  }

需要调用 summary.labels(“abc”, “123”).observe(),labels 方法中的值表示构造 summary 指标时对应的 labelName 的值。

    @RequestMapping("/saySummary")
    @ResponseBody
    public String saySummary() {
        Stopwatch stopwatch = Stopwatch.createStarted();
        simulateInterfaceCall();
        var costMillis = stopwatch.elapsed().toMillis();
        DEMO_SUMMARY.labels("abc", "123").observe(costMillis);
        return "hello summary";
    }

summary 分位数指标效果示例

image-20240901103720431

grafana 视图

grafana query 填写示例如下,注意正确的分位数查询写法是如下图红圈所示,在 metric 位置填写 quantile = 0.5(客户端收集时填写的具体分位数)。

Screenshot 2024-09-01 at 11.41.23

分位数查询错误示例: operations 中填写 quantile 是错误的写法,可以看到图中,通过 operations 计算出的和真实值差距很大。

Screenshot 2024-09-01 at 11.48.24

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2094413.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python函数(进程和线程)

Python基础语法文章导航&#xff1a; Python基础&#xff08;01初识数据类型&变量&#xff09;Python基础&#xff08;02条件&循环语句&#xff09;Python基础&#xff08;03字符串格式化&运算符&进制&编码&#xff09;Python基础&#xff08;04 基础练习…

【媒体人必备】免费的 AI 配音神器,还有黑神话悟空的配音

【媒体人必备】免费的 AI 配音神器&#xff0c;还有黑神话悟空的配音 在短视频风靡全球的时代&#xff0c;TikTokVoice 提供了一个功能强大、操作简单的在线文字转语音工具&#xff0c;支持多种语言和热门AI配音角色。本文将带你了解这个工具的独特之处&#xff0c;以及如何利用…

【计算机组成原理】计算机系统的层次结构——计算机软件

计算机系统的层次结构 导读一、计算机软件的分类二、计算机语言三、计算机系统的层次结构3.1 从计算机语言的角度来理解多级层次结构3.2 计算机层次之间的关系3.3 指令集体系结构&#xff08;ISA&#xff09; 结语 导读 大家好&#xff0c;很高兴又和大家见面啦&#xff01;&a…

Redis(13)| 主从复制

关键词&#xff1a;主从复制&#xff0c;主从数据一致性&#xff0c;同步 带着问题阅读 主从复制解决了什么问题&#xff1f;主从复制的原理&#xff08;过程&#xff09;是什么&#xff1f;主从切换时是如何减少数据丢失的 前言 我在前已经给大家图解了 AOF 和 RDB&#x…

创新之光闪耀,点赋科技在第十三届创新创业大赛中绽放光彩

近日&#xff0c;第十三届创新创业大赛决赛落下帷幕&#xff0c;这场充满激情与挑战的赛事吸引了众多优秀企业参与角逐。在激烈的竞争中&#xff0c;点赋科技脱颖而出&#xff0c;荣获第三名的佳绩。 创新创业大赛一直是企业展示实力、交流创新理念的重要平台。本次大赛中&…

p2p、分布式,区块链笔记: Merkle-DAG和Merkle-Tree的区别与联系

Merkle-DAG和Merkle-Tree的区别与联系 结构: Merkle-Tree 是一种二叉树结构&#xff0c;每个非叶子节点是其子节点哈希的哈希。它具有层次结构&#xff0c;通常用于验证数据的完整性。Merkle-DAG&#xff08;有向无环图&#xff09;是一种更通用的图结构&#xff0c;其一个节点…

142. Go操作Kafka(confluent-kafka-go库)

文章目录 Apache kafka简介开始使用Apache Kafka构建生产者构建消费者 总结 之前已经有两篇文章介绍过 Go如何操作 kafka 28.windows安装kafka&#xff0c;Go操作kafka示例&#xff08;sarama库&#xff09; 51.Go操作kafka示例&#xff08;kafka-go库&#xff09; Apache ka…

django外键表查询

Django外键&#xff08;ForeignKey&#xff09;操作以及related_name的作用-CSDN博客 django模型中外键操作_django的model的contain外键-CSDN博客 通过基本表可以查外键表 删基本表可以删外键表

【Redis】Redis 持久化 AOF、RDB—(七)

目录 一、AOF 日志二、RDB 内存快照 Redis 一旦服务器宕机&#xff0c;内存中的数据将全部丢失&#xff0c;从后端数据库恢复这些数据&#xff0c;对数据库压力很大&#xff0c;且性能肯定比不上从 Redis 中读取&#xff0c;会拖慢应用程序。所以&#xff0c;对 Redis 来说&…

临时性解决斐讯K3 路由器端口转发限制

几年前&#xff0c;原来买的斐讯路由器被我折腾坏掉了。然后那时候刚好K3出来。差不多2000块&#xff0c;因为之前的一个路由器顺利下车&#xff0c;然后就傻傻的上了K3的车。结局&#xff0c;你懂的。 最近因为需要&#xff0c;在折腾远程办公&#xff0c;大概目的就是方便连…

Python | Leetcode Python题解之第386题字典序排数

题目&#xff1a; 题解&#xff1a; class Solution:def lexicalOrder(self, n: int) -> List[int]:ans [0] * nnum 1for i in range(n):ans[i] numif num * 10 < n:num * 10else:while num % 10 9 or num 1 > n:num // 10num 1return ans

pycharm破解教程

下载pycharm https://www.jetbrains.com/pycharm/download/other.html 破解网站 https://hardbin.com/ipfs/bafybeih65no5dklpqfe346wyeiak6wzemv5d7z2ya7nssdgwdz4xrmdu6i/ 点击下载破解程序 安装pycharm 自己选择安装路径 安装完成后运行破解程序 等到Done图标出现 选择Ac…

数据安全法实施三周年 | 天空卫士引领关键技术突破

2024.09.01星期日 三年前的今天数据安全法正式实施&#xff0c;标志着我国数据安全防护体系迈入了一个崭新的发展阶段。 《数据安全法》提出&#xff1a;国家建立数据分类分级保护制度&#xff0c;对数据实行分类分级保护。建立健全全流程数据安全管理制度&#xff0c;组织开展…

NumPy实现线性回归

1 单变量线性回归 1.1 sklearn实现(最小二乘法) import osimport pandas as pd import matplotlib.pyplot as plt import syscurrent_diros.getcwd() pathcurrent_dir\\"Salary Data.csv"def plot_data(path):tablepd.read_csv(path)experience table["Exper…

六、Selenium操作指南(三)

文章目录 七、模拟鼠标操作&#xff08;一&#xff09;左键 click()&#xff08;二&#xff09;右键 context_click()&#xff08;三&#xff09;双击 double_click()&#xff08;四&#xff09;拖拽 drag_and_drop(source,target)&#xff08;五&#xff09;悬停 move_to_elem…

python-禁止抽烟

题目描述 小理的朋友有 n 根烟&#xff0c;他每吸完一根烟就把烟蒂保存起来&#xff0c;k&#xff08; k>1&#xff09;个烟蒂可以换一个新的烟&#xff0c;那么小理的朋友最终能吸到多少根烟呢&#xff1f; 与某些脑筋急转弯不同的是&#xff0c;小理的朋友并不能从异次元借…

AI 通过python脚本自动化导出交易软件某一天的分笔成交明细

一.背景需求 打开交易软件,我们想要导出非今日的日线股票成交分笔明细,其实,很麻烦的。你得在日线图上点击某一天的柱状图,然后双击,就会出现当日的成交明细,然后导出。如果你想到导出30天或者1年的数据呢?你难道盯着电脑一步一步的操作?不,我不允许你还不知道用pytho…

应急响应-爆破漏洞应急响应流程(以SSH爆破为例)

目录 概述研判分析登录成功登录失败历史命令authorized_keys 定损止损攻击链路还原清理恢复总结复盘参考 概述 爆破漏洞是比较常见漏洞&#xff0c;端口开放&#xff0c;管理后台没有做登录频率限制等情况都可能遭受到爆破攻击&#xff0c;本文以SSH爆破为例&#xff0c;介绍下…

【SpringCloud Alibaba】(十二)学习 Sleuth + ZipKin

目录 1、ZipKin 核心架构1.1、ZipKin 概述1.2、ZipKin 核心架构 2、集成 ZipKin2.1、下载安装 ZipKin 服务端2.2、集成 ZipKin 客户端 3、ZipKin 数据持久化3.1、ZipKin 数据持久化到 MySQL 在前面整合 Sleuth 实现链路追踪时&#xff0c;我们是通过查看日志的情况来了解系统调…

【书生大模型实战营】进阶岛 第2关 Lagent 自定义你的 Agent 智能体

文章目录 【书生大模型实战营】进阶岛 第2关 Lagent 自定义你的 Agent 智能体学习任务Lagent 介绍环境配置Lagent Web Demo 使用基于 Lagent 自定义智能体 【书生大模型实战营】进阶岛 第2关 Lagent 自定义你的 Agent 智能体 学习任务 使用 Lagent 自定义一个智能体&#xff…