rack-middleware-query_tracer 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6c02198ebe278a54ea01421ce4e7df4628e9e62d
4
+ data.tar.gz: 93584615ce58b9f9fd889c995f8c046e812d2af0
5
+ SHA512:
6
+ metadata.gz: 4465790bb53bac9000546c617424a1318dca39d236b19ade0d3e17b43bfed5e073b3c69dd177d4cd571ad95524809476bdc63675f496e63f55adac9a7daab605
7
+ data.tar.gz: a8e8a930737130d94d7fa8607e069ed0126c9c50c5f5c28099b3a0d3ab1035fd9df49eefba7f3c951177132a2ccac673b1b9b601e8653af2e9daf2df72ad6913
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Keiichiro Nagano
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # Rack::Middleware::QueryTracer
2
+
3
+ Dumps SQL queries with their positions in the source code. For debugging purposes.
4
+
5
+ ex:
6
+
7
+ ```
8
+ [QueryTracing] [/manage/users?utf8=%E2%9C%93&id=&code=&nickname=a&udid=&commit=Search] [QueryTracer] [/app/views/manage/users/_users_list.html.slim:16:in `_app_views_manage_users__users_list_html_slim__2039225012774348846_70304982918140'] SELECT COUNT(*) FROM `users` WHERE (nickname LIKE 'a%')
9
+ ```
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'rack-middleware-query_tracer'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install rack-middleware-query_tracer
26
+
27
+ ## Usage
28
+
29
+ (Rails app) Create config/initializers/query_tracer.rb as follows:
30
+
31
+ ```
32
+ Rails.application.config.middleware.use Rack::Middleware::QueryTracer, Rails.logger if Rails.env.development?
33
+ ```
34
+
35
+ (non-Rails app) Not tested. Patches are welcome.
36
+
37
+ ## Credits
38
+
39
+ @mitaku
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/knagano/rack-middleware-query_tracer/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,87 @@
1
+ require "rack/middleware/query_tracer/version"
2
+ require "arproxy"
3
+
4
+ module Rack
5
+ module Middleware
6
+ class QueryTracer
7
+ def initialize(app, logger = ::Arproxy.logger)
8
+ @app = app
9
+ @logger = logger
10
+
11
+ ::Arproxy.configure do |config|
12
+ config.adapter = ActiveRecord::Base.connection.adapter_name.downcase # "mysql2" # A DB Apdapter name which is used in your database.yml
13
+ config.use ProxyImpl, @logger
14
+ end
15
+ end
16
+
17
+ def call(env)
18
+ request = ActionDispatch::Request.new(env)
19
+ @logger.tagged("QueryTracing") do
20
+ ::Arproxy.enable!
21
+ begin
22
+ if request.filtered_path.blank?
23
+ @app.call(env)
24
+ else
25
+ @logger.tagged(request.filtered_path) do
26
+ @app.call(env)
27
+ end
28
+ end
29
+ ensure
30
+ ::Arproxy.disable!
31
+ end
32
+ end
33
+ end
34
+
35
+ class ProxyImpl < ::Arproxy::Base
36
+ def initialize(logger)
37
+ @logger = logger
38
+ end
39
+
40
+ def execute(sql, name = nil)
41
+ code_position_tag = "#{caller.select { |n| /\/app\// =~ n }[0].to_s.sub(/^.+?(\/app\/)/, '\1')}"
42
+ do_logging(code_position_tag, sql)
43
+ super(sql, name)
44
+ end
45
+
46
+ private
47
+
48
+ def do_logging(tag, sql)
49
+ @logger.tagged("QueryTracer") do
50
+ @logger.tagged(tag) do
51
+ @logger.info sql
52
+
53
+ case sql
54
+ when /SELECT/, /UPDATE/
55
+ case sql
56
+ when /1=0/
57
+ @logger.tagged("ALERT") do
58
+ @logger.info("1=0 QUERY: #{sql}")
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ #TODO
67
+ # def analyzer(sql, name=nil)
68
+ # case sql
69
+ # when /SELECT/, /UPDATE/
70
+ # explain_sql = "EXPLAIN #{sql}"
71
+ # _result = super(explain_sql, name)
72
+ #
73
+ # Rails.logger.tagged("EXPLAIN") do
74
+ # result = ActiveRecord::Result.new(_result.fields, _result.to_a)
75
+ # case sql
76
+ # when /1=0/
77
+ # Rails.logger.tagged("ALERT") do
78
+ # Rails.logger.info "1=0 QUERY: #{sql}"
79
+ # end
80
+ # end
81
+ # end
82
+ # end
83
+ # end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,7 @@
1
+ module Rack
2
+ module Middleware
3
+ class QueryTracer
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/middleware/query_tracer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-middleware-query_tracer"
8
+ spec.version = Rack::Middleware::QueryTracer::VERSION
9
+ spec.authors = ["Keiichiro Nagano"]
10
+ spec.email = ["knagano@CPAN.org"]
11
+ spec.summary = %q{Dumps SQL queries with their positions in the source code}
12
+ spec.description = %q{Dumps SQL queries with their positions in the source code}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "arproxy", "~> 0.2.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-middleware-query_tracer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Keiichiro Nagano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: arproxy
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Dumps SQL queries with their positions in the source code
56
+ email:
57
+ - knagano@CPAN.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/rack/middleware/query_tracer.rb
68
+ - lib/rack/middleware/query_tracer/version.rb
69
+ - rack-middleware-query_tracer.gemspec
70
+ homepage: ''
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.2.2
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Dumps SQL queries with their positions in the source code
94
+ test_files: []