Prometheus query for table in grafana
Asked Answered
T

3

12

When i make a table panel and go to "Options" tab, columns parameter set to Auto: Columns and their order are determined by the data query.

Is there a doc on how write prometheus queries for grafana tables?

My prometheus data is a metric with 2 labels my_col and my_row:

my_metric{instance="lh",job="job",my_col="1",my_row="A"} 6
my_metric{instance="lh",job="job",my_col="2",my_row="A"} 8
my_metric{instance="lh",job="job",my_col="1",my_row="B"} 10
my_metric{instance="lh",job="job",my_col="2",my_row="B"} 17

I want to make a table that looks like:

|   | 1 | 2 |
| A | 6 | 8 |
| B | 10| 17|
Ticonderoga answered 4/9, 2018 at 10:0 Comment(0)
A
2

There is some documentation in the Grafana docs about Table Panels here: https://grafana.com/docs/features/panels/table_panel/#table-panel

The particular transform you showed in the example doesn't appear to be supported in the above documentation unfortunately.

If possible, a change to the structure of the prometheus data to something like the below should make this easier:

my_metric{instance="lh",job="job",col_1="6",col_2="8",my_row="A"}
my_metric{instance="lh",job="job",col_1="10",col_2="17",my_row="B"}
Adelina answered 16/7, 2019 at 11:4 Comment(0)
D
9

After some experimentations in Grafana 9.1.1, I have obtained a way to construct a table like you have described with prometheus metric like that. Here are Grafana transform functions you will need:

  1. Labels to fields
    This function separate the labels in the metric to columns.
  • Set Mode to Columns
  • Set Labels to be only the columns that are relevant so in this case it's my_col and my_row
  • Set Value field name to my_col
  1. Reduce
    This function reduce all values in separate times into one row.
  • Set Mode to Reduce fields
  • Set Calculations to Last*. You may change this part according to your needs.
  • Set include time to false
  1. Merge
    This function will merge all datasets into one with corresponding columns.
  2. Organize fields
    Finally, this function will help you reorganize the table into something that's more proper. For presenting the data in bar chart, ensure that my_row column is the most left one.
Djakarta answered 26/8, 2022 at 6:37 Comment(0)
G
6

It is possible with Grafana 7.0.3. I think it was possible with earlier versions as well, but the approach has changed since 7.0.0 with the introduction of transformations.

Add two queries:

my_metric{instance="lh",job="job",my_col="1"}
my_metric{instance="lh",job="job",my_col="2"}

Make them instant queries and format them as tables. Create the transformation Outer join and join by my_col. You should get the two values as Value #A and Value #B. You can use the transformation Organize fields to rename them and drop all the other fields.

Garibald answered 26/6, 2020 at 6:40 Comment(2)
Would you mind sharing where you found that "Organize fields" option on the Explore view?Rescind
It's not available in the explorer, only on panels of a dashboard.Garibald
A
2

There is some documentation in the Grafana docs about Table Panels here: https://grafana.com/docs/features/panels/table_panel/#table-panel

The particular transform you showed in the example doesn't appear to be supported in the above documentation unfortunately.

If possible, a change to the structure of the prometheus data to something like the below should make this easier:

my_metric{instance="lh",job="job",col_1="6",col_2="8",my_row="A"}
my_metric{instance="lh",job="job",col_1="10",col_2="17",my_row="B"}
Adelina answered 16/7, 2019 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.