英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论