Scrape Prometheus endpoints

Scrape applications and exporters that expose Prometheus /metrics endpoints, and write the metrics to InfluxDB 3. This example scrapes a node_exporter instance and an application endpoint.

Configuration

[[inputs.prometheus]]
  ## Endpoints to scrape on each collection interval.
  urls = [
    "http://node1.example.com:9100/metrics",
    "http://app1.example.com:8080/metrics"
  ]

  ## Metric layout. Use 1 (the plugin default) when writing to InfluxDB 3.
  metric_version = 1

  ## Tag metrics with the scraped URL.
  url_tag = "url"

[[outputs.influxdb_v3]]
  urls = ["http://localhost:8181"]
  token = "
AUTH_TOKEN
"
database = "
DATABASE_NAME
"

Replace the following:

  • AUTH_TOKEN: your InfluxDB authorization token
  • DATABASE_NAME: the database to write to

Use metric version 1 with InfluxDB 3

metric_version = 2 writes every scraped Prometheus metric into a single measurement as its own field. In InfluxDB 3, that produces one wide, sparse table in which most columns are null in any given row, and queries against it perform poorly. Set metric_version = 1 when writing to any InfluxDB 3 product. See Choose a metric version.

How it works

  • urls lists the endpoints to scrape. The plugin requests each one on the collection interval and parses the exposition-format response.
  • metric_version selects one of two metric layouts. Version 1, the plugin default, stores each Prometheus metric as its own Telegraf metric, with labels as tags and a field named for the sample type (counter, gauge, or value). Version 2 stores every sample in a single Telegraf metric named prometheus, with labels as tags and the Prometheus metric name as the field key. The right choice depends on where you write the data. See Choose a metric version.
  • url_tag records which endpoint each metric came from.

Example output

With metric_version = 1, each Prometheus metric becomes its own measurement:

node_cpu_seconds_total,cpu=cpu0,mode=idle,url=http://node1.example.com:9100/metrics counter=18472.1 1709572230000000000
node_memory_MemAvailable_bytes,url=http://node1.example.com:9100/metrics gauge=5836500992 1709572230000000000
http_requests_total,method=GET,path=/api/orders,status=200,url=http://app1.example.com:8080/metrics counter=84721 1709572230000000000

With metric_version = 2, the same samples share the prometheus measurement and each Prometheus metric name becomes a field:

prometheus,cpu=cpu0,mode=idle,url=http://node1.example.com:9100/metrics node_cpu_seconds_total=18472.1 1709572230000000000
prometheus,url=http://node1.example.com:9100/metrics node_memory_MemAvailable_bytes=5836500992 1709572230000000000
prometheus,method=GET,path=/api/orders,status=200,url=http://app1.example.com:8080/metrics http_requests_total=84721 1709572230000000000

Choose a metric version

The two layouts produce different schemas in the destination, so choose the version that fits the destination you write to.

Destinationmetric_versionWhy
InfluxDB 3 (Core, Enterprise, Cloud Dedicated, Cloud Serverless, Clustered)1Version 2 puts every Prometheus metric name in one measurement as its own field. In InfluxDB 3, that becomes a wide, sparse table that slows persistence and queries. Version 1 writes each Prometheus metric to its own measurement, which InfluxDB 3 stores as a narrow table.
InfluxDB v2 and InfluxDB Cloud (TSM)1 or 2Either layout works. Use 2 to match the layout that Flux prometheus.scrape() produces and that the Flux experimental/prometheus package expects. See InfluxDB v2 Prometheus metric parsing formats.
InfluxDB 1.x1 or 2Either layout works. Version 2 keeps all metrics in one measurement, so a single InfluxQL query can select several metrics.
Prometheus, through the prometheus_client output plugin or the Prometheus output data formatMatch the outputSet the same metric_version in the input and the prometheus_client output so metrics round-trip without modification. The Prometheus output data format requires version 2.

Input data formats default to version 2

The prometheus, openmetrics, and prometheusremotewrite input data formats default to version 2, and they name the option prometheus_metric_version or openmetrics_metric_version. If you accept pushed Prometheus data with http_listener_v2 and write to InfluxDB 3, set the version to 1 explicitly.

Extend this example


Was this page helpful?

Thank you for your feedback!