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

如何用单条VictoriaMetrics查询按区间统计temperature指标值数量?

Prometheus按温度区间统计Gauge指标数量的实现

核心思路

  1. 区间分组:用floor(temperature / 10) * 10把温度值按10度间隔分组——比如1019.999的温度会被归为10,2029.999归为20,以此实现同一区间数值的归一化。
  2. 统计计数:通过count by (...)按分组后的标签统计样本数,因为每个(node_id, system_id)组合对应唯一温度值,统计结果就是该区间内的指标数量。
  3. 区间格式化:高版本Prometheus可直接生成"X-Y"格式的区间标签;低版本可先按起始值分组,再在可视化层(如Grafana)转换格式。

可用查询语句

方案1:兼容所有Prometheus版本(需展示层调整格式)

该查询返回区间起始值作为标签,你可以在Grafana等工具中将图例设置为{{temp_floor}}-{{temp_floor | toInt + 10}},即可显示为"10-20"这类区间:

count by (temp_floor) (
  floor(temperature / 10) * 10
)

如果要统计过去1小时内的有效指标,修改为:

count by (temp_floor) (
  floor(temperature[1h] / 10) * 10
)

方案2:直接生成区间字符串(需Prometheus 2.47+)

若你的Prometheus版本≥2.47(支持strcat和string函数),可直接生成带区间字符串的结果:

count by (temperature_range) (
  label_replace(
    strcat(
      string(floor(temperature / 10) * 10),
      "-",
      string(floor(temperature / 10) * 10 + 10)
    ),
    "temperature_range", "$1", "", "(.*)"
  )
)

统计1小时数据的版本:

count by (temperature_range) (
  label_replace(
    strcat(
      string(floor(temperature[1h] / 10) * 10),
      "-",
      string(floor(temperature[1h] / 10) * 10 + 10)
    ),
    "temperature_range", "$1", "", "(.*)"
  )
)

补充说明

  • 若需要自定义区间间隔(比如5度一组),只需把公式里的10改成对应间隔值即可,例如floor(temperature /5)*5。
  • count by统计的是符合条件的时间序列数量,正好对应你需要的不同标签组合的温度值数量。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 16:43:15