Prometheus自動發現Exporter方案實現
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 默認爲http
- metrics_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中將匹配組引用({2},…)替換爲它們的值。如果正則表達式不匹配,則不進行替換。
keep:刪除正則表達式與連接的source_labels不匹配的目標。
drop:刪除正則表達式與連接的source_labels匹配的目標。
hashmod:將target_label設置爲連接的source_labels散列的模數。
labelmap:將regex與所有標籤名稱匹配。然後將匹配標籤的值複製到替換中使用匹配組引用({2},…)替換給出的標籤名稱中,替換爲匹配標籤的值
labeldrop:將regex與所有標籤名稱匹配。匹配的任何標籤都將從標籤集中刪除。
labelkeep:將regex與所有標籤名稱匹配。任何不匹配的標籤都將從標籤集中刪除。
5.最終效果
新增的Exporter服務無需再在promtheus做任何配置,普羅可以自動採集相關指標.
訪問prometheus的/targets接口查看所有的exporter信息