如何在 prometheus 配置中重新标记 ECS cadvisor 标签?

Posted

技术标签:

【中文标题】如何在 prometheus 配置中重新标记 ECS cadvisor 标签?【英文标题】:How can I relabel ECS cadvisor labels in prometheus config? 【发布时间】:2021-01-21 19:29:29 【问题描述】:

我正在尝试从 prometheus 配置中的 cadvisor 重命名相同的标签。 cadvisor 名字太长了,所以我有以下配置用于重新标记。

  - job_name: 'cadvisor job foo'
scrape_interval: 60s
ec2_sd_configs:
  - role_arn: 'arn:aws:iam::$id:role/foo'
    region: 'us-west-2'
    port: 8484
relabel_configs:
  - source_labels: [__meta_container_label_com_amazonaws_ecs_cluster]
    target_label: cluster_name
  - source_labels: [__meta_container_label_com_amazonaws_ecs_container_name]
    target_label: container_name  

不幸的是,这似乎不起作用。 这甚至可以在 prometheus 配置中重新标记 cadvisor 标签吗?

【问题讨论】:

【参考方案1】:

您确定标签__meta_container_label_com_amazonaws_ecs_cluster 存在吗?我在 AWS ECS 集群中运行 Cadvisor,并从 container_label_com_amazonaws_ecs_container_name 获取容器名称。它不是元标签,而是由 Cadvisor 从 Docker 标签中提取的。

__meta.+ 标签由relabel_configs 检索。这是我的配置部分的样子:


  - job_name: cadvisor
    ec2_sd_configs:
      - region: eu-central-1
        port: 28080
        filters:
          - name: tag:aws:autoscaling:groupName 
            values:
              - data-dev
              - data-prod

    relabel_configs:
      - action: replace
        source_labels: [__meta_ec2_instance_id]
        target_label: instance_id
      - action: replace
        source_labels: [__meta_ec2_instance_state]
        target_label: instance_state
      - action: replace
        source_labels: [__meta_ec2_instance_type]
        target_label: instance_type

    metric_relabel_configs:

      # Drop series.
      - action: drop
        source_labels: [__name__]
        regex: "(container_tasks_state|container_cpu_load_average_10s)"
      
      # Drop all series that do not have a 'name' label.
      - action: keep
        source_labels: [name]
        regex: (.+)

      # Drop all series that do not have a '/ecs/' c group prefix and a proper 
      # container id after the third forword slash.
      - action: keep
        source_labels: [id]
        regex: ^/ecs/.+/.+

      # Drop label 'instance_type'.
      - action: labeldrop
        regex: instance_type

      # Rewrite 'id' label to only include short form container id.
      - action: replace
        source_labels: [id]
        target_label: id
        regex: ^/ecs/.+/(.0,12).*
        replacement: $1

      # Rename label task family.
      - action: replace
        source_labels: [container_label_com_amazonaws_ecs_task_definition_family]
        target_label: task_family
      - action: labeldrop
        regex: container_label_com_amazonaws_ecs_task_definition_family

      # Overwrite 'name' with value of container name label.
      - action: replace
        source_labels: [container_label_com_amazonaws_ecs_container_name]
        target_label: name
      - action: labeldrop
        regex: container_label_com_amazonaws_ecs_container_name

      # namespace
      - action: replace
        source_labels: [container_label_promstack_namespace]
        target_label: namespace
      - action: labeldrop
        regex: container_label_promstack_namespace

      # api_type
      - action: replace
        source_labels: [container_label_promstack_api_type]
        target_label: api_type
      - action: labeldrop
        regex: container_label_promstack_api_type

【讨论】:

以上是关于如何在 prometheus 配置中重新标记 ECS cadvisor 标签?的主要内容,如果未能解决你的问题,请参考以下文章

Prometheus(公制)使用逆正则表达式匹配/负前瞻重新标记配置

在 Prometheus 中将实例重新标记为主机名

prometheus relabel 配置

prometheus relabel 配置

Prometheus - 通过正则表达式聚合和重新标记

如何标记 Prometheus blackbox_exporter 端点