How to write the query statement PromQl of prometheus

Prometheus data features:

Prometheus stores the pulled data in its own DB, which is called TSDB (Time Series Database). Each indicator corresponds to a hash value as the time point changes, thus forming a time series, and multiple indicators constitute Multiple time series, users can extract indicator data and display charts according to their own needs;
insert image description here

In the prometheus expression language, there are four types:

instant vector: A set of time series containing a single sample of each time series, sharing the same timestamp.
Range vector: A set of time series containing data points for each time series over time.
Scalar: a simple numeric floating point value
String (String): a simple string value (currently not used)
insert image description here

Prometheus provides a functional expression language that allows users to select and aggregate time series data. The results of expression query can be drawn as a graph, or displayed as a table in the expression browser provided by prometheus, or can be called and used by an external system with HTTP API.

Expression is the writing specification of PromQl:

Prometheus uniquely defines a time series based on the metrics name and associated label set

  • The name of the indicator represents the basic characteristic identification of a certain type of measurable attribute on the monitoring target
  • Labels are multiple measurable dimensions subdivided from this basic feature
    PromQl is the built-in data query language of prometheus Server

How to use PromQl?

Open the web page of prometheus, and enter the indicator name in the Expression box:

insert image description here

insert image description here

Data filtering:

Equivalent query:

Click excute to get the query results. If you want to further filter the data and narrow the scope, you can add brackets {} after the metric name and add label attributes in the brackets to filter: for example, node_cpu_seconds_total{cpu
="0"} is Select the data of the node_cpu_seconds_total indicator and the cpu is 0:
insert image description here

unequal query

When filtering with tags, you can also use inequality queries:
insert image description here

offset modifier

The offset modifier allows to change the time offset of a single instant vector and range vector in the query.
For example:
node_cpu_seconds_total{cpu="0", mode=~"s.*"}offset 5m
is to view the value of the indicator 5m ago:
insert image description here

Applications of regular expressions

Use =~"expression" to match regular expressions
such as: node_cpu_seconds_total{cpu="0", mode=~"s.*"} is to select all samples whose CPU is 0 and whose mode value starts with s value:
insert image description here

Queries for range vectors:

The use of the real-time vector displayed above, that is, the indicator value at a certain moment, will display the data within the last 1 hour by default in the Prometheus graph:
insert image description here

Add a square bracket [] after the expression in Expression, and the original instant vector becomes a range vector when the time range is defined in the brackets, such as 5m and 1h. At this time, the amount of data is not one, but multiple:
insert image description here
range The unit of the vector selector:
s:seconds
m:minutes h
:hours
d:days
w:weeks
y:years
, for example, the following expression returns the value of http_requests_total 5 minutes ago relative to the current query time:

Then the range vector cannot be drawn, because Prometheus has added a time range by default when the instant vector is drawn, and the range vector itself is a "bunch" of data with a time range, which cannot be displayed as a trend graph. At this time, it is generally It will be processed with the rate function to get the change rate of the node_cpu_seconds_total indicator within 5m minutes:
insert image description here
so that it can be drawn:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42808782/article/details/116399473