Prometheus邮件警报显示指标值

huangapple go评论72阅读模式
英文:

Prometheus email alert to show metric value

问题

我在Prometheus/Alert Manager中有一个名为cs_job_time的标签,并希望在满足另一个作业的条件时发送电子邮件警报。电子邮件发送正常,但是否可以在电子邮件中包含cs_job_time的值?我可以使用{{$value}}来获取所需指标的值,但我还想打印出cs_job_time的值。

我看到了这个,但是当我尝试以下代码时:

time = "{{  `cs_job_time{instance='%s', job='/'}` $labels.instance | query | first }}

或类似的变体时,我收到错误消息"Error expanding alert template CSJobAlert with data '{map[] 2123}': runtime error: invalid memory address or nil pointer dereference" source="alerting.go:199"

是否可以通过电子邮件发送指标值?

英文:

I have a label cs_job_time in Prometheus/Alert Manager and would like to send an email alert when a condition is met for another job. The email sends fine but is it possible to include the value of cs_job_time within the email? I can use {{$value}} for the metric in question but I would also like to print the value of cs_job_time.

I came across this but when I try

time = "{{  `cs_job_time{instance='%s', job='/'}` $labels.instance | query | first }}

or similar variants, I get the error message "Error expanding alert template CSJobAlert with data '{map[] 2123}': runtime error: invalid memory address or nil pointer dereference" source="alerting.go:199"

Is it possible to email metric values?

答案1

得分: 3

你在示例中漏掉了printf

"{{ printf `cs_job_time{instance='%s', job='/'}` $labels.instance | query | first }}"

不过要小心,如果没有结果,first会失败。通常最好使用一个范围语句,因为它能够适应这种情况。

英文:

You're missing the printf there from the example:

"{{ printf `cs_job_time{instance='%s', job='/'}` $labels.instance | query | first }}"

Be careful though, if there's no results then the first will fail. It's generally best to use a range statement as that'll be resilient to that.

huangapple
  • 本文由 发表于 2017年4月18日 21:22:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/43473473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定