假设 prom-aggregation-gateway 的地址为 192.168.1.100:8080
,那么在 Prometheus 的配置文件中,需要将原有的 scrape_configs
部分中的 targets
参数修改为该地址。示例配置如下:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'aggregated-metrics'
metrics_path: '/metrics/aggregated'
scrape_interval: 30s
scrape_timeout: 10s
scheme: http
honor_labels: true
params:
match[]:
- '{job=~".+"}'
- '{__name__=~"job:.+"}'
aggregate[]:
- 'job'
- '__name__'
- 'instance'
- 'environment'
- 'region'
- 'zone'
# 修改 targets 参数为 prom-aggregation-gateway 的地址
# 注意:此处需要使用 prom-aggregation-gateway 中定义的 target labels,比如这里是使用了 environment=prod 这个 label。
relabel_configs:
- source_labels : [__meta_consul_service_port]
regex : ".*"
action : replace
target_label : __address__
- source_labels : [__meta_consul_service_address, __meta_consul_service_tags]
regex : "(.*);environment=prod(,.*)?"
replacement : "${1}"
action : replace
target_label : instance
在上面的配置中,我们将 scrape_configs
部分新增了一个名为 aggregated-metrics
的 job,它会通过 HTTP 协议从 prom-aggregation-gateway 上获取聚合后的指标数据,并且使用 relabel_configs
来对获取的 target 进行处理以满足需要。