为Prometheus Target配置basic_auth

问题描述

我的prometheus.yml配置文件static_configs中的目标之一是通过基本身份验证来保护的。结果,在Prometheus目标页面上总是显示对该目标的描述“连接被拒绝”错误

我研究了如何设置prometheus来在尝试抓取特定目标时提供安全凭证,但是找不到任何解决方案。我发现的是如何在文档的scrape_config部分中进行设置。这对我不起作用,因为我还有其他不受basic_auth保护的目标。 请帮助我应对这一挑战。

关于我的挑战,这是我的.yml配置的一部分。

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s
    scrape_timeout: 5s

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      
      - targets: ['localhost:5000']
        labels: 
          service: 'Auth'
      - targets: ['localhost:5090']
        labels:
          service: 'Approval'
      - targets: ['localhost:6211']
        labels:
          service: 'Credit Assessment'
      - targets: ['localhost:6090']
        labels:
          service: 'Sweep'
      - targets: ['localhost:6500']
        labels:

解决方法

为需要身份验证的工作创建另一个工作。
因此,在您发布的内容下,再做一次

  - job_name: 'prometheus-basic_auth'
    scrape_interval: 5s
    scrape_timeout: 5s
    static_configs:
      
      - targets: ['localhost:5000']
        labels: 
          service: 'Auth'
        basic_auth:
          username: foo
          password: bar
,

我想在@PatientZro答案中添加更多详细信息。

就我而言,我需要创建另一个作业(如指定的),但是basic_auth需要与job_name处于相同的缩进级别。参见example here

同样,我的basic_auth案例也需要一个路径,因为它们不会显示在域的根目录中。

以下是指定了API端点的示例:

  - job_name: 'myapp_health_checks'
    scrape_interval: 5m
    scrape_timeout: 30s
    static_configs:
        - targets: ['mywebsite.org']
    metrics_path: "/api/health"
    basic_auth:
      username: '[email protected]'
      password: 'cfgqvzjbhnwcomplicatedpasswordwjnqmd'

最好