Download Cheatsheet for PromQL Queries
Getting started with PromQL can be challenging if you're just starting your journey into the fascinating world of Prometheus. This guide will help you understand how it works, and this article includes interesting and helpful tips to get you started.
Because Prometheus stores data as a time-series data model, PromQL queries are radically different from conventional SQL. Understanding how to work with data in Prometheus is key to learning how to write efficient queries.
Don't forget to download Cheatsheet for PromQL Requests!
How time-series databases work
Time series are streams of values ββassociated with a timestamp.
Each time series can be identified by the metric name and labels, for example:
mongodb_up{}
or
kube_node_labels{cluster="aws-01", label_kubernetes_io_role="master"}
In the example above, there is a metric name ( kube_node_labels
) and labels ( cluster
and label_kubernetes_io_role
). In fact, metrics are also labels. The above query can be written like this:
{__name__ = "kube_node_labels", cluster="aws-01", label_kubernetes_io_role="master"}
There are four types of metrics in Prometheus:
Gauges () β , . ,
mongodb_up
, exporter MongoDB.
Counters ()
_total
. ,http_requests_total
.
Histogram () β , , .
Summary () , .
PromQL
PromQL , , . http_requests_total
.
, / api 10.2.0.4. host
path
:
http_requests_total{host="10.2.0.4", path="/api"}
:
name |
host |
path |
status_code |
value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
. http_requests_total
, , 98 .
instant vector, . , Prometheus . , .
, instant vector (, ).
offset
(), :
http_requests_total{host="10.2.0.4", path="/api", status_code="200"} offset 1d
, :
http_requests_total{host="10.2.0.4", path="/api"}[10m]
:
name |
host |
path |
status_code |
value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
, , .
range vector β .
PromQL
, PromQL . , ?
, node_cpu_cores
cluster
. , , , :
sum by (cluster) (node_cpu_cores)
:
cluster |
value |
foo |
100 |
bar |
50 |
, 100
cluster_foo
50
cluster_bar
.
, PromQL . , node_memory_MemFree_bytes
, , :
node_memory_MemFree_bytes / (1024 * 1024)
, node_memory_MemTotal_bytes
, , :
(node_memory_MemFree_bytes / node_memory_MemTotal_bytes) * 100
, 5% :
(node_memory_MemFree_bytes / node_memory_MemTotal_bytes) * 100 < 5
PromQL
PromQL , . , topk
, , ( ):
topk(2, (node_memory_MemFree_bytes / node_memory_MemTotal_bytes) * 100)
Prometheus , . pred_linear
, .
, , 24 . pred_linear
node_filesystem_free_bytes
, . 24 :
predict_linear(node_filesystem_free_bytes[1w], 3600 * 24) / (1024 * 1024 * 1024) < 100
Prometheus rate
. , . , .
, , , 10 . http_requests_total
, :
http_requests_total[10m]
name |
host |
path |
status_code |
value |
|
|
|
|
|
300 50, . rate
. , , :
name |
host |
path |
status_code |
value |
|
|
|
|
|
rate(http_requests_total[10m])
name |
host |
path |
status_code |
value |
|
|
|
|
|
10 0,83 . :
rate(http_requests_total[10m]) = 0
?
, Prometheus , PromQL .
Cheatsheet PromQL, ββ PromQL. Cheatsheet Prometheus playground.