Prometheus scrape /metric with custom header

I have an application that will be monitored by Prometheus, but the application need the custom header key like :

x-auth-token: <customrandomtoken>

What should I do with prometheus.yml?


Solution 1:

Prometheus itself does not have a way to define custom headers in order to reach an exporter. The idea of adding the feature was discussed in this GitHub issue. Tl;dr: if you need a custom header, inject it with a forward or a reverse proxy.

The prometheus-blackbox-exporter tag suggest that the question is about the exporter that makes probes, which is a separate thing and it does have a way to set headers. Only, it does not scrape metrics, it makes them.

Blackbox exporter has it's own configuration file and it consists of modules. A module is a set of parameters, defining how to do the probe and what result to expect. Here is an example of a module that looks for 200-299 response code and uses X-Auth-Token header:

modules:
  http_2xx_with_header:
    prober: http
    http:
      headers:
        X-Auth-Token: skdjfh98732hjf22exampletoken

More examples can be found here and the list of configuration options - here.

When you made the blackbox exporter to load the new configuration, you need to adjust Prometheus configuration as well:

scrape_configs:
  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [http_2xx_with_header]  # <- Here goes the name of the new module
    static_configs:
      - targets:
        - http://prometheus.io
    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