'How to add headers(autherization header) in Prometheus.yml file while configuring spring application

  1. Running my application on localhost with this url -> http://localhost:8081/my-service-name
  2. After configuring prometheus, I am getting the metrics on postman with this url -> http://localhost:8081/my-service-name/actuator/prometheus
  3. In postman along with this, there is also one mandatory header key which I've applied when not applied, postman also gives 401 unauthorized error.
  4. This is my prometheus.yml file, which is giving error after authorization in scrape_configs. I am not able to figure out the correct way to provide headers and autherization

prometheus.yml file

 global:
  scrape_interval:     15s
  evaluation_interval: 15s

rule_files:
  # - "first.rules"
  # - "second.rules"

scrape_configs:
  - job_name: prometheus
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']

  - job_name: my-service-name-localhost
    metrics_path: '<my-service-name>/actuator/prometheus'
    authorization:
       [type: 'key']
       [credentials: 'fekvejkvd']
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:8081']

error

level=error msg="Error loading config (--config.file=prometheus.yml)" file=/usr/local/Cellar/prometheus/2.35.0/prometheus.yml err="parsing YAML file prometheus.yml: yaml: line 18: did not find expected key"


Solution 1:[1]

I found the solution here at this post. We do not have the custom header functionality to be added in prometheus. But this can be achieved via proxies or exporters. For more details -> prometheus-scrape-metric-with-custom-header

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aboli Khare