justwatchcom / sql_exporter

Flexible SQL Exporter for Prometheus.
MIT License
402 stars 109 forks source link

Possible to calculate query time taken? #110

Closed jmreicha closed 11 months ago

jmreicha commented 12 months ago

I'm testing out the exporter and it is working for me so far. Wondering if there is any method to get the amount of time a query as a Prometheus metric so that I can measure certain database latencies.

Apologies if this is already outlined in docs, I might have missed it!

dewey commented 12 months ago

Hey, are you talking about getting the historic duration a query takes? If you can query it from the database you can do it, it won't be super accurate as you'd only get the value at the time Prometheus scrapes but might be good enough. In Postgres the pg_stat_activity table is probably your best bet.

jmreicha commented 12 months ago

Thanks for the reply, that makes more sense.

Maybe I'm thinking about the problem the wrong way. I was thinking that Prometheus would capture the amount of time it takes for a query to complete, but it looks like I am mistaken. I will look into the pg_stat_activity table.