prometheus在springboot应用中埋点

添加依赖

        <!-- prometheus指标埋点 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

配置application.yml

配置项可参考SpringBoot2.x系列教程之SpringBoot2.x配置大全03。注意的是management.metrics.distribution.sla的新版本将sla -> slo

management:
  endpoints:
    web:
      exposure:
        include: prometheus  # 打开 Prometheus 的 Web 访问 Path
  metrics:
    # 下面选项建议打开,以监控 http 请求的 P99/P95 等,具体的时间分布可以根据实际情况设置
    distribution:
      slo:
        http:
          server:
            requests: 1ms,5ms,10ms,50ms,100ms,200ms,500ms,1s,5s
    # 在 Prometheus 中添加特别的 Labels
    tags:
      # 必须加上对应的应用名,因为需要以应用的维度来查看对应的监控
      application: reptile-data-platform-display

本地验证

  • security 放行 “/actuator/**”
  • 运行项目后访问http://localhost:8080/actuator/prometheus 访问到 Prometheus 协议的指标数据,说明相关的依赖配置已经正确。

例子中配置默认配置,对应的端口和路径以实际项目为准

自定义指标

有下面两种方式,以官方的两个实例讲解

  1. prometheus SDK中自带的Counter、Gauge、Histogram、Summary类型build
  2. 继承Collector 实现collect()方法添加自定义指标
@Component
public class MyPrometheusConfig {
    @Autowired
    private CollectorRegistry collectorRegistry;

    @Bean
    public Counter requestTotalCountCollector() {
        return Counter.build()
                // 用于指定该指标的名称
                .name("http_requests_total")
                // 用于声明该metrics拥有的维度label
                .labelNames("path", "method", "code")
                .help("http请求总计数").register(collectorRegistry);
    }
    @Bean
    @Primary
    public YourCustomCollector yourCustomCollector(){
        return new YourCustomCollector().register(collectorRegistry);
    }
}
public class YourCustomCollector extends Collector{
    @Override
    public List<MetricFamilySamples> collect() {
        List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
        // With no labels.
        mfs.add(new GaugeMetricFamily("my_gauge", "help", 42));
        // 创建metrics指标
        GaugeMetricFamily labeledGauge = new GaugeMetricFamily("my_other_gauge", "help", Arrays.asList("labelname"));
        // 设置指标的label以及value
        labeledGauge.addMetric(Arrays.asList("foo"), new Random().nextInt(5));
        labeledGauge.addMetric(Arrays.asList("bar"), new Random().nextInt(5));
        mfs.add(labeledGauge);
        return mfs;
    }
}

添加这两个类后再/actuator/prometheus中找一下http_requests_total、my_gauge、my_other_gauge这三个指标名称

在prometheus.yml中配置节点

- job_name: 'reptile_node'
    static_configs:
      - targets: ['localhost:8080']
    metrics_path: "/actuator/prometheus"

参考资料

  1. springboot整合prometheus
  2. 警惕 Spring Boot Actuator 引发的安全问题
  3. 自定义Metrics:让Prometheus监控你的应用程序(Spring版)
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脸是真的白

如果对你有用的话,可以支持一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值