Scrape Prometheus metrics

Use Telegraf, InfluxDB scrapers, or the prometheus.scrape Flux function to scrape Prometheus-formatted metrics from an HTTP-accessible endpoint and store them in InfluxDB.

Use Telegraf

To use Telegraf to scrape Prometheus-formatted metrics from an HTTP-accessible endpoint and write them to InfluxDB, follow these steps:

  1. Add the Prometheus input plugin to your Telegraf configuration file.
    1. Set the urls to scrape metrics from.
    2. Set the metric_version configuration option to specify which metric parsing version to use (version 2 is recommended).
  2. Add the InfluxDB v2 output plugin to your Telegraf configuration file and configure it to to write to InfluxDB.
Example telegraf.conf
  1. # ...
  2. ## Collect Prometheus formatted metrics
  3. [[inputs.prometheus]]
  4. urls = ["http://example.com/metrics"]
  5. metric_version = 2
  6. ## Write Prometheus formatted metrics to InfluxDB
  7. [[outputs.influxdb_v2]]
  8. urls = ["http://localhost:8086"]
  9. token = "$INFLUX_TOKEN"
  10. organization = "example-org"
  11. bucket = "example-bucket"
  12. # ...

Use an InfluxDB scraper

InfluxDB scrapers automatically scrape Prometheus-formatted metrics from an HTTP-accessible endpoint at a regular interval.

Create a scraper with the InfluxDB API

Use the InfluxDB API /api/v2/scrapers endpoint to manage scrapers and member privileges for scrapers.

To create a scraper, send an HTTP POST request to the /api/v2/scrapers endpoint.

  1. POST http://localhost:8086/api/v2/scrapers

Include the following in your request:

RequirementInclude by
API token with write: orgs/YOUR_ORG_ID/scrapers permissionUse the Authorization header and the Bearer or Token scheme.
OrganizationPass as orgID in the request body.
Scraper namePass as name in the request body.
Format of scraped dataPass as type in the request body. Prometheus data format (prometheus) is the only supported type.
URL endpoint to scrapePass as url in the request body.

Optionally, to force the scraper to skip TLS verification and ignore self-signed certificates at the url, pass "allowInsecure": true in the request body.

Example

  1. curl -v --request POST http://localhost:8086/api/v2/scrapers \
  2. --header "Authorization: Token ${INFLUX_TOKEN}" \
  3. --header 'Content-type: application/json' \
  4. --data-binary @- << JSON
  5. {
  6. "name": "Scraper name",
  7. "type": "prometheus",
  8. "url": "http://localhost:9090/metrics",
  9. "orgID": "${INFLUX_ORG}",
  10. "bucketID": "c55193a037c3b0ca",
  11. "allowInsecure": true
  12. }
  13. JSON

Create a scraper with the InfluxDB UI

To use the InfluxDB UI to manage scrapers, see Scrape data using InfluxDB scrapers.

Use prometheus.scrape()

To use the prometheus.scrape() Flux function to scrape Prometheus-formatted metrics from an HTTP-accessible endpoint and write them to InfluxDB, do the following in your Flux script:

  1. Import the experimental/prometheus package.
  2. Use prometheus.scrape() and provide the URL to scrape metrics from.
  3. Use to() and specify the InfluxDB bucket to write the scraped metrics to.
Example Flux script
  1. import "experimental/prometheus"
  2. prometheus.scrape(url: "http://example.com/metrics")
  3. |> to(bucket: "example-bucket")
  1. (Optional) To scrape Prometheus metrics at regular intervals using Flux, add your Flux scraping script as an InfluxDB task.

For information about scraping Prometheus-formatted metrics with prometheus.scrape(), see Scrape Prometheus metrics with Flux.

prometheus scraper