GithubHelp home page GithubHelp logo

kirillshevch / query_track Goto Github PK

View Code? Open in Web Editor NEW
290.0 8.0 14.0 331 KB

Find time-consuming database queries for ActiveRecord-based Rails Apps

Home Page: https://link.medium.com/owCvid7iJZ

License: MIT License

Ruby 98.92% Shell 1.08%
rails database performance slack query-optimization development-tools

query_track's Introduction

QueryTrack

Gem Version Maintainability

Tool for finding time-consuming database queries for ActiveRecord-based Rails Apps. Provides Slack notifications with backtrace, raw SQL, time duration, etc.

Installation

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

gem 'query_track'

Usage

SQL Duration Limit

Specify SQL duration query limit (in seconds):

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

Console Log

Enable console logs from config:

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

Log Example

Filters

To avoid noisy warnings from used gems, and places where fat queries are justified, you can filters SQL by backtrace. For example, you have installed activeadmin and want to skip everything from app/admin:

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

App Directory

QueryTrack finds the trace by filtering the caller by the app directory. By default, the app directory is set to 'app', the default for Rails apps. For apps that have a non-stanard app directory, this can be set with the app_dir config field:

QueryTrack::Settings.configure do |config|
  config.duration = 0.5
  config.app_dir = 'backend'
end

Enable/Disable toggle

Enable/disable with ENV variables to turn it on/off without code push. By default QueryTrack is enabled.

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

Slack Notifications

To receive notifications about slow queries into Slack, you need to install incoming-webhooks and put link into config file:

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

Incoming Hook Example

Custom Notifications (Handlers)

You can write your own handler for slow queries. Send data to any source(for e.g. to logs storage) or make notification for another source(Email, Messengers, etc.)

QueryTrack::Settings.configure do |config|
  config.duration = 0.5
  config.notifications.custom_handler = -> (sql, duration, trace) {
    # data processing...
  }
end

Production Usage Notes

When QueryTrack send slack hooks, request is executed in separate thread. So there should be no synchronous delays.

Subscription to SQL events and checking duration time actually take insignificant time in milliseconds.

If your project is horizontally scaled, you can install query_track for one of the node to avoid performance degrade for whole application.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kirillshevch/query_track.

License

The gem is available as open source under the terms of the MIT License.

query_track's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar evanboho avatar hosamaly avatar kirillshevch avatar mstate avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

query_track's Issues

Minimal supported version

It would be great to have some note about the minimal supported version.

ActiveSupport::Notifications.subscribe is a new feature.

Slack Notifications

I am using the same Incoming WebHooks ID for different stages.

I know when you create an Incoming WebHooks ID, it defaults to a channel. However other library like stevenosloan/slack-notifier, let you choose a channel when to send a notification.

Are we able to select a channel in this block?

QueryTrack::Settings.configure do |config|
end

New version bump to fix dry-core deprecation warnings?

Hi, thank you for fixing the dry gem deprecation warnings in 0f8233d! I was wondering if it was possible for you to release a new version of the gem with those changes, so I can get rid of those warnings in my app?

As a workaround I can point our Gemfile at the latest commit SHA, but it'd be better for us to be using official releases.

Queries not showing the correct source code location

Hi,

First of all, thanks for this awesome gem!

I noticed that every query that comes through shows the source code location below, instead of the actual source line of the query:

/app/vendor/bundle/ruby/2.6.0/gems/query_track-0.0.9/lib/query_track/event_processor.rb:17:in 'call'

Any idea what might be causing this? I'm on Rails 5.2.

Thanks!

how it works?

Hi
I add config/initializers/query_track.rb with code:

QueryTrack::Settings.configure do |config|
config.duration = 0.01 # for testing
config.logs = true
end

but i don't see logs in log/development.log
why doesn't it work? looks like i'm missing something

Request: Enable/Disable toggle

Hi!

It would be great if it was possible in config to enable/disable query_track completely. I can think of several use cases:

  • To easily enable/disable with ENV variables to turn it on/off without code push
  • Have it on in just some prod environments
  • Maybe even differ between dev/test/prod

WDYT?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.