Introducing PromQL + Cheatsheet

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







10.2.0.4







/api







200







98







http_requests_total







10.2.0.4







/api







503







20







http_requests_total







10.2.0.4







/api







401







1







. 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





http_requests_total







10.2.0.4







/api







200







641309@1614690905.515







641314@1614690965.515







641319@1614691025.502







http_requests_total







10.2.0.5







/api







200







641319@1614690936.628







641324@1614690996.628







641329@1614691056.628







http_requests_total







10.2.0.2







/api







401







368736@1614690901.371







368737@1614690961.372







368738@1614691021.372







, , .





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





http_requests_total







10.2.0.4







/api







200







100@1614690905.515







300@1614690965.515







50@1614691025.502







300 50, . rate



. , , :





name





host





path





status_code





value





http_requests_total







10.2.0.4







/api







200







100@1614690905.515







300@1614690965.515







350@1614691025.502







rate(http_requests_total[10m])
      
      



name





host





path





status_code





value





http_requests_total







10.2.0.4







/api







200







0.83







10 0,83 . :





rate(http_requests_total[10m]) = 0
      
      



?

, Prometheus , PromQL .





Cheatsheet PromQL, ​​ PromQL. Cheatsheet Prometheus playground.








All Articles