Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grafana state timeline panel with values (states) supplied by label

I do have a Prometheus time series with samples like these:

a_metric{band="1", state="A"} 1
a_metric{band="2", state="C"} 1
a_metric{band="1", state="A"} 1
a_metric{band="2", state="C"} 1
a_metric{band="1", state="B"} 1
a_metric{band="1", state="B"} 1
...

I would like to visualize this time series in a state timeline panel such that bands become horizontal bands and states become discrete states within these bands. For this, I would have to extract values from the label state (and use them instead of values 1).

Is this possible and can such a visualization be achieved?

If I understand correctly Prometheus' label_values() cannot serve here, because it is restricted to templating. I suspect Grafana transformations could play a role, but I do not yet have experience with those. The complication also arises because Prometheus does not have string type metrics.

UPDATE Here is a basic image, as requested by @JanGaray.

basic image

like image 605
rookie099 Avatar asked Sep 14 '25 08:09

rookie099


1 Answers

suspect Grafana transformations could play a role

It is indeed possible with transformations.

In short

  1. Set query format to table
  2. Add transformation Grouping to matrix.
    • Set column to band
    • Set row to time
    • Set cell_value to state
  3. Add transformation Convert field type
    • Set field to Time/<column>
    • As Time
  4. Configure Value mappings for each of the states you have

Explanation

I'll show how the transformations work To see what is happening in the transformations, it is always helpful to enable the Table view toggle set to active as this will show actual returned data to the visualization. Also toggling the transformations on and off using the symbol of an eye next to transformation really helps in seeing what is happening.

The state timeline visualization works best with a table as data source. Although it does support time series, the information we want to show is not a time series.

The visualization works best when the data is as follows.

🕓 Time 🇦 Band 1 🇦 Band 2
T1 State A State B
T2 State C State A

But when data is formatted as a time series the data will look like this:

🕓 Time 🪟 {Band 1, State A} 🪟 {Band 1, State C} 🪟 ...
T1 1
T2 1

So first we need to set query format to from Timeseries to Table. Expend the query options, and set format to Table.

When doing that our table will look like this.

🕓 Time 🇦 band 🇦 state 🪟 Value
T1 Band 1 State A 1
T1 Band 2 State B 1
T2 Band 1 State C 1
T2 Band 1 State A 1

Next step is to pivot the table. For this we add the Grouping to matrix transformation.

  • Set column to band
  • Set row to time
  • Set cell_value to state
🇦 Time 🇦 Band 1 🇦 Band 2
1000 State A State B
2000 State C State A

Now there is just one problem left. The first column should be of data type time but it is of type string. You can see this by the icon infront of the column name. Indicated with emoji in my examples.

But this is an easy fix. Just add Convert field type transformation below Grouping to matrix transformation.

  • Set field to Time/<column>
  • As Time

Now the table should look as follows:

🕓 Time 🇦 Band 1 🇦 Band 2
T1 State A State B
T2 State C State A

Example

Following shows an example for ArgoCD. It shows the sync status for each application over time.

The prometheus exported data from ArgoCD looks as follows:

argocd_app_info{name="Application A", sync_state="Synced", ...} 1
argocd_app_info{name="Application B", sync_state="OufOfSync", ...} 1
argocd_app_info{name="Application C", sync_state="Unknown", ...} 1
argocd_app_info{name="Application D", sync_state="Synced", ...} 1

State timeline visualization example

like image 80
Waaghals Avatar answered Sep 16 '25 12:09

Waaghals