在 Prometheus 配置中添加更多静态实例标签

48次阅读
没有评论

问题描述

正在向 Prometheus 配置中添加服务器,如下所示:

- job_name: cluster1
  static_configs:
    - targets:
      - server1.example.com
      - server2.example.com
      - server3.example.com

现在,用户希望为实例添加额外的静态标签,比如将 server1 和 server2 标记为 “database”,将 server3 标记为 “mail”,并且可能还将 server1 标记为 “mysql”,将 server2 标记为 “postgres”。
用户希望在 Grafana 中使用这些标签进行过滤,例如:

free_space_in_bytes{job=~'$job', server_type='database', database_type='mysql' }

用户想知道是否可以在 Prometheus 配置中实现这一点。过滤作业名称或实例是可行的,但用户希望扩展到更多标签。

解决方案

请注意以下操作注意版本差异及修改前做好备份。
根据 Prometheus 的文档,可以在 static_configs 中指定一组目标和一个公共标签集。这是在抓取配置中指定静态目标的规范方式。
因此,可以按照以下方式修改配置文件:

- job_name: cluster1
  static_configs:
    - targets:
      - server1.example.com
      - server2.example.com
      - server3.example.com
      labels:
        database: mysql
        some_carefully_chosen_property: its_value_for_these_targets

如果你想在本地目录中尝试 prometheus.yml 文件的语法,请运行以下命令:

docker run -it --rm --name prometheus_configcheck \
  -v ${PWD}/prometheus.yml:/prometheus.yml \
  --entrypoint /bin/promtool \
  prom/prometheus:v2.40.6 \
  check config /prometheus.yml

如果你已经在本地安装了 Prometheus,并且带有 promtool,你可以运行以下命令:

promtool check config prometheus.yml

请注意,不要滥用标签,特别是在你完全理解基数的问题以及为什么高基数会过载你的系统,并且在 PromQL 的情况下是不必要的之前,请不要这样做。选择过多的标签或具有高基数或非有界值的标签可能会破坏你的设置,因为每个标签组合都会作为单独的指标流存储。这篇文章很好地解释了这个问题:https://grafana.com/blog/2020/08/27/the-concise-guide-to-labels-in-loki/(虽然是关于 Loki 的,但标签在 Prometheus 中的工作方式相同)。
此外,你的标签值示例表明你可能没有确定你正在讨论的属性(键)。例如,mysql 不是一个有意义的标签值;唯一的值将是 truefalse。尽量找出真正描述你的基础架构的属性,然后不要将它们作为标签添加,而是在 PromQL 查询中使用它们,或者仅将它们适度添加为标签。

正文完