Writing HTTP Service Discovery

Prometheus provides a generic HTTP Service Discovery, that enables it to discover targets over an HTTP endpoint.

The HTTP Service Discovery is complimentary to the supported service discovery mechanisms, and is an alternative to File-based Service Discovery.

Comparison between File-Based SD and HTTP SD

Here is a table comparing our two generic Service Discovery implementations.

ItemFile SDHTTP SD
Event BasedYes, via inotifyNo
Update frequencyInstant, thanks to inotifyFollowing refresh_interval
FormatYaml or JSONJSON
TransportLocal fileHTTP/HTTPS
SecurityFile-Based securityTLS, Basic auth, Authorization header, OAuth2

Requirements of HTTP SD endpoints

If you implement an HTTP SD endpoint, here are a few requirements you should be aware of.

The response is consumed as is, unmodified. On each refresh interval (default: 1 minute), Prometheus will perform a GET request to the HTTP SD endpoint. The GET request contains a X-Prometheus-Refresh-Interval-Seconds HTTP header with the refresh interval.

The SD endpoint must answer with an HTTP 200 response, with the HTTP Header Content-Type: application/json. The answer must be UTF-8 formatted. If no targets should be transmitted, HTTP 200 must also be emitted, with an empty list []. Target lists are unordered.

Prometheus caches target lists. If an error occurs while fetching an updated targets list, Prometheus keeps using the current targets list. The targets list is not saved across restart. The prometheus_sd_http_failures_total counter metric tracks the number of refresh failures.

The whole list of targets must be returned on every scrape. There is no support for incremental updates. A Prometheus instance does not send its hostname and it is not possible for a SD endpoint to know if the SD requests is the first one after a restart or not.

The URL to the HTTP SD is not considered secret. The authentication and any API keys should be passed with the appropriate authentication mechanisms. Prometheus supports TLS authentication, basic authentication, OAuth2, and authorization headers.

HTTP_SD format

  1. [
  2. {
  3. "targets": [ "<host>", ... ],
  4. "labels": {
  5. "<labelname>": "<labelvalue>", ...
  6. }
  7. },
  8. ...
  9. ]

Examples:

  1. [
  2. {
  3. "targets": ["10.0.10.2:9100", "10.0.10.3:9100", "10.0.10.4:9100", "10.0.10.5:9100"],
  4. "labels": {
  5. "__meta_datacenter": "london",
  6. "__meta_prometheus_job": "node"
  7. }
  8. },
  9. {
  10. "targets": ["10.0.40.2:9100", "10.0.40.3:9100"],
  11. "labels": {
  12. "__meta_datacenter": "london",
  13. "__meta_prometheus_job": "alertmanager"
  14. }
  15. },
  16. {
  17. "targets": ["10.0.40.2:9093", "10.0.40.3:9093"],
  18. "labels": {
  19. "__meta_datacenter": "newyork",
  20. "__meta_prometheus_job": "alertmanager"
  21. }
  22. }
  23. ]