marginalia 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of marginalia might be problematic. Click here for more details.
- data/.gitignore +3 -0
- data/.rbenv-version +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +63 -0
- data/LICENSE +20 -0
- data/README.md +68 -0
- data/Rakefile +34 -0
- data/init.rb +2 -0
- data/lib/marginalia/railtie.rb +53 -0
- data/lib/marginalia.rb +22 -0
- data/marginalia.gemspec +20 -0
- data/test/query_comments_test.rb +64 -0
- metadata +120 -0
data/.gitignore
ADDED
data/.rbenv-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3-p0
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
marginalia (1.0.0)
|
5
|
+
actionpack (>= 2.3, < 3.3)
|
6
|
+
activerecord (>= 2.3, < 3.3)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionpack (3.1.3)
|
12
|
+
activemodel (= 3.1.3)
|
13
|
+
activesupport (= 3.1.3)
|
14
|
+
builder (~> 3.0.0)
|
15
|
+
erubis (~> 2.7.0)
|
16
|
+
i18n (~> 0.6)
|
17
|
+
rack (~> 1.3.5)
|
18
|
+
rack-cache (~> 1.1)
|
19
|
+
rack-mount (~> 0.8.2)
|
20
|
+
rack-test (~> 0.6.1)
|
21
|
+
sprockets (~> 2.0.3)
|
22
|
+
activemodel (3.1.3)
|
23
|
+
activesupport (= 3.1.3)
|
24
|
+
builder (~> 3.0.0)
|
25
|
+
i18n (~> 0.6)
|
26
|
+
activerecord (3.1.3)
|
27
|
+
activemodel (= 3.1.3)
|
28
|
+
activesupport (= 3.1.3)
|
29
|
+
arel (~> 2.2.1)
|
30
|
+
tzinfo (~> 0.3.29)
|
31
|
+
activesupport (3.1.3)
|
32
|
+
multi_json (~> 1.0)
|
33
|
+
arel (2.2.1)
|
34
|
+
builder (3.0.0)
|
35
|
+
erubis (2.7.0)
|
36
|
+
hike (1.2.1)
|
37
|
+
i18n (0.6.0)
|
38
|
+
multi_json (1.1.0)
|
39
|
+
mysql (2.8.1)
|
40
|
+
mysql2 (0.3.11)
|
41
|
+
rack (1.3.6)
|
42
|
+
rack-cache (1.1)
|
43
|
+
rack (>= 0.4)
|
44
|
+
rack-mount (0.8.3)
|
45
|
+
rack (>= 1.0.0)
|
46
|
+
rack-test (0.6.1)
|
47
|
+
rack (>= 1.0)
|
48
|
+
rake (0.9.2.2)
|
49
|
+
sprockets (2.0.3)
|
50
|
+
hike (~> 1.2)
|
51
|
+
rack (~> 1.0)
|
52
|
+
tilt (~> 1.1, != 1.3.0)
|
53
|
+
tilt (1.3.3)
|
54
|
+
tzinfo (0.3.32)
|
55
|
+
|
56
|
+
PLATFORMS
|
57
|
+
ruby
|
58
|
+
|
59
|
+
DEPENDENCIES
|
60
|
+
mysql
|
61
|
+
mysql2
|
62
|
+
marginalia!
|
63
|
+
rake
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Copyright (c) 2012 37signals, LLC
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
# a copy of this software and associated documentation files (the
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
# the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be
|
12
|
+
# included in all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# marginalia
|
2
|
+
|
3
|
+
Attach comments to your ActiveRecord queries. By default, it adds the application, controller, and action names as a
|
4
|
+
comment at the end of each query.
|
5
|
+
|
6
|
+
This helps when searching log files for queries, and seeing where slow queries came from.
|
7
|
+
|
8
|
+
For example, once enabled, your logs will look like:
|
9
|
+
|
10
|
+
Account Load (0.3ms) SELECT `accounts`.* FROM `accounts`
|
11
|
+
WHERE `accounts`.`queenbee_id` = 1234567890
|
12
|
+
LIMIT 1
|
13
|
+
/*application:BCX,controller:project_imports,action:show*/
|
14
|
+
|
15
|
+
You can also these query comments with a tool like [pt-query-digest](http://www.percona.com/doc/percona-toolkit/2.1/pt-query-digest.html#query-reviews)
|
16
|
+
to automate identification of controllers and actions that are hotspots forslow queries.
|
17
|
+
|
18
|
+
This gem was created at 37signals. You can read more about how we use it [on
|
19
|
+
our blog](http://37signals.com/svn/posts/3130-tech-note-mysql-query-comments-in-rails).
|
20
|
+
|
21
|
+
This has been tested and used in production with both the mysql and mysql2 gems,
|
22
|
+
tested on Rails 2.3.5 through 3.2-stable. Patches are welcome for other database adapters.
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
### For Rails 3.x:
|
27
|
+
|
28
|
+
gem 'marginalia'
|
29
|
+
|
30
|
+
Then `bundle`, and that's it!
|
31
|
+
|
32
|
+
### For Rails 2.x:
|
33
|
+
|
34
|
+
If using cached externals, add to your `config/externals.yml` file.
|
35
|
+
|
36
|
+
Or, if your prefer using `config.gem`, you can use:
|
37
|
+
|
38
|
+
config.gem 'marginalia'
|
39
|
+
|
40
|
+
Finally, if bundled, you'll need to manually run the initialization step in an
|
41
|
+
initializer, e.g.:
|
42
|
+
|
43
|
+
# Gemfile
|
44
|
+
gem 'marginalia', :require => false
|
45
|
+
|
46
|
+
#config/initializers/marginalia.rb
|
47
|
+
require 'marginalia'
|
48
|
+
Marginalia::Railtie.insert
|
49
|
+
|
50
|
+
### Customization
|
51
|
+
Optionally, you can set the application name shown in the log like so in an initializer (e.g. `config/initializers/marginalia.rb`):
|
52
|
+
|
53
|
+
Marginalia.application_name = "BCX"
|
54
|
+
|
55
|
+
For Rails 3 applications, the name will default to your Rails application name.
|
56
|
+
For Rails 2 applications, "rails" is used as the default application name.
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Start by bundling and creating the test database:
|
61
|
+
|
62
|
+
bundle
|
63
|
+
rake db:create
|
64
|
+
|
65
|
+
Then, running `rake` will run the tests on both the `mysql` and `mysql2` adapters:
|
66
|
+
|
67
|
+
rake
|
68
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
task :default => ['test:all']
|
5
|
+
|
6
|
+
namespace :test do
|
7
|
+
desc "test all drivers"
|
8
|
+
task :all => [:mysql, :mysql2]
|
9
|
+
|
10
|
+
desc "test mysql driver"
|
11
|
+
task :mysql do
|
12
|
+
sh "DRIVER=mysql ruby -Ilib -Itest test/*_test.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "test mysql2 driver"
|
16
|
+
task :mysql2 do
|
17
|
+
sh "DRIVER=mysql2 ruby -Ilib -Itest test/*_test.rb"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
namespace :db do
|
22
|
+
desc "reset database"
|
23
|
+
task :reset => [:drop, :create]
|
24
|
+
|
25
|
+
desc "create database"
|
26
|
+
task :create do
|
27
|
+
sh 'mysql -u root -e "create database marginalia_test;"'
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "drop database"
|
31
|
+
task :drop do
|
32
|
+
sh 'mysql -u root -e "drop database marginalia_test;"'
|
33
|
+
end
|
34
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'marginalia'
|
2
|
+
|
3
|
+
module Marginalia
|
4
|
+
if defined? Rails::Railtie
|
5
|
+
require 'rails/railtie'
|
6
|
+
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
initializer 'marginalia.insert' do
|
9
|
+
ActiveSupport.on_load :active_record do
|
10
|
+
Marginalia::Railtie.insert_into_active_record
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveSupport.on_load :action_controller do
|
14
|
+
Marginalia::Railtie.insert_into_action_controller
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Railtie
|
21
|
+
def self.insert
|
22
|
+
insert_into_active_record
|
23
|
+
insert_into_action_controller
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.insert_into_action_controller
|
27
|
+
ActionController::Base.class_eval do
|
28
|
+
def record_query_comment
|
29
|
+
Marginalia.comment = "application:#{Marginalia.application_name || "rails"},controller:#{controller_name},action:#{action_name}"
|
30
|
+
yield
|
31
|
+
ensure
|
32
|
+
Marginalia.comment = nil
|
33
|
+
end
|
34
|
+
around_filter :record_query_comment
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.insert_into_active_record
|
39
|
+
if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
40
|
+
ActiveRecord::ConnectionAdapters::Mysql2Adapter.module_eval do
|
41
|
+
include Marginalia::ActiveRecordInstrumentation
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
if defined? ActiveRecord::ConnectionAdapters::MysqlAdapter
|
46
|
+
ActiveRecord::ConnectionAdapters::MysqlAdapter.module_eval do
|
47
|
+
include Marginalia::ActiveRecordInstrumentation
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/marginalia.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'marginalia/railtie'
|
2
|
+
|
3
|
+
module Marginalia
|
4
|
+
mattr_accessor :comment, :application_name
|
5
|
+
|
6
|
+
module ActiveRecordInstrumentation
|
7
|
+
def self.included(instrumented_class)
|
8
|
+
if defined? Rails.application
|
9
|
+
Marginalia.application_name = Rails.application.class.name.split("::").first
|
10
|
+
end
|
11
|
+
instrumented_class.class_eval do
|
12
|
+
alias_method :execute_without_marginalia, :execute
|
13
|
+
alias_method :execute, :execute_with_marginalia
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def execute_with_marginalia(sql, name = nil)
|
18
|
+
execute_without_marginalia("#{sql} /*#{Marginalia.comment}*/", name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/marginalia.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.authors = ["Noah Lorang", "Nick Quaranto", "Taylor Weibley"]
|
3
|
+
gem.email = ["noah@37signals.com"]
|
4
|
+
gem.homepage = "https://github.com/37signals/marginalia"
|
5
|
+
|
6
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
7
|
+
gem.files = `git ls-files`.split("\n")
|
8
|
+
gem.test_files = `git ls-files -- {test}/*`.split("\n")
|
9
|
+
gem.name = "marginalia"
|
10
|
+
gem.require_paths = ["lib"]
|
11
|
+
gem.version = "1.0.0"
|
12
|
+
|
13
|
+
gem.add_runtime_dependency "actionpack", ">= 2.3", "< 3.3"
|
14
|
+
gem.add_runtime_dependency "activerecord", ">= 2.3", "< 3.3"
|
15
|
+
gem.add_development_dependency "rake"
|
16
|
+
gem.add_development_dependency "mysql"
|
17
|
+
gem.add_development_dependency "mysql2"
|
18
|
+
|
19
|
+
gem.summary = description = %q{Attach comments to your ActiveRecord queries.}
|
20
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'logger'
|
3
|
+
require 'pp'
|
4
|
+
require 'active_record'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'marginalia'
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection({
|
9
|
+
:adapter => ENV["DRIVER"] || "mysql",
|
10
|
+
:host => "localhost",
|
11
|
+
:username => "root",
|
12
|
+
:database => "marginalia_test"
|
13
|
+
})
|
14
|
+
|
15
|
+
class Post < ActiveRecord::Base
|
16
|
+
end
|
17
|
+
|
18
|
+
class PostsController < ActionController::Base
|
19
|
+
def driver_only
|
20
|
+
ActiveRecord::Base.connection.execute "select id from posts"
|
21
|
+
render :nothing => true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
unless Post.table_exists?
|
26
|
+
ActiveRecord::Schema.define do
|
27
|
+
create_table "posts", :force => true do |t|
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Marginalia::Railtie.insert
|
33
|
+
|
34
|
+
class MarginaliaTest < Test::Unit::TestCase
|
35
|
+
def setup
|
36
|
+
@queries = []
|
37
|
+
ActiveSupport::Notifications.subscribe "sql.active_record" do |*args|
|
38
|
+
@queries << args.last[:sql]
|
39
|
+
end
|
40
|
+
@env = Rack::MockRequest.env_for('/')
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_query_commenting_on_mysql_driver_with_no_action
|
44
|
+
ActiveRecord::Base.connection.execute "select id from posts"
|
45
|
+
assert_match %r{select id from posts /\*\*/$}, @queries.first
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_query_commenting_on_mysql_driver_with_action
|
49
|
+
PostsController.action(:driver_only).call(@env)
|
50
|
+
assert_match %r{select id from posts /\*application:rails,controller:posts,action:driver_only\*/$}, @queries.first
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_configuring_application
|
54
|
+
Marginalia.application_name = "customapp"
|
55
|
+
PostsController.action(:driver_only).call(@env)
|
56
|
+
|
57
|
+
assert_match %r{/\*application:customapp,controller:posts,action:driver_only\*/$}, @queries.first
|
58
|
+
end
|
59
|
+
|
60
|
+
def teardown
|
61
|
+
Marginalia.application_name = nil
|
62
|
+
ActiveSupport::Notifications.unsubscribe "sql.active_record"
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: marginalia
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Noah Lorang
|
9
|
+
- Nick Quaranto
|
10
|
+
- Taylor Weibley
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2012-04-24 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: actionpack
|
18
|
+
requirement: &13238580 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ! '>='
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '2.3'
|
24
|
+
- - <
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.3'
|
27
|
+
type: :runtime
|
28
|
+
prerelease: false
|
29
|
+
version_requirements: *13238580
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: &13237460 !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.3'
|
38
|
+
- - <
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.3'
|
41
|
+
type: :runtime
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: *13237460
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: rake
|
46
|
+
requirement: &13236300 !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
type: :development
|
53
|
+
prerelease: false
|
54
|
+
version_requirements: *13236300
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mysql
|
57
|
+
requirement: &13235060 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: *13235060
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: mysql2
|
68
|
+
requirement: &13234220 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
type: :development
|
75
|
+
prerelease: false
|
76
|
+
version_requirements: *13234220
|
77
|
+
description:
|
78
|
+
email:
|
79
|
+
- noah@37signals.com
|
80
|
+
executables: []
|
81
|
+
extensions: []
|
82
|
+
extra_rdoc_files: []
|
83
|
+
files:
|
84
|
+
- .gitignore
|
85
|
+
- .rbenv-version
|
86
|
+
- Gemfile
|
87
|
+
- Gemfile.lock
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- init.rb
|
92
|
+
- lib/marginalia.rb
|
93
|
+
- lib/marginalia/railtie.rb
|
94
|
+
- marginalia.gemspec
|
95
|
+
- test/query_comments_test.rb
|
96
|
+
homepage: https://github.com/37signals/marginalia
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.11
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Attach comments to your ActiveRecord queries.
|
120
|
+
test_files: []
|