如何在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:
This approach pulls the latest values for both metrics separately, then combines them directly in Grafana:
- Create a new Stat panel in Grafana and select your InfluxDB data source.
- 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
Replaceyour_measurementwith your actual measurement name in InfluxDB.
- For
- Switch to the Transform tab, then click
Add transformationand selectAdd field from calculation. - 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
- Operation:
- Add a second transformation:
Filter fields by name, then keep only thetotal_bytesfield to clean up the panel output. - Head to the Options tab and confirm the Stat type is set to
Last value—this ensures the latest summed value is displayed prominently.
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_bytesandout_bytesfor 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

