I need to scrape Prometheus metrics from an endpoint that requires a custom HTTP header, x-service-token
.
Prometheus does not include an option to scrape using a custom HTTP header, only the Authorization
header.
One user shared a workaround for using nginx to create a reverse proxy
Just in case others come looking here for how to do this (there are at least 2 other issues on it), I've got a little nginx config that works. I'm not an nginx expert so don't mock! ;)
I run it in docker. A forward proxy config file for nginx listening on 9191:
http { map $request $targetport { ~^GET\ http://.*:([^/]*)/ "$1"; } server { listen 0.0.0.0:9191; location / { proxy_redirect off; proxy_set_header NEW-HEADER-HERE "VALUE"; proxy_pass $scheme://$host:$targetport$request_uri; } } } events { }
Run the transparent forward proxy:
docker run -d --name=nginx --net=host -v /path/to/nginx.conf:/etc/nginx/nginx.conf:ro nginx
In your prometheus job (or global) add the
proxy_url
key- job_name: 'somejob' metrics_path: '/something/here' proxy_url: 'http://proxyip:9191' scheme: 'http' static_configs: - targets: - '10.1.3.31:2004' - '10.1.3.31:2005'
Originally posted by @sra in https://github.com/prometheus/prometheus/issues/1724#issuecomment-282418757
I have tried configuring this, but without 'host' networking and using host.docker.internal
instead of localhost, but nginx is not able to connect
nginx | 172.26.0.4 - - [31/Oct/2022:16:07:38 +0000] "GET http://host.docker.internal:8080/actuator/prometheus HTTP/1.1" 502 157 "-" "Prometheus/2.39.1"
This workaround also requires saving the API key in a file, which is not ideal, as this could accidentally be committed to a repo.
Prometheus locked the GitHub issue, so users are not able to ask for help or follow up questions.
There are two other StackOverflow questions on this topic, but the answers do not attempt to provide workarounds: