如果值为空,如何在图表上显示指标

How show metrics on graph if value is null

Prometheus 中存在一些来自 DB 的自定义指标

在 Graphana 中,我使用来自 Prometheus 的数据源制作了 Graph Dashboard

计数(custom_metrics_project1<1)

如果条件 custom_metrics_project1<1 未找到任何指标,Graphana 将显示未找到点数。

如何改变条件显示0?

您可以select如何在给定图表的编辑菜单中显示 NULL 值。

请按照以下步骤操作:

  • 点击你的图表
  • 单击 edit 调出图表的编辑菜单
  • 切换到标签Display
  • Null Value 下拉菜单中选择一个选项

您要的选项是:null as zero.

只需使用count(custom_metrics_project1<1) or on() vector(0)。这会将所有间隙替换为零。参见 docs for or operator and docs for vector() function

请注意,q or on() vector(0) 技巧仅在 q returns 单个时间序列时有效。如果 q returns 多个时间序列,那么 Prometheus 不会提供一种简单的方法来用零填充每个时间序列中的空白:(

如果您仍然需要填补多个时间序列中的空白,那么您可以在 VictoriaMetrics 中使用 default 运算符。例如,以下查询将从 q 返回的所有时间序列的所有空白填充为零:

q default 0

VictoriaMetrics 还提供 interpolate, keep_last_value and keep_next_value 函数,可用于以更复杂的方式填补空白。