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"
			},
======

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

  • 10
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: Prometheus 是一款开源的监控系统,用于采集和存储时间序列数据(TSDB),用于监控和警报。Docker SD 配置是一种可以将 Prometheus 服务发现应用到 Docker 指标数据的方式,可以方便地对 Docker 容器进行监控。Docker SD 配置的详细信息可以在Prometheus官方文档中找到:https://prometheus.io/docs/prometheus/latest/configuration/configuration/#docker_sd_config ### 回答2: Prometheus是一款开源的监控和警报系统,而docker_sd_config是其中的一个配置选项,用于指定Prometheus如何发现和监控运行在Docker容器中的目标。 在配置文件中,我们可以使用以下格式来定义docker_sd_config: ``` scrape_configs: - job_name: 'docker' static_configs: - targets: ['container1:port1', 'container2:port2'] labels: group: 'app_group' - targets: ['container3:port3'] labels: group: 'another_group' docker_sd_configs: - target: 'unix:///var/run/docker.sock' labels: env: 'production' ``` 在上述配置中,`scrape_configs`是一个列表,用于定义所有需要监控的目标。每个目标都有一个`job_name`来标识,这里我们使用“docker”作为示例。`static_configs`用于定义静态目标,即需要直接指定的Docker容器的地址和端口。每个静态目标都可以定义一些标签,用于在Prometheus中进行过滤和分类。 `docker_sd_configs`是一个列表,用于定义如何通过Docker的服务发现来动态发现和监控容器目标。在示例中,我们使用`unix:///var/run/docker.sock`作为目标,这是Docker守护进程的UNIX套接字文件路径。通过这个配置,Prometheus能够通过查询Docker守护进程来发现运行在容器中的目标,并自动添到监控列表中。 除了目标之外,`docker_sd_configs`也可以定义一些标签,以便在Prometheus中对发现的目标进行额外的过滤和分类。在示例中,我们为这些目标添了一个名为“env”的标签,用于标识目标所处的环境。 通过配置`docker_sd_config`,Prometheus能够方便地自动发现和监控运行在Docker容器中的目标,并为其添所需的标签。这样,我们可以更好地组织和管理我们的监控目标,提供更可靠和高效的监控服务。 ### 回答3: prometheus是一个开源的监测和告警系统,而docker_sd_configprometheus的一种服务发现配置方式。 在prometheus中,服务发现是指自动发现和监测系统中的各个服务和其对应的实例。而docker_sd_config则是prometheus实现在Docker环境中自动发现服务的一种配置方式。 docker_sd_config配置主要包括以下几个关键部分: 1. targets:定义要监测的目标列表,即要监测的docker容器。可以使用通配符或正则表达式进行匹配。例如,可以设置为"docker.*"表示所有以docker开头的容器。 2. labels:标签是对目标的额外描述信息,可以用于标识、过滤和分类目标。可以根据自己的需求定义不同的标签。例如,可以使用标签"environment=production"表示该容器运行在生产环境中。 3. role:角色用于识别容器的作用或身份。可以根据需要定义不同的角色。例如,可以设置为"app"表示该容器是一个应用程序容器。 4. refresh_interval:表示刷新目标列表的时间间隔。可以根据需要设置刷新频率,例如设置为"30s"表示每30秒刷新一次目标列表。 通过docker_sd_config配置,prometheus可以根据定义的规则动态地发现和监测Docker容器。当新的容器被创建或移除时,prometheus会自动更新目标列表,并开始对新的容器进行监测。这种自动发现和监测的方式,使得prometheus可以更灵活和自动化地进行系统监测和告警。同时,docker_sd_config配置也可以根据实际情况灵活地进行调整和修改,以适应不同的需求和环境。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值