1

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?

1
  • The expvar package is available specifically for in-app metrics. Commented Feb 27, 2019 at 15:18

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Also see flame graphs in go programs. You'll quickly see calls in the go application that are waiting on SQL responses but also see time consuming areas of your application.
3

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.