'How to get all the metrics of an instance with prometheus api?

I want to fetch the monitor host's metrics through the api of prometheus, and I need to initiate a request for each metric requested.

curl http://IP:9090/api/v1/query?query=node_cpu_seconds_total{instance="IP:9100"}

curl http://IP:9090/api/v1/query?query=node_memory_MemTotal_bytes{instance="IP:9100"}

Is there a way to request all the performance data from the monitoring host at once?



Solution 1:[1]

You can fetch all metrics with following curl expression:

url=http://{urIPorhostname}:9090    
curl -s  $url/api/v1/label/__name__/values | jq -r ".data[]" | sort 

Solution 2:[2]

You can request all current node_exporter metrics from a specific machine with the following command:

curl --request GET "http://NODE-EXPORTER-IP:9100/metrics"

Solution 3:[3]

All the metrics for a particular instance IP:9100 can be obtained via the following PromQL query:

{instance="IP:9100"}

This query returns all the metrics, which have the given instance="IP:9100" label. See time series selector docs for more details. See also PromQL tutorial.

The {instance="IP:9100"} query can be sent to the following Prometheus querying APIs:

  • /api/v1/query - this endpoint returns matching time series values at the given timestamp specified via time query arg. For example, curl 'http://prometheus:9090/api/v1/query?query={instance="IP:9100"}'
  • /api/v1/series - this endpoint returns matching time series without any data, e.g. only metric names and labels are returned. For example, curl 'http://prometheus:9090/api/v1/series?match[]={instance="IP:9100"}'
  • /api/v1/query_range - this endpoint returns calculated datapoints for matching time series on the selected time range [start ... end] with the given step interval between samples. See more details about calculated datapoints in these docs.

If you want returning just unique metric names without labels, then you can query /api/v1/label/name/values endpoint: curl http://prometheus:9090/api/v1/label/__name__/values . The __name__ is a special label name used in Prometheus for referring to metric names. Note that multiple time series may share the same metric name. For example, http_requests_total{path="/foo"} and http_requests_total{path="/bar"} time series share http_requests_total metric name, while they differ by path label values.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Boris Ivanov
Solution 2
Solution 3 valyala