active_record_stats 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d8b4a993bf23d773e272eb4a8edcc7d503727714
4
+ data.tar.gz: f603d3de05cdf67455242909c058651610582737
5
+ SHA512:
6
+ metadata.gz: 57dbb243d4be150fbdacf18d66cf7bd1f1de3b82909739b322db29bba7905b69b212f6283f2bf01df60aba408a4c2160e6676d531a168f54a05233146b9bed7b
7
+ data.tar.gz: f7a4df44e3fcf1047f1703395bcfb87501d023eac85a748d0e0b0f3f306106e3284768eb2908784187038c6fb2a59acf61981f7c55921d89e8b83d808f796050
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 2.3.0
6
+
7
+ before_install:
8
+ - gem install bundler -v 1.11.2
9
+ - bin/setup
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_record_stats.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kyle Hargraves
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # ActiveRecordStats
2
+
3
+ Provides Rails, Resque, and Sidekiq integrations for emitting metrics
4
+ about ActiveRecord usage to StatsD using Shopify's [statsd-instrument][].
5
+
6
+ ## Installation
7
+
8
+ As usual:
9
+
10
+ ~~~ruby
11
+ gem 'active_record_stats', require: false
12
+ ~~~
13
+
14
+ This library assumes that statsd has already been configured, and is
15
+ available via the `StatsD` constant.
16
+
17
+ ## Usage
18
+
19
+ For Rails web request instrumentation, add the following to your
20
+ `config/application.rb`:
21
+
22
+ ~~~ruby
23
+ require 'active_record_stats/rack_middleware'
24
+
25
+ module YourApp
26
+ class Application < Rails::Application
27
+ config.middleware.use 'ActiveRecordStats::RackMiddleware'
28
+ end
29
+ end
30
+ ~~~
31
+
32
+ For Resque job instrumentation, `extend` your job classes like so:
33
+
34
+ ~~~ruby
35
+ require 'active_record_stats/resque_plugin'
36
+
37
+ class SomeJob
38
+ extend ActiveRecordStats::ResquePlugin
39
+ end
40
+ ~~~
41
+
42
+ For Sidekiq instrumentation, enable the server middleware:
43
+
44
+ ~~~ruby
45
+ require 'active_record_stats/sidekiq_server_middleware'
46
+
47
+ Sidekiq.configure_server do |config|
48
+ config.server_middleware do |chain|
49
+ chain.add ActiveRecordStats::SidekiqServerMiddleware
50
+ end
51
+ end
52
+ ~~~
53
+
54
+ ## Metric names
55
+
56
+ Metrics will be with names in the following formats:
57
+
58
+ 1. Gauge db.web._controller_._action_._statement_: the number of `statement` queries
59
+ performed for a given controller action.
60
+ 2. Timer db.web._controller_._action_.runtime: the total `db_runtime` cost of
61
+ a given controller action, in milliseconds.
62
+ 3. Gauge db.job._class_._statement_: the number of `statement` queries performed
63
+ for a given job.
64
+
65
+ As Graphite metric names may not contain a `/` character, they are replaced with
66
+ two underscores (`__`). For sanity's sake, the same is done for `::` delimiters.
67
+
68
+ Example metrics that might be emitted:
69
+
70
+ * `db.web.v1__widgets.update.UPDATE:2|g`: two `UPDATE` statements were issued
71
+ when processing `V1::WidgetsController#update`.
72
+ * `db.web.posts.create.runtime:123|g`: Rails reported the total time spent
73
+ in ActiveRecord for the `PostsController#create` action as 123 ms.
74
+ * `db.job.cache__prime.SELECT:10|g`: ten `SELECT` statements were issued when
75
+ processing the `Cache::Prime` job.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
81
+ [statsd-instrument]: https://github.com/Shopify/statsd-instrument
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ namespace :db do
9
+ desc "Use psql's `createdb` to create the test database"
10
+ task :create do
11
+ sh "createdb active_record_stats_test"
12
+ end
13
+
14
+ desc "Use psql's `dropdb` to drop the test database"
15
+ task :drop do
16
+ sh "dropdb active_record_stats_test"
17
+ end
18
+
19
+ desc "db:{drop,create}"
20
+ task reset: %w[db:drop db:create]
21
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record_stats/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_record_stats"
8
+ spec.version = ActiveRecordStats::VERSION
9
+ spec.authors = ["Kyle Hargraves"]
10
+ spec.email = ["khargraves@enova.com"]
11
+
12
+ spec.summary = %q{Emit ActiveRecord query counts and runtime to StatsD}
13
+ spec.description = %q{ActiveRecord instrumentation for Rails, Resque and Sidekiq}
14
+ spec.homepage = "https://github.com/enova/active_record_stats"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency 'activerecord', '~> 4.0'
23
+ spec.add_runtime_dependency 'statsd-instrument', '~> 2.0', '>= 2.0.4'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency 'pry', '~> 0.10'
29
+
30
+ spec.add_development_dependency 'pg', '~> 0.18'
31
+ spec.add_development_dependency 'actionpack', '~> 4.0'
32
+ spec.add_development_dependency 'rack-test', '~> 0.6'
33
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_record_stats"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+ bundle exec rake db:create
@@ -0,0 +1,15 @@
1
+ require 'active_record_stats/version'
2
+ require 'active_record'
3
+ require 'statsd-instrument'
4
+
5
+ module ActiveRecordStats
6
+ def self.statement_type(sql)
7
+ return if sql.nil?
8
+
9
+ cleaned = sql.gsub(/^\s*(?:--.*)?$/, '').strip
10
+ return if cleaned.empty?
11
+
12
+ type = cleaned.split(' ', 2).first
13
+ type.try(:upcase)
14
+ end
15
+ end
@@ -0,0 +1,61 @@
1
+ require 'active_record_stats'
2
+ require 'active_support/notifications'
3
+ require 'action_dispatch/http/parameters'
4
+
5
+ module ActiveRecordStats
6
+ class RackMiddleware
7
+ # The location in the Rack `env` where ActionDispatch stores its
8
+ # `parameters` value. This _may_ change across Rails versions, but
9
+ # I am not aware of any more reliable means of retrieving it.
10
+ ENV_KEY = 'action_dispatch.request.parameters'.freeze
11
+
12
+ def initialize(app)
13
+ @app = app
14
+ end
15
+
16
+ def call(env)
17
+ totals = {}
18
+ db_time = 0
19
+
20
+ gather_sql = ->(_name, _started_at, _finished_at, _unique_id, payload) {
21
+ return if payload[:name] == 'SCHEMA' || payload[:sql].blank?
22
+ return unless type = ActiveRecordStats.statement_type(payload[:sql])
23
+ totals[type] ||= 0
24
+ totals[type] += 1
25
+ }
26
+
27
+ gather_runtime = ->(_name, _started_at, _finished_at, _unique_id, payload) {
28
+ db_time = payload[:db_runtime]
29
+ }
30
+
31
+ subs = [
32
+ ActiveSupport::Notifications.subscribe('sql.active_record', &gather_sql),
33
+ ActiveSupport::Notifications.subscribe('process_action.action_controller', &gather_runtime)
34
+ ]
35
+
36
+ @app.call(env)
37
+
38
+ ensure
39
+ request_params = env[ENV_KEY]
40
+ if controller = request_params['controller']
41
+ controller = controller.gsub('/', '__')
42
+ action = request_params['action']
43
+ emit(controller, action, db_time, totals)
44
+ end
45
+
46
+ subs.each do |sub|
47
+ ActiveSupport::Notifications.unsubscribe(sub)
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def emit(controller, action, db_time, totals)
54
+ totals.each do |verb, count|
55
+ StatsD.gauge "db.web.#{controller}.#{action}.#{verb}", count
56
+ end
57
+
58
+ StatsD.measure "db.web.#{controller}.#{action}.runtime", db_time
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_record_stats'
2
+
3
+ module ActiveRecordStats
4
+ module ResquePlugin
5
+ def around_perform_active_record_stats(*args, &block)
6
+ totals = {}
7
+
8
+ gather_sql = ->(_name, _started_at, _finished_at, _unique_id, payload) {
9
+ return if payload[:name] == 'SCHEMA' || payload[:sql].blank?
10
+ return unless type = ActiveRecordStats.statement_type(payload[:sql])
11
+ totals[type] ||= 0
12
+ totals[type] += 1
13
+ }
14
+
15
+ sub = ActiveSupport::Notifications.subscribe('sql.active_record', &gather_sql)
16
+ yield
17
+
18
+ ensure
19
+ emit_active_record_stats(name, totals)
20
+ ActiveSupport::Notifications.unsubscribe(sub)
21
+ end
22
+
23
+ private
24
+
25
+ def emit_active_record_stats(name, totals)
26
+ job = name.underscore.gsub('/', '__')
27
+ totals.each do |verb, count|
28
+ StatsD.gauge "db.job.#{job}.#{verb}", count
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_record_stats'
2
+
3
+ module ActiveRecordStats
4
+ class SidekiqServerMiddleware
5
+ def call(worker, job, queue)
6
+ totals = {}
7
+
8
+ gather_sql = ->(_name, _started_at, _finished_at, _unique_id, payload) {
9
+ return if payload[:name] == 'SCHEMA' || payload[:sql].blank?
10
+ return unless type = ActiveRecordStats.statement_type(payload[:sql])
11
+ totals[type] ||= 0
12
+ totals[type] += 1
13
+ }
14
+
15
+ sub = ActiveSupport::Notifications.subscribe('sql.active_record', &gather_sql)
16
+ yield
17
+
18
+ ensure
19
+ emit(worker.class.to_s, totals)
20
+ ActiveSupport::Notifications.unsubscribe(sub)
21
+ end
22
+
23
+ private
24
+
25
+ def emit(worker_name, totals)
26
+ job = worker_name.underscore.gsub('/', '__')
27
+ totals.each do |verb, count|
28
+ StatsD.gauge "db.job.#{job}.#{verb}", count
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordStats
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,193 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Hargraves
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: statsd-instrument
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 2.0.4
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '2.0'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.0.4
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.11'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.11'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: pry
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.10'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.10'
103
+ - !ruby/object:Gem::Dependency
104
+ name: pg
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.18'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.18'
117
+ - !ruby/object:Gem::Dependency
118
+ name: actionpack
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '4.0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '4.0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: rack-test
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '0.6'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - "~>"
143
+ - !ruby/object:Gem::Version
144
+ version: '0.6'
145
+ description: ActiveRecord instrumentation for Rails, Resque and Sidekiq
146
+ email:
147
+ - khargraves@enova.com
148
+ executables: []
149
+ extensions: []
150
+ extra_rdoc_files: []
151
+ files:
152
+ - ".gitignore"
153
+ - ".rspec"
154
+ - ".ruby-version"
155
+ - ".travis.yml"
156
+ - Gemfile
157
+ - LICENSE.txt
158
+ - README.md
159
+ - Rakefile
160
+ - active_record_stats.gemspec
161
+ - bin/console
162
+ - bin/setup
163
+ - lib/active_record_stats.rb
164
+ - lib/active_record_stats/rack_middleware.rb
165
+ - lib/active_record_stats/resque_plugin.rb
166
+ - lib/active_record_stats/sidekiq_server_middleware.rb
167
+ - lib/active_record_stats/version.rb
168
+ homepage: https://github.com/enova/active_record_stats
169
+ licenses:
170
+ - MIT
171
+ metadata: {}
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ requirements: []
187
+ rubyforge_project:
188
+ rubygems_version: 2.5.1
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: Emit ActiveRecord query counts and runtime to StatsD
192
+ test_files: []
193
+ has_rdoc: