You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Grafana 9.5+含humanize函数的自定义模板保存失败报错求助

解决Grafana 9.5+中Alertmanager模板humanize函数未定义的问题

问题原因

Grafana 9.5及以上版本调整了与Alertmanager的集成逻辑,Alertmanager模板环境不再支持humanize这个非标准Go模板扩展函数,因此保存包含该函数的模板时会触发function "humanize" not defined错误。

解决方法

方法1:使用原生printf函数替代

用Go模板原生支持的printf函数实现类似的数值格式化效果,可根据需求调整格式参数:

  • 保留三位小数:{{ printf "%.3f" 0.123456 }}
  • 带千分位的两位小数:{{ printf "%,.2f" 1234.56 }}
  • 针对小数的精细格式化:{{ printf "%.4f" 0.123456 }}

方法2:自定义模板实现人性化数值处理

如果需要更复杂的人性化逻辑(比如自动切换单位),可以自定义模板函数:

{{- define "customHumanize" }}
  {{- if ge . 1000000 }}
    {{- printf "%.1fM" (div . 1000000) }}
  {{- else if ge . 1000 }}
    {{- printf "%.1fK" (div . 1000) }}
  {{- else if ge . 1 }}
    {{- printf "%.2f" . }}
  {{- else }}
    {{- printf "%.4f" . }}
  {{- end }}
{{- end }}

调用自定义模板:{{ template "customHumanize" 0.123456 }}

内容的提问来源于stack exchange,提问作者justanothercoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 21:24:58