sparkle
sparkle

Reputation: 7398

Grafana multi-line chart from Postgres

I want to display a multi-line chart where each line it's the value of a column. For example, "status" has several values: 200, 404, 500 etc.. and I want to see a line for each. On X-axis I have to see how many records had the status=200 and so on..

I've tried with this

SELECT
  created_at AS "time",
  status,
  count(*) 
FROM api_logs
group BY time, status

but it's showing

enter image description here

instead I would something like

enter image description here

UPDATED: I tried with this query

SELECT
  $__timeGroupAlias(created_at, '5m'),
  status AS "metric",
  count(*) AS "count"
FROM api_logs
WHERE $__timeFilter(created_at)
GROUP BY 1,2

ORDER BY 1

but I got this chart

enter image description here

and this is the table output

enter image description here

I would see a line for each status. Where Y is the count of event for that status. Basically If I see a spike for a line (errors 500) I have to worry.

Upvotes: 1

Views: 2089

Answers (2)

warreee
warreee

Reputation: 527

The above answer did not solve it for me. I'm using Grafana 10 and what solved is was using the multi-frame time series transformation: enter image description here

Source

Upvotes: 1

Jan Garaj
Jan Garaj

Reputation: 28714

SQL query with 5min time aggregation with PostgreSQL macros for Grafana 8.2:

SELECT
  $__timeGroupAlias(created_at, '5m'),
  status AS "metric",
  count(*) AS "count"
FROM api_logs
WHERE $__timeFilter(created_at)
GROUP BY 1,2
ORDER BY 1

created_at column is timestamptz type.

Upvotes: 3

Related Questions