当 __address__ 相同并且 Prometheus 是 sidecar 容器时实例标签重写

Instance label rewrite when __address__ is the same and Prometheus is a sidecar container

我在 AWS Lightsail 容器中进行了设置,其中 Prometheus 作为每个应用程序容器的边车容器加载。所以如果我有两个应用程序容器,我也有两个 Prometheus 容器 运行。 (Prometheus 容器使用 remote_write 将指标转发到用于查询的全局 Prometheus 实例。)

每个 Sidecar 容器的 Prometheus 配置如下所示:

global:
  scrape_interval:     15s

scrape_configs:
  - job_name: app
    static_configs:
      - targets:
          - localhost:8080

请注意,每个目标的目标地址都是相同的。

据我了解,instance 标签对于每个目标(?)应该是唯一的,但在这里不是。我如何使用标签重写将它们更改为独特的东西?我有吗?可用作元标签的 IP 地址?我没有使用任何服务发现,例如__meta_lightsail_instance_name 不可用。

我最终通过在 prometheus.yml 全局配置中使用带有环境变量的外部标签解决了这个问题:

global:
  external_labels:
    container: ${HOSTNAME}

${HOSTNAME} env var 由容器上的 ECS Fargate / Lightsail 容器设置。

为此,需要启用一个名为 --enable-feature=expand-external-labels 的功能标志。我的最终 Dockerfile 看起来像这样:

FROM prom/prometheus
ADD prometheus.yml /etc/prometheus/
ENTRYPOINT ["/bin/prometheus"]
CMD ["--config.file=/etc/prometheus/prometheus.yml", \
     "--storage.tsdb.path=/prometheus", \
     "--storage.tsdb.retention.time=1d", \
     "--enable-feature=expand-external-labels"]