Giter VIP home page Giter VIP logo

sql_origin's Introduction

SQL:Origin

Adds backtraces to your SQL queries and query logs, so you know where your queries are coming from. This only works with Rails, and is only tested with Rails 4.0.

Why do I want this?

Simple. To turn this

Without backtrace logging

into this.

With backtrace logging

So now, you needn't wonder where that odd-looking or broken SQL query is coming from.

It can also turn this

Reading mysql slow query log from /usr/local/mysql/data/mysqld51-apple-slow.log
Count: 1  Time=4.32s (4s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT "events".* FROM "events" WHERE "events"."bug_id" = ?

Count: 3  Time=2.53s (7s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT "deploys".* FROM "deploys" WHERE "deploys"."id" = ?

Count: 3  Time=2.13s (6s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT * FROM "slugs" WHERE (LOWER("slugs"."slug") = LOWER(?) AND "slugs"."scope" IS NULL AND "slugs"."sluggable_type" = ?) LIMIT 1

into this.

Reading mysql slow query log from /usr/local/mysql/data/mysqld51-apple-slow.log
Count: 1  Time=4.32s (4s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT "events".* FROM "events" WHERE "events"."bug_id" = ? /* app/models/project.rb:125:in `_callback_after_617' */

Count: 3  Time=2.53s (7s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT "deploys".* FROM "deploys" WHERE "deploys"."id" = ? /* app/controllers/projects_controller.rb:359:in `require_or_load' */

Count: 3  Time=2.13s (6s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 SELECT * FROM "slugs" WHERE (LOWER("slugs"."slug") = LOWER(?) AND "slugs"."scope" IS NULL AND "slugs"."sluggable_type" = ?) LIMIT 1 /* app/models/observers/bug_observer.rb:23:in `create_open_event' */

Installation

To use, add SQL:Origin to your Gemfile:

gem 'sql_origin'

If you would like to add three-line backtraces below every SQL query in your Rails log, add

SQLOrigin.append_to_log

somewhere in your Rails initialization (e.g., application.rb or a config/initializer file).

If you would like to add a one-line backtrace comment to every SQL query, add

SQLOrigin.append_to_query

somewhere in your Rails initialization.

It would be typical to enable append_to_log for development and test, and append_to_query for production, in order to keep production logs small.

Backtrace Filtering

By default, files not under your Rails root, and files under vendor, are filtered from your backtrace. If you need to filter other files, add them to {SQLOrigin::LIBRARY_PATHS}:

SQLOrigin::LIBRARY_PATHS << 'config/initializers/active_record_hacks.rb'

sql_origin's People

Contributors

riscfuture avatar stefschenkelaars avatar srajkovic avatar

Stargazers

Michael Corrado avatar Anthony Scalisi avatar sufleR avatar Kévin Le Roy avatar Jason Kay avatar Paweł Kubicki avatar dryaf avatar           ​ ⁠             ⁠   Ån̶t̶hia¯₣ab̷r̷ε  ⚜ avatar Mihai Târnovan avatar Vladimir Vinnikov avatar Tomas Markauskas avatar Jens Krämer avatar Justinas L. avatar

Watchers

Nihad Abbasov avatar dryaf avatar James Cloos avatar  avatar

sql_origin's Issues

Duplicate log lines in rails 4.1

After I upgraded our Rails 4 app to 4.1, I noticed that all sql logs are duplicated. If I remove the SqlOrigin gem, the duplicate lines go away.

With SqlOrigin:

Processing by AccountController#index as HTML
  User Load (0.3ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 171  ORDER BY `users`.`id` ASC LIMIT 1
    bin/rails:8:in `require'
    bin/rails:8:in `<top (required)>'
    bin/spring:16:in `<top (required)>'
  User Load (0.8ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 171  ORDER BY `users`.`id` ASC LIMIT 1
    bin/rails:8:in `require'
    bin/rails:8:in `<top (required)>'
    bin/spring:16:in `<top (required)>'
  UserEmail Load (0.2ms)  SELECT `user_emails`.* FROM `user_emails`  WHERE `user_emails`.`user_id` = 171
    app/models/user.rb:51:in `confirmed?'
    app/models/user.rb:136:in `active_for_authentication_with_confirmed?'
    bin/rails:8:in `require'
  UserEmail Load (0.6ms)  SELECT `user_emails`.* FROM `user_emails`  WHERE `user_emails`.`user_id` = 171
    app/models/user.rb:51:in `confirmed?'
    app/models/user.rb:136:in `active_for_authentication_with_confirmed?'
    bin/rails:8:in `require'

Without SqlOrigin gem:

Processing by AccountController#index as HTML
  User Load (0.3ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 171  ORDER BY `users`.`id` ASC LIMIT 1
  UserEmail Load (0.2ms)  SELECT `user_emails`.* FROM `user_emails`  WHERE `user_emails`.`user_id` = 171

Oddly, I see that the times are different too, so perhaps it's actually making rails make duplicate calls?

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.