prometheus relabel_config 详解加示例

relabel_config配置

Relabeling(重定义标签),是在拉取(scraping)阶段前,修改target和它的labels;
在每个scrape_configs可以定义多个重定义标签的步骤;

默认的, target的job标签设置为配置文件里的job_name的值;
__address__设置为配置里的targets的值;
instance标签的值,是重定义标签操作之后__address__的值
__scheme__和__metrics_path__标签的值,也是从配置里取值的;
__param_<name>标签的值,是传给URL的查询参数<name>的值;

__meta_开头的标签,也可以用于重定义标签;

重定义标签完成后,__开头的标签会被删除;

重定义标签阶段,如果要临时存储值用于下一阶段的处理,使用__tmp开头的标签名,这种标签不会被Prometheus使用;

# 从已有的标签选择值的源标签组;多个标签,使用separator分隔;
# regex匹配源标签里的值
# 动作为replace, keep, 和 drop
[ source_labels: '[' <labelname> [, ...] ']' ]
# 多个源标签的分隔符;
[ separator: <string> | default = ; ]
# replace动作必需,regex匹配到的值,要替换的目标标签;
[ target_label: <labelname> ]
# 正则匹配源标签的值
[ regex: <regex> | default = (.*) ]
# 源标签值取hash的模块;
[ modulus: <uint64> ]
# 要替换的匹配分组号
[ replacement: <string> | default = $1 ]
# 基于正则匹配的动作
[ action: <relabel_action> | default = replace ]

正则匹配是到结尾的, 可以使用.*<regex>.*匹配更多内容

relabel_action

replace: 正则匹配源标签的值用来替换目标标签;如果有replacement,使用replacement替换目标标签;
keep: 如果正则没有匹配到源标签,删除targets 
drop: 正则匹配到源标签,删除targets
hashmod: 设置目标标签值为源标签值的hash值
labelmap: 正则匹配所有标签名; 将匹配的标签的值复制到由replacement提供的标签名
labeldrop: 正则匹配所有标签名;匹配则移除标签;
labelkeep: 正则匹配所有标签名;不匹配的标签会被移除;

示例环境

使用默认的prometheus target和监控指标

默认配置文件

cat prometheus.yml
------
global:
  scrape_interval:     15s
  evaluation_interval: 15s
alerting:
  alertmanagers:
  - static_configs:
    - targets:
rule_files:
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
======

查看target

curl 'http://localhost:9090/api/v1/targets?state=active'
------
{
	"status": "success",
	"data": {
		"activeTargets": [{
			"discoveredLabels": { // 这块只能作为源标签
				"__address__": "localhost:9090",
				"__metrics_path__": "/metrics",
				"__scheme__": "http",
				"job": "prometheus"
			},
			"labels": {
				"instance": "localhost:9090",
				"job": "prometheus"
			},
			"scrapePool": "prometheus",
			"scrapeUrl": "http://localhost:9090/metrics",
			"lastError": "",
			"lastScrape": "2020-04-25T08:42:19.254191755-04:00",
			"lastScrapeDuration": 0.012091634,
			"health": "up"
		}],
		"droppedTargets": []
	}
}
======

relabel_config主要是处理target的labels字段

重载配置命令

# 检查配置
./promtool check config prometheus.yml
# 重载配置;需要以--web.enable-lifecycle启动promethues
curl -X POST http://localhost:9090/-/reload
# 查看使用的配置 
curl http://localhost:9090/api/v1/status/config

添加自定义标签

添加2个自定义标签,用于后续的演示

vi prometheus.yml
------
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
        labels: 
          userLabel1: value1
          userLabel2: value2
======

重载配置后查询

curl 'http://localhost:9090/api/v1/targets?state=active'
------
{
	"status": "success",
	"data": {
		"activeTargets": [{
			"discoveredLabels": {
				"__address__": "localhost:9090",
				"__metrics_path__": "/metrics",
				"__scheme__": "http",
				"job": "prometheus",
				"userLabel1": "value1", //新增
				"userLabel2": "value2"  //新增
			},
			"labels": {
				"instance": "localhost:9090",
				"job": "prometheus",
				"userLabel1": "value1",  //新增
				"userLabel2": "value2"  //新增
			},
            ...
		}],
		"droppedTargets": []
	}
}

替换标签的值

用源标签的值,替换目标标签的值

vi prometheus.yml
------
scrape_configs:
    ...
    relabel_configs:
    - source_labels: [userLabel1] 
      target_label:  userLabel2
      #默认action 是 'replace'
======

重载配置后查询

curl 'http://localhost:9090/api/v1/targets?state=active'
------
			"discoveredLabels": {
				"__address__": "localhost:9090",
				"__metrics_path__": "/metrics",
				"__scheme__": "http",
				"job": "prometheus",
				"userLabel1": "value1", //不变
				"userLabel2": "value2"  //不变
			},
			"labels": {
				"instance": "localhost:9090",
				"job": "prometheus",
				"userLabel1": "value1",
				"userLabel2": "value1"  //用userLabel1的值替换了userLabel2
			},
======

relabel_configs是在拉取(scraping)前,修改target和它的labels

用userLabel1的部分值替换userLabel2

vi prometheus.yml
------
scrape_configs:
    ...
    relabel_configs:
    - source_labels: [userLabel1]
      regex: 'value([0-9]+)'
      target_label:  userLabel2
      replacement: '$1'
      action: replace
======

重载配置后查询

curl 'http://localhost:9090/api/v1/targets?state=active'
------
			"labels": {
				"instance": "localhost:9090",
				"job": "prometheus",
				"userLabel1": "value1",
				"userLabel2": "1"
			},
======

删除匹配的标签

vi prometheus.yml
------
scrape_configs:
    ...
    relabel_configs:
    - regex: userLabel1
      action: labeldrop
======      

重载配置后查询

curl 'http://localhost:9090/api/v1/targets?state=active'
------
{
	"status": "success",
	"data": {
		"activeTargets": [{
			"discoveredLabels": { //这部分只能做为源标签
				"__address__": "localhost:9090",
				"__metrics_path__": "/metrics",
				"__scheme__": "http",
				"job": "prometheus",
				"userLabel1": "value1",
				"userLabel2": "value2"
			},
			"labels": {
				"instance": "localhost:9090",
				"job": "prometheus",
				"userLabel2": "value2" //删除了userLabel1
			},
            ...
	}
}
======

删除匹配的target

vi prometheus.yml
------
scrape_configs:
    ...
    relabel_configs:
    - source_labels: [userLabel1] 
      action: drop
======      

重载配置后查询

curl 'http://localhost:9090/api/v1/targets?state=active'
------
{
	"status": "success",
	"data": {
		"activeTargets": [],
		"droppedTargets": []
	}
}
======

取匹配的标签名的一部分生成新标签

vi prometheus.yml
------
scrape_configs:
    ...
    relabel_configs:
    - regex: user(.*)1
      action: labelmap
======      

重载配置后查询

curl 'http://localhost:9090/api/v1/targets?state=active'
------
			"labels": {
				"Label": "value1", //新生成的标签
				"instance": "localhost:9090",
				"job": "prometheus",
				"userLabel1": "value1",
				"userLabel2": "value2"
			},
======

参考链接
官方配置示例
官方文档

<think>嗯,用户问的是如何配置Prometheus把数据推送到Thanos的Receive组件。首先,我需要回忆一下Prometheus和Thanos的集成方式。Thanos Receive是通过Prometheus的remote write功能来接收数据的,所以关键点在于正确配置Prometheus的remote_write部分。 首先,用户可能需要知道Prometheus的配置文件结构,特别是remote_write的配置项。我需要确认remote_write的URL应该是Thanos Receive的端点,比如http://thanos-receive:10908/api/v1/receive。然后,考虑是否需要启用HTTP服务发现,比如在Kubernetes环境中,可能要用到http_sd_configs来自动发现服务。 接下来,可能需要配置一些参数,比如remote_timeout,通常是30秒或者更久,确保数据能成功发送。还有是否需要添标签,比如tenant_id,这时候可能需要写标签重写规则,使用action: replace或者hashmod之类的动作。 然后,关于身份验证,如果Thanos Receive需要认证,比如Basic Auth或者Bearer Token,这部分也需要在remote_write中配置。例如,basic_auth部分包含username和password,或者authorization的credentials。 另外,用户可能关心如何验证配置是否正确,可能需要指导他们检查Prometheus的日志,或者用curl测试Thanos Receive的连通性。还有,Thanos Receive的配置是否正确,比如端口是否开放,服务是否正常运行。 还有一点是,是否需要配置TLS,如果Thanos Receive启用了HTTPS,Prometheus的remote_write需要配置tls_config,比如insecure_skip_verify设为true,或者指定CA证书。 可能还需要提醒用户,配置完成后要重启Prometheus服务,或者发送SIGHUP信号让配置生效。另外,监控remote write的状态指标,比如prometheus_remote_storage_samples_total,可以帮助确认数据是否正常推送。 总结一下,步骤应该是:配置remote_write指向Thanos Receive的URL,处理可能的认证,设置必要的标签重写,检查网络连通性,配置TLS如果需要,然后验证配置是否正确应用,数据是否正常传输。可能需要给出一个完整的配置示例,涵盖这些方面,让用户可以直接参考修改自己的prometheus.yml文件。</think>以下是配置Prometheus通过Remote Write将数据推送至Thanos Receive的步骤详解: --- ### 一、核心配置框架 在Prometheus的`prometheus.yml`中添`remote_write`模块,基础配置模板如下: ```yaml remote_write: - url: "http://<thanos-receive-service>:<port>/api/v1/receive" # Thanos Receive服务地址 name: thanos-receive # 写入目标名称(可自定义) remote_timeout: 30s # 超时时间 queue_config: max_samples_per_send: 5000 # 单次发送样本数 capacity: 10000 # 队列容量 max_shards: 200 # 最大并发分片数 ``` --- ### 二、关键参数说明 #### 1. 网络配置 - **必须参数**:确认Thanos Receive的地址和端口,例如: ```yaml url: "http://thanos-receive:10908/api/v1/receive" ``` #### 2. 标签重写(可选) 若需添租户标识等元数据: ```yaml write_relabel_configs: - source_labels: [__tenant_id__] target_label: tenant_id action: replace ``` #### 3. 身份认证配置 - **Basic Auth**示例: ```yaml basic_auth: username: <your_username> password: <your_password> ``` - **Bearer Token**示例: ```yaml authorization: credentials: "Bearer <your_token>" ``` #### 4. TLS密配置 ```yaml tls_config: insecure_skip_verify: true # 跳过证书验证(生产环境慎用) # ca_file: /path/to/ca.crt # 正式CA证书路径 ``` --- ### 三、完整配置示例 ```yaml global: scrape_interval: 15s remote_write: - url: "http://thanos-receive:10908/api/v1/receive" name: thanos-stream remote_timeout: 30s queue_config: max_samples_per_send: 5000 capacity: 10000 max_shards: 200 write_relabel_configs: - source_labels: [__tenant_id__] target_label: tenant_id action: replace tls_config: insecure_skip_verify: true scrape_configs: - job_name: "node-exporter" static_configs: - targets: ["node-exporter:9100"] ``` --- ### 四、验证步骤 1. **配置检查**: ```bash promtool check config prometheus.yml ``` 2. **服务重启**: ```bash systemctl restart prometheus # 或发送HUP信号 kill -HUP $(pgrep prometheus) ``` 3. **日志验证**: ```bash journalctl -u prometheus -f | grep "remote write" ``` 4. **指标检查**: 访问`http://prometheus:9090/metrics`,验证以下指标: - `prometheus_remote_storage_samples_total` - `prometheus_remote_storage_failed_samples_total` --- ### 五、排错指南 1. **网络连通性测试**: ```bash curl -v http://thanos-receive:10908/-/healthy ``` 2. **Thanos Receive状态检查**: ```bash thanos receive inspect --endpoint=thanos-receive:10908 ``` 3. **抓包分析**: ```bash tcpdump -i any port 10908 -w thanos_traffic.pcap ``` --- 通过以上配置,Prometheus将通过Remote Write协议将采集数据实时推送至Thanos Receive组件,实现数据的长期存储和分布式架构支持。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值