Find Time-Consuming ActiveRecord Database Queries with QueryTrack

A tool for monitoring SQL queries in Rails

Kirill Shevchenko
Aug 25 · 2 min read

Get notified about slow queries

In the early stages of development, it may be useful to identify slow queries in time (when you don’t have a paid APM). QueryTrack helps to get notified about slow queries with related data: duration and backtrace.

https://github.com/kirillshevch/query_track

Installation

Add this line to your application’s Gemfile and then execute bundle install:

gem 'query_track'

To start receiving information about requests you need to createconfig/initializers/query_track.rb where there is a time limit that is specified in seconds and an output channel (for example console logs):

QueryTrack::Settings.configure do |config|
config.duration = 0.5
config.logs = true
end

Configuration

SQL Duration Limit (Time Limits)

You won't get any notifications without setting the max duration of an SQL query. These settings will show at what point requests are considered to be slow.

QueryTrack::Settings.configure do |config|
config.duration = 3
end

Filters

To avoid noisy warnings from used gems, and places where fat queries are justified, you can filter SQL with backtrace.

For example, you have installed activeadmin and want to skip everything from app/admin . It possible with filters config which accepts an array of excepted folders.

QueryTrack::Settings.configure do |config|
config.duration = 3
config.filters = ['app/admin']
end

Enable/Disable without code change

Since some of you may need the ability to disable, an option is provided to do this with ENV variable (or encrypted secrets).

QueryTrack::Settings.configure do |config|
config.duration = 3
config.enabled = ENV['QUERY_TRACK_ENABLED']
end

By default QueryTrack is enabled. This option fully disables listening of ActiveRecord events.

Console Log

QueryTrack::Settings.configure do |config|
config.duration = 3
config.logs = true
end

Slack Notifications

To receive notifications about slow queries into Slack, you need to install incoming-webhooks app or create your own and put a link into the config file. But be careful because it can be noisy in a case with many requests.

QueryTrack::Settings.configure do |config|
config.duration = 3
config.notifications.slack = 'https://hooks.slack.com/services/T0000000/B0000000/C0000000'
end

Then you will receive notifications into the selected Slack channel:

Conclusion

I suggest adding a gem query_track which will show notifications in the Slack /console (or both) info about slow SQL queries. It will make it easier for developers to profile the database and improve performance at the development stage.

Kirill Shevchenko

Written by

Ruby/JS developer at RubyGarage. Interested in Full-Stack Development and DevOps.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade