如何在 Bosun 模板中格式化数字?

How do I format numbers within Bosun templates?

在 Bosun 模板中,是否可以将来自警报的已评估变量的输出格式化为小数点后的精度?

简单示例:

template test_template{
    subject = test
    body = {{.Eval .Alert.Vars.average_runtime}} seconds
}
alert test_alert{
    template test_template
    $average_runtime = avg(q("avg:metric_name", "24h",""))
    crit = $average_runtime > 150.0
}

结果

190.71165892385326 seconds

在模板正文中,这是不必要的精确。理想情况下,我希望看到:

190.71 seconds

在 go 模板中,您可以使用 printf 根据您想要的任何格式字符串来格式化输出。这段代码对我有用:

template test_template{
  subject = test
  body = {{printf "%.3f" (.Eval .Alert.Vars.average_runtime)}} seconds
}

alert test_alert{
  template = test_template
  $average_runtime = 1234.5678999
  crit = 1
}