wellthatssomething
wellthatssomething

Reputation: 155

Count number of queries on database in Golang?

I am attempting to count the number of queries that are run against the database in a request against my Golang API server. This is in order to optimize + find bottlenecks in our API.

An example of how to do this in Django is here.

Any idea if there is a simple way to do this in Golang?

Upvotes: 1

Views: 667

Answers (2)

danblack
danblack

Reputation: 14666

In order to find bottlenecks I would suggest going straight to the MySQL database and enabling the slow query log and slowly setting the long_query_time down to a low millisecond value.

Use tools like pt-query-digest to help digest these to get a frequency of like queries. Attack those as slow queries that need fixing and then set a lower value.

The actual count of the queries on each isn't actually that useful.

When attacking the problem from a go point of view measuring the API response time of each interface will help you look holisticly at the service.

Upvotes: 4

robbieperry22
robbieperry22

Reputation: 2225

No easy solution that I'm aware of.

You could wrap your db in your own struct, and then implement Exec() or whichever function you use directly on that struct. Your function would just call the database one, and count it however you see fit.

A similar example, but with a logger, can be found here: How to log queries to database drivers?

Upvotes: 3

Related Questions