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 tokenDATABASE_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
urlslists the endpoints to scrape. The plugin requests each one on the collection interval and parses the exposition-format response.metric_versionselects one of two metric layouts. Version1, 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, orvalue). Version2stores every sample in a single Telegraf metric namedprometheus, 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_tagrecords 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 1709572230000000000With 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 1709572230000000000Choose a metric version
The two layouts produce different schemas in the destination, so choose the version that fits the destination you write to.
| Destination | metric_version | Why |
|---|---|---|
| InfluxDB 3 (Core, Enterprise, Cloud Dedicated, Cloud Serverless, Clustered) | 1 | Version 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 2 | Either 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.x | 1 or 2 | Either 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 format | Match the output | Set 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
- In Kubernetes, scrape dynamically instead of listing URLs: set
monitor_kubernetes_pods = trueto discover pods by annotation, orkubernetes_servicesfor service endpoints. See the plugin documentation. - To accept pushed Prometheus data instead of scraping, pair
http_listener_v2 with
the Prometheus input data format
or the
Prometheus Remote Write input data format.
Both default to metric version
2, so setprometheus_metric_version = 1when writing to InfluxDB 3. - Histogram and summary metrics accumulate state. If you re-expose scraped metrics for Prometheus to collect, use the prometheus_client output plugin.
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for Telegraf and this documentation. To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support.