When i use prometheus (promql) in grafana as datasource i can define a single query that shows multiple graphs on the same panel (e.g. http_requests_total shows all http request). Now i would like to do the same with a sql datasource. I have a table that contains time series data as a simple counter with some tags. i would like to show the counter_value for each tag as a graph. It would work when i define for each tag a single query. But this solution is not really maintainable because the tags can vary. I would prefere to have only 1 query as it works with promql. Is there a way to define a single query that shows for each tag a graph?
In this example only 1 graph is shown. I tried it with group by and my tag column but without success.
SELECT
$__timeGroupAlias(created,24h),
sum(counter_value) AS "counter_value"
FROM counter
GROUP BY 1
ORDER BY 1
Look into query variables.
Query variables allow you to construct a variable dynamically with an SQL query, which then can be referenced in your dashboard.
You could use a query such as this to obtain all the tag values from your table:
SELECT tag FROM counter
GROUP BY tag;
I am not 100% sure but it sounds like you may be able to solve your problem by using a query variable to obtain all your desired values, then use the repeat panel option. The repeat panel option will repeat the graph for each of the variable values. For example, if you have a query variable tag_var
which returned the values tag_a, tag_b, tag_c
, you could do something like this with the repeat panel option enabled to repeat on tag_var
:
SELECT
$__timeGroupAlias(created,24h),
sum(counter_value) AS "counter_value"
FROM counter
WHERE tag_value = '$tag_var'
GROUP BY 1
ORDER BY 1