3.06L Query Prometheus - MartinWong06/grafana GitHub Wiki
-
Open the Prometheus web UI by navigating to the Prometheus URL in your web browser.
-
In the query box, type your PromQL query. For example, if you want to get the CPU usage metric, you can type node_cpu_seconds_total in the query box.
-
After typing your query, press Enter to execute it. Prometheus will return the results of your query in a table.
-
You can add more queries to the same graph by clicking on the “+” icon on the right side of the graph.
-
To filter the results of your query, you can use labels. For example, to filter the CPU usage metric for a specific server, you can use the label cpu or mode. To do this, add the label to your query like this:
node_cpu_seconds_total{cpu="1",mode="iowait"}
-
You can also use functions to manipulate your query results. For example, you can use the sum() function to add up multiple metrics, or the rate() function to calculate the rate of change of a metric.
rate(node_cpu_seconds_total{cpu="1",mode="iowait"}[1m])
-
Query Example
metric_name[label_name=label_value]{time_selector}
-
Here, <metric_name> is the name of the metric you want to select, <label_name> and <label_value> specify the label for the metric, and <time_selector> specifies the time range for which you want to retrieve the data.
-
For example, to select the cpu_usage metric for a specific hostname label over the last 5 minutes, the PromQL query would look like:
node_cpu_seconds_total{cpu="1",mode="iowait"}[5m]
-
Start by selecting the metric you want to aggregate and group. For example, prometheus_http_requests_total.
-
Use an aggregation function such as sum, count, avg, min, max, etc. to calculate a value for the selected metric over a time range. For example,
sum(prometheus_http_requests_total)
-
Use a grouping operator such as by to group the selected metric by one or more labels. For example, sum(prometheus_http_requests_total) by (status_code) will group the metric by status_code label.
-
You can further filter and manipulate the results using other PromQL functions and operators such as rate, increase, offset, etc.
-
Here's an example PromQL query that aggregates and groups the http_requests_total metric by status_code label over the last 5 minutes:
sum(prometheus_http_requests_total{job="prometheus"}) by (status_code)