Grouped bar chart from counter

30 views Asked by At

Struggling to understand how to properly generate a (grouped) bar chart like the one in this example:

enter image description here

My Prometheus metrics are like this:

articles_sellable_total{location="Warehouse0", sellable="false"} 0
articles_sellable_total{location="Warehouse0", sellable="true"} 2
articles_sellable_total{location="Warehouse0", sellable="false"} 0
articles_sellable_total{location="Warehouse0", sellable="true"} 43
articles_sellable_total{location="Warehouse1", sellable="false"} 100
articles_sellable_total{location="Warehouse1", sellable="true"} 21
articles_sellable_total{location="Warehouse1", sellable="false"} 2
articles_sellable_total{location="Warehouse1", sellable="true"} 15
articles_sellable_total{location="Warehouse2", sellable="false"} 1
articles_sellable_total{location="Warehouse2", sellable="true"} 8
articles_sellable_total{location="Warehouse2", sellable="false"} 22
articles_sellable_total{location="Warehouse2", sellable="true"} 11

And so, I've been following the docs, then trying to group like

sum by (location, sellable) (articles_sellable_total{})

but in the Table view in Grafana it gives me something like

Time           {location="Warehouse0", sellable="false"}
2024-03-25 14:19:00   0
2024-03-25 14:19:30   0
2024-03-25 14:20:00   0
2024-03-25 14:20:30   0

also variants like count by (location, sellable) (articles_sellable_total{}) and sum by (location) (articles_sellable_total{sellable="true"}) + (articles_sellable_total{sellable="false"})

So either the location is not persisted, or the table view gives nonsense, or the x-axis is just messed up.

1

There are 1 answers

0
markalex On

Bar chart panel expects data in format

Categories Column1 Column2
Category1 value value
Category2 value value
Category3 value value

Your initial query is just fine in terms of providing needed data. But it requires some massaging to put it into expected format.

First, to leave only latest values and present them in an easier form for following steps change query options to Format: Table, Type: Instant (Options block under query editor).

Then to "transpose" table into expected format add Transformation Grouping to Matrix with Column set to sellable, Row to location, and Cell value left as Value.

After that in Table view you should see something like

location\sellable true false
Warehouse0 45 0
Warehouse1 36 102
Warehouse2 19 23

And bar chart view should present you groups of bars per location, with two bars in each group true and false. If you want goupping to happen the other way around, you can switch places for Row and Column options in transformation.