整理写过的promQL


0、metric_relabel_configs一个常用的用途:drop不需要的数据,不保存在Prometheus 中。

 

1、统计计算节点已经分配的cpu/memory request占节点Allocatable的百分比(类似kubectl describe node)

sum(kube_pod_container_resource_requests_cpu_cores{} * on(namespace, pod) group_left() (kube_pod_status_phase(phase!="Succeeded", phase!="Failed") == 1)) by (node) / on(node) kube_node_status_allocatable_cpu_cores{} * 100

group_left一般用来在多对一场景下作二元运算时,指定左边的指标样本具有更高的基数,比如kube_pod_container_resource_requests_cpu_cores{}这个指标(namespace,pod)是一样的记录也可以有多条(container不同)

注意如果pod没有调度成功(pending),那么指标kube_pod_container_resource_requests_cpu_cores{}中没有node label,所以后面kube_pod_status_phase可以不用去掉Pending状态的Pod,去掉也无所谓

group_left还可以用来实现运算后保留左边指标的标签

 

2、计算一段时间内counter类型指标变化率的最大值,如计算3h内某容器cpu使用率最大值

我们知道求某个指标样本时间序列range vector数据的最大值应该使用max_over_time,但是promql子语句无法再计算range vector数据,即

irate(container_cpu_usage_seconds_total[5m])[3h]这种写法是错误的,导致max_over_time无法输入一个cpu使用率的range vector值,可行的一个解决办法是通过Rules引入一个record(相当于一个新的指标),那么就可以通过这个record的metrics selector来获取range vector数据,即metrcis{}[time],如下

1、现在Prometheus配置一个rules文件,内容如下

groups
- name: kubernets-pod-cpu-irate
  rules:
  - record: container:cpu_usage:irate
    expr:  irate(container_cpu_usage_seconds_total{}[5m])

 

2、在Prometheus可以直接查询container:cpu_usage:irate这个指标,那么通过如下PromQL即可计算时间范围内速率的最大值

max_over_time(container:cpu_usage:irate{}[3h])

 

 

 3、在grafana面板中添加变量Variables时,如果变量的Type时Query,那么可以通过promQL来获取变量的取值,其中label_values()可以用来获取某个指标具体某个标签的值,而label_names()可以用来获取所有的标签名(这个要求prometheus的版本大于2.6)

 

4、prometheus的指标类型可以分为Counter、Gauge、Histogram、Summary,promQL表达式数据类型可以分为Instant vector、Range vector、Scalar、String

Histogram和Summary的区别是什么呢?
Histogram:客户端(即暴露指标的服务)设定桶bucket的边界le,并且计算落在bucket区间样本的数量(不会保存样本值),因为只是做一个计数统计,所以性能开销低(和Counter或Gauge类似),可以通过promQL的histogram_quantile()函数来计算summary分位点数据,但是毕竟没有保存实际的样本数据,histogram_quantile计算出的数据可能不准确,但是因为在服务端计算,可以按需指定分位点。
Summary:客户端已经计算好quantile分位点数据,算法计算过程需要全局锁保护,所以计算性能相对差。服务端如prometheus不能获取未指定的分位点数据。

 

 

 

__address__:当前Target实例的访问地址<host>:<port>

__scheme__:采集目标服务访问地址的HTTP Scheme,HTTP或者HTTPS

__metrics_path__:采集目标服务访问地址的访问路径

__param_<name>:采集任务目标服务的中包含的请求参数

# The source labels select values from existing labels. Their content is concatenated
# using the configured separator and matched against the configured regular expression
# for the replace, keep, and drop actions.
[ source_labels: '[' <labelname> [, ...] ']' ]
 
# Separator placed between concatenated source label values.
[ separator: <string> | default = ; ]
 
# Label to which the resulting value is written in a replace action.
# It is mandatory for replace actions. Regex capture groups are available.
[ target_label: <labelname> ]
 
# Regular expression against which the extracted value is matched.
[ regex: <regex> | default = (.*) ]
 
# Modulus to take of the hash of the source label values.
[ modulus: <uint64> ]
 
# Replacement value against which a regex replace is performed if the
# regular expression matches. Regex capture groups are available.
[ replacement: <string> | default = $1 ]
 
# Action to perform based on regex matching.
[ action: <relabel_action> | default = replace ]

 

posted @ 2020-08-27 16:54  JL_Zhou  阅读(2664)  评论(0编辑  收藏  举报