How to get Grafana to include sum of values in tooltip or legend for stacked linechart
Asked Answered
L

1

5

I have Kubernetes running (K3s on TrueNAS scale). I've deployed Prometheus and Grafana and am able to access the metrics in Grafana. I now want to create a stacked line chart that shows memory usage by namespace and total memory used by Kubenetes.

I got it working without the total with this query: sum (container_memory_working_set_bytes{namespace!=""}) by(namespace) (see screen shot 1).

I needed to add the {namespace!=""} or it would add an entry with the same name as the query (see screen shot 2). Don't understand what that value represents, but its not the total I'm after!

How can I include a total of the memory used in the tooltip (without it appearing as a line in the chart)?

Screen shot 1 of query Screen shot 2 of query not filtered with namespace!=""

Lilias answered 9/10, 2022 at 15:3 Comment(0)
L
6

I think I figured it out. I still have the query sum (container_memory_working_set_bytes {namespace!=""} ) by(namespace).

Then added a transformation "Add field from calculation", again with the defaults. I thought this would only work for the properties listed at the time of creating the transformation/query, but spinning up a new container did get it automatically added to the chart.

Mode = Reduce row
Field name = all selected
Calculation = Total
Alias = Total
Replace all fields = False

Then in the panel on the right, configure these settings:

Graph styles > Style: Lines
Graph styles > Fill opacity: 40
Graph styles > Stack series: Normal
Standard options > Unit: Byte(IEC)

Finally, also in the panel on the right, add an override (see Grafana query screen shot):

Add field override > Fields with name: Total
Add override property > Graph styles > Stack series: 100%

End Result End result

Grafana query Grafana query

Grafana transformations Grafana transformations

Lilias answered 14/10, 2022 at 16:16 Comment(1)
Nice job! I used a "Graph styles > Transform: Constant" override for a similar effect, in my case it led to Total being displayed on the tooltip and legend but not on the graph itself.Replay

© 2022 - 2024 — McMap. All rights reserved.