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

如何在Grafana中用InfluxDB查询聚合多实例服务活跃连接数

Solution for Summing Multi-Instance Active Connections in InfluxDB + Grafana

Got it, let's work through this problem. The core issue here is that your activeConnections metric only logs updates when connections change (not at regular intervals), so when dealing with multiple instances (tagged by host), you need to first capture the latest valid connection count for each host in every time interval, then sum those values to get the total active connections across all instances.

Here's the query that will solve this:

SELECT sum(last_value) AS total_active_connections
FROM (
  SELECT last("value") AS last_value
  FROM "activeConnections"
  WHERE $timeFilter
  GROUP BY time($__interval), "host" fill(previous)
)
GROUP BY time($__interval) fill(0)

Breakdown of how this works:

  1. Inner Subquery:

    • Groups data by both Grafana's dynamic time interval ($__interval) and the host tag
    • Uses last("value") to grab the most recent connection count for each host within each time interval
    • fill(previous) is critical here: if a host has no new data in a time interval, it fills in the last known value from previous intervals (since connection counts don't disappear unless explicitly logged as such)
  2. Outer Query:

    • Takes the per-host last values from the subquery and sums them for each time interval
    • fill(0) handles edge cases where no instances reported data at all in a given interval, defaulting the total to 0

Testing with your sample data:

For the time range 2018-05-16T16:00:00Z to 2018-05-16T16:10:00Z:

  • The inner query would pull 3 for host 58e5bd04a313 and 4 for host 29b39780fd7b
  • The outer query sums these to get 7, which matches the total you wanted

This approach ensures you're always accounting for all active instances, even if they don't log connection changes every interval.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:05:39