概述
网上查询关于increase
的使用知道大概,但当具体使用时,关于其指标详情理解,仍然云里雾里,本人在使用过程中,关于该指标,有以下总结,在文末部分,sum
函数会结合increase
一起分析,讲解实际遇到的问题。
increase官网描述
increase(v range-vector) calculates the increase in the time series in the range vector. Breaks in monotonicity (such as counter resets due to target restarts) are automatically adjusted for. The increase is extrapolated to cover the full time range as specified in the range vector selector, so that it is possible to get a non-integer result even if a counter increases only by integer increments.
从官网的描述可以得到如下信息:
- 横坐标是时间范围;
- 计算的结果可能包含小数,由此可知
increase
所得结果并不是简单的减法所得。
increase函数案例分析
以如下案例做进一步讲解,下面这个指标主要用来计算 K8S Pod什么时间发生重启,并重启多少次 :
increase(kube_pod_container_status_restarts_total{k8s_cluster_name=~"$k8s_cluster_name", pod=~".*$container_name.*", pod!~".*istio-proxy.*"}[24h])
从上图可以可以得出以下结论:
increase
函数本周期的First
的取值源自上个计算周期的Mean
值,如果计算周期是 24h,即改值是上个24小时的Mean
值;- Mean 值是由 Total/Count 得出;
- 如果一个 Pod 中的 container 重启了多次,但后续时间段没有再发生重启,则该 Pod 在后续时间范围会是一条非0直线,如上图所示;
- 一个 Pod 如果连续多天发生重启失败然后反复重启的情况,则该Pod在非0纵坐标上会有小波浪线组成的直线,如下图所示
hip-fast-design-service-6769b7b6f5-9d2z
Pod, 会一直在255,256,255…之间波动,如下所示,Pod 的状态会是Waiting 和 Terminated 之间轮询切换;
- 根据 increase 指标,求 sum,会四舍五入得到整数数值,如下所示:
sum 函数结合 increase函数的案例分析
由上面可知,increase 所获得的值可能包含小数点,而 sum
函数在求和后会四舍五入进行取整,这样会导致一些监控指标项所得结果会有偏差,以下面统计 24 小时之内,Pod 和 Service层级(比如一个deployment服务可能包含多个副本,即多个Pod)两个层级重启的次数。
下面的左边服务层级计算所得是178,但右边169+4+4 所得是177,结果不一样。
左边的计算公式是sum(increase(kube_pod_container_status_restarts_total{deployment_environment="prod", container=~"$container_name"}[24h])) by (container) >0
,右边是sum(increase(kube_pod_container_status_restarts_total{deployment_environment="prod", container=~"$container_name"}[24h])) by (pod) >0
,两遍区别是公式最后的by container 和 by pod。
左边的by container
是直接在increase原值169.12+4.28+4.22=177.62相加之后在取整,如下图所示,根据四舍五入所以得到178,右边的by pod
是各自根据pod
直接取整,因为三个值,小数部分都小于5,被舍去,所以得到169/4/4。
通过在Grafana的explorer中进一步分析,可知,
另外补充,当对sum
函数的值做聚合,比如group by
计算时,需要特别注意,计算的结果在 Panel上面显示是整数,但实际上仍然包含小数部分(如果有),所以在group by
之前,必须取整,可以看下面截图的案例:
这里用到了线性计算
Unary operation
的 floor
操作,即去掉小数部分,然后再group by
。