Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:
like image 608
Kacey Ezerioha Avatar asked Sep 23 '20 15:09

Kacey Ezerioha


People also ask

How do you set the Prometheus target?

Install [prometheus-node-exporter] package that includes function to get general resource on the System like CPU or Memory usage on the Node you'd like to add. Add setting on Prometheus Server Config. Access to the Prometheus Web UI and click [Status] - [Targets] to verify new nodes are listed.

How do I set authentication on Prometheus?

To successfully access Prometheus endpoints using basic auth, for example the /metrics endpoint, supply the proper username using the -u flag and supply the password when prompted: curl -u admin http://localhost:9090/metrics Enter host password for user 'admin':

How do I enable TLS in Prometheus?

You need to set both the tls_config part of the configuration and the scheme to https . You can see the full tls_config client options in the Prometheus configuration. If visit locally https://localhost:9090/targets with your browser, you should see https://localhost:9090/metrics in the list of targets.


1 Answers

I would like to add more details to the @PatientZro answer.

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'

Best,

like image 166
François Avatar answered Oct 05 '22 07:10

François