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

如何在Grafana状态面板展示InfluxDB中in_bytes+out_bytes的求和结果

Got it, let's work through this since we can't rely on InfluxDB joins or server-side aggregation to create a combined sum measurement. Here are two solid approaches to display the sum of in_bytes and out_bytes in your Grafana Stat panel, with priority on the latest data:

Method 1: Use Grafana's Transform Feature (Works with InfluxQL or Basic Flux)

This approach pulls the latest values for both metrics separately, then combines them directly in Grafana:

  1. Create a new Stat panel in Grafana and select your InfluxDB data source.
  2. Add two separate queries to fetch the latest values:
    • For in_bytes: SELECT last("in_bytes") FROM "your_measurement" WHERE $timeFilter
    • For out_bytes: SELECT last("out_bytes") FROM "your_measurement" WHERE $timeFilter
      Replace your_measurement with your actual measurement name in InfluxDB.
  3. Switch to the Transform tab, then click Add transformation and select Add field from calculation.
  4. Configure the transformation like this:
    • Operation: Binary operation
    • Left operand: Choose the field from your first query (e.g., last_in_bytes)
    • Operator: Add (+)
    • Right operand: Choose the field from your second query (e.g., last_out_bytes)
    • Output field name: Name it something clear like total_bytes
  5. Add a second transformation: Filter fields by name, then keep only the total_bytes field to clean up the panel output.
  6. Head to the Options tab and confirm the Stat type is set to Last value—this ensures the latest summed value is displayed prominently.
Method 2: Use Flux Query for Direct Calculation (Cleaner if Flux is Supported)

If your InfluxDB instance uses Flux (v1.8+ with Flux enabled, or InfluxDB 2.x), you can handle the sum directly in the query without relying on Grafana transforms:

// Fetch latest in_bytes value
in_data = from(bucket: "your_bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "your_measurement" and r._field == "in_bytes")
  |> last()
  |> keep(columns: ["_value"])
  |> rename(columns: {_value: "in_bytes"})

// Fetch latest out_bytes value
out_data = from(bucket: "your_bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "your_measurement" and r._field == "out_bytes")
  |> last()
  |> keep(columns: ["_value"])
  |> rename(columns: {_value: "out_bytes"})

// Join tables and calculate total
join(tables: {in: in_data, out: out_data}, on: [])
  |> map(fn: (r) => ({_time: r._time, _value: r.in_bytes + r.out_bytes, _field: "total_bytes"}))

Swap your_bucket and your_measurement with your actual values.
Paste this into the Grafana query editor, and your Stat panel will automatically show the latest summed total_bytes value.

Quick Notes

  • Both methods prioritize the latest individual values of in_bytes and out_bytes for the sum, which aligns with your requirement.
  • If you notice slight timestamp mismatches between the two metrics, don't worry—the sum will still use the most recent value from each, which is what you want.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 21:19:08