3.06L Query Prometheus - MartinWong06/grafana GitHub Wiki

How do you query metrics in Prometheus?

  1. Open the Prometheus web UI by navigating to the Prometheus URL in your web browser.

  2. 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.

  3. After typing your query, press Enter to execute it. Prometheus will return the results of your query in a table.

  4. You can add more queries to the same graph by clicking on the “+” icon on the right side of the graph.

  5. 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"}
  6. 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])

What is the PromQL syntax for selecting a range of data?

  1. Query Example

    metric_name[label_name=label_value]{time_selector}
  2. 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.

  3. 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]

How can you use PromQL to aggregate and group data in Prometheus?

  1. Start by selecting the metric you want to aggregate and group. For example, prometheus_http_requests_total.

  2. 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)
  3. 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.

  4. You can further filter and manipulate the results using other PromQL functions and operators such as rate, increase, offset, etc.

  5. 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)
⚠️ **GitHub.com Fallback** ⚠️