Prometheus规则中{{ with query "time()" }}语句含义咨询
Prometheus告警规则中
{{ with query "time()" }}{{ . | first | value | humanizeTimestamp }}{{ end }}的含义 这是Prometheus告警注解里的Go模板语法结合Prometheus扩展函数,作用是生成告警触发时的人类可读时间戳,各部分拆解说明:
{{ with query "time()" }}:执行PromQL查询time(),该查询返回当前Prometheus服务器的Unix时间戳(单样本结果集),with将查询结果绑定到上下文变量.。{{ . | first }}:从查询返回的样本集中取出唯一的样本(time()仅返回一个样本)。{{ . | value }}:提取样本的数值部分,即原始Unix时间戳数字。{{ . | humanizeTimestamp }}:将Unix时间戳转换为人类易读的字符串格式(比如2024-05-20 14:35:20)。{{ end }}:闭合with代码块,结束这段逻辑执行。
在你提供的告警规则里,这段代码会在告警触发时,把当前Prometheus服务器的时间转成可读格式,填充到timestamp注解和summary字段中,方便直观查看告警触发时间点。
告警规则示例代码:
- alert: SCPIngressTrafficRateAboveMajorThreshold annotations: timestamp: ' {{ with query "time()" }}{{ . | first | value | humanizeTimestamp }}{{ end }} ' description: 'Ingress Traffic Rate at locality: {{$labels.ocscp_locality}} is above major threshold (i.e. 1400 mps)' summary: 'namespace: {{$labels.kubernetes_namespace}}, podname: {{$labels.kubernetes_pod_name}}, timestamp: {{ with query "time()" }}{{ . | first | value | humanizeTimestamp }}{{ end }}: Current Ingress Traffic Rate is {{ $value | printf "%.2f" }} mps which is above 80 Percent of Max MPS(1700)' # Provide app and kubernetes_namespace of scp deployed
内容的提问来源于stack exchange,提问作者Daniel Wu
相关产品推荐
相关产品推荐

