Kacey Ezerioha
Kacey Ezerioha

Reputation: 1198

Configure basic_auth for Prometheus Target

One of the targets in static_configs in my prometheus.yml config file is secured with basic authentication. As a result, an error of description "Connection refused" is always displayed against that target in the Prometheus Targets' page.

I have researched how to setup prometheus to provide the security credentials when trying to scrape that particular target but couldn't find any solution. What I found was how to set it up on the scrape_config section in the docs. This won't work for me because I have other targets that are not protected with basic_auth. Please help me out with this challenge.

Here is part of my .yml config as regards my challenge.

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:

Upvotes: 26

Views: 63147

Answers (3)

lipka
lipka

Reputation: 1272

I've also had success with using http://username:[email protected]/health as the target for HTTP Basic Authentication.

  - job_name: blackbox
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets:
        - 'http://username:[email protected]/health'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9115

Upvotes: 0

Fran&#231;ois
Fran&#231;ois

Reputation: 2200

In my case, I need to create another job (as specified), but basic_auth needs to be at the same level of indentation as job_name. See example here.

As well, my basic_auth cases require a path as they are not displayed at the root of my domain.

Here is an example with an API endpoint specified:

  - 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'

Upvotes: 53

PatientZro
PatientZro

Reputation: 3

Create another job for the one that needs auth.
So just under what you've posted, do another

  - 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

Upvotes: -3

Related Questions