Prometheus自动发现Exporter实现方案(一看就懂)

    技术2022-07-10  168

    Prometheus自动发现Exporter方案实现

    1. 基础准备1.1 环境准备1.2 物料准备 2. 注册中心拉起3. 自动发现Exporter模板的拉起3.1 关键配置修改(application-prod.yml)3.2 声明ExporterMetric3.3 书写业务逻辑 4. Prometheus方面的配置4.1 简单自动发现配置4.2 指定服务自动发现4.3 附带服务名标示自动发现 5.最终效果

    1. 基础准备

    1.1 环境准备

    JAVA运行环境

    1.2 物料准备

    基于consul的注册中心或者针对consul做了适配的eureka注册中心 ps:方便大家操作,我已经打包好了注册中心,直接下载就好Eureka-server-0.0.2-RELEASE已经搭建完成的promethues ps: 关于搭建promethues的教程,请自行百度需要自动发现的Exporter ps: 这里我提供了通用的Exporter模板,直接下载就好promethues-demo

    2. 注册中心拉起

    方式1:自己搭建Consul注册中心方式2: 借助提供的Eureka-server-0.0.2-RELEASE.jar完成 ps: 关于如何启动jar包就不详细说明了,默认启动端口为11111

    3. 自动发现Exporter模板的拉起

    3.1 关键配置修改(application-prod.yml)

    cz: swagger: config: app-name: exporter-demo app-version: 0.0.1 app-description: prometheus-exporter的demo app-developer-email: chenz74@chinauicom.cn app-developer-name: XX base-package: com.example eureka: client: serviceUrl: defaultZone: http://10.11.5.51:11111/eureka/ # #健康检查(需要spring-boot-starter-actuator依赖) # healthcheck: # enabled: true instance: # 续约到期时间(默认90秒) lease-expiration-duration-in-seconds: 10 # 续约更新时间间隔(默认30秒) lease-renewal-interval-in-seconds: 10 prefer-ip-address: true #超时时间 feign: httpclient: connection-timeout: 30000 management: endpoints: web: exposure: include: "*" endpoint: info: enabled: true health: show-details: always frequency: '0 0/1 * * * ?' spring: application: name: ${cz.swagger.config.app-name}

    修改defalutZone为注册中心地址,视业务情况修改指标刷新频率frequency,根据exporter功能指定cz开头的服务相关信息。

    3.2 声明ExporterMetric

    MetricFactory.java

    package com.example.promethuesdemo.metric; import io.prometheus.client.CollectorRegistry; import io.prometheus.client.Gauge; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; /** * @author chenzhen * Created by chenzhen on 2020/6/17. */ @Component public class MetricFactory { @Autowired private CollectorRegistry collectorRegistry; @Bean("tcp_avg_client_delay") public Gauge tcpClientDelay(){ return Gauge.build() .name("tcp_avg_client_delay") .labelNames("province","area","center") .help("一分钟内平均(synack和ack之间的时间差,单位ms)") .register(collectorRegistry); } }

    一个指标即为一个bean,多个指标声明多个bean。

    3.3 书写业务逻辑

    Timer.java

    package com.example.promethuesdemo.time; import io.prometheus.client.Gauge; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.logging.Logger; /** * @author chenzhen * Created by chenzhen on 2020/6/23. */ @Component @Slf4j public class Timer { @Qualifier("tcp_avg_client_delay") @Autowired private Gauge tcpAvgClientDelay; @Scheduled(cron = "${frequency}") public void frequencySip(){ log.info("刷新sip指标"); //todo 书写指标获取逻辑 tcpAvgClientDelay.labels("A","B","C").set(1); log.info("刷新完毕"); } }

    4. Prometheus方面的配置

    普罗官方配置详解

    4.1 简单自动发现配置

    - job_name: 'consul-prometheus' scheme: http metrics_path: '/actuator/prometheus' consul_sd_configs: #consul 地址 - server: '10.11.5.51:11111' scheme: http services: []

    关键参数释义

    job_name 作业名,可以理解为组名,其下可以有多个实例配置scheme 默认为httpmetrics_paht 获取到监控指标的接口(Exporter对外暴露的数据接口)consul_sd_configs 自动注册配置server 注册中心地址scheme 交互方式

    4.2 指定服务自动发现

    - job_name: 'consul-prometheus' scheme: http metrics_path: '/actuator/prometheus' consul_sd_configs: #consul 地址 - server: '10.11.5.51:11111' scheme: http services: [SERVIER-A,SERVICE-B]

    关键参数释义

    services 指定使用自动注册的服务名

    4.3 附带服务名标示自动发现

    - job_name: 'consul-prometheus' relabel_configs: - source_labels: [__meta_consul_service_] regex: __meta_consul_service.* replacement: service_name action: labelmap scheme: http metrics_path: '/actuator/prometheus' consul_sd_configs: #consul 地址 - server: '10.11.5.51:11111' scheme: http services: []

    关键参数释义

    relabel_configs relabel_config的作用就是将metrics中 label 的值做一个替换,具体的替换规则由配置决定source_labels 源label,要要操作的指标regex: 正则匹配replacement 替换内容action 行为 ps: replace:将regex与连接的source_labels匹配。然后,将target_label设置为replacement,在replacement中将匹配组引用( 1 , {1}, 1{2},…)替换为它们的值。如果正则表达式不匹配,则不进行替换。 keep:删除正则表达式与连接的source_labels不匹配的目标。 drop:删除正则表达式与连接的source_labels匹配的目标。 hashmod:将target_label设置为连接的source_labels散列的模数。 labelmap:将regex与所有标签名称匹配。然后将匹配标签的值复制到替换中使用匹配组引用( 1 , {1}, 1{2},…)替换给出的标签名称中,替换为匹配标签的值 labeldrop:将regex与所有标签名称匹配。匹配的任何标签都将从标签集中删除。 labelkeep:将regex与所有标签名称匹配。任何不匹配的标签都将从标签集中删除。

    5.最终效果

    新增的Exporter服务无需再在promtheus做任何配置,普罗可以自动采集相关指标. 访问prometheus的/targets接口查看所有的exporter信息

    Processed: 0.014, SQL: 9