rorvswild 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b033cdb12c4cf5bfdb8b9fc3f983a45767a128e
4
+ data.tar.gz: 3e606b32e0f287eb0e50ff6497e529e1f7cee78b
5
+ SHA512:
6
+ metadata.gz: 4498c3532133cdab3aad97df6c26a2104ea96f983ff119bdf9243efedea3d27e0be68b925a477422bee6979b48fab6b739749b77a4483c8a5ee8d1979e26a168
7
+ data.tar.gz: 04373c6b6c67c44270dd5492a7e13adada6b063177eab35eb9ae38473d3a097ed38e5d7b20716269caff5e55c9674045a5bff000d4b157e22b1e28a5234340ec
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rorvswild.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexis Bernard
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rorvswild
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rorvswild'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rorvswild
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/rorvswild/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ class Rorvswild
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rorvswild.rb ADDED
@@ -0,0 +1,139 @@
1
+ require "rorvswild/version"
2
+
3
+ class RorVsWild
4
+ def self.default_config
5
+ {
6
+ api_url: "http://www.rorvswild.com/api",
7
+ sql_threshold: 500
8
+ }
9
+ end
10
+
11
+ attr_reader :api_url, :api_key, :app_id, :sql_threshold, :error, :request
12
+
13
+ def initialize(config)
14
+ config = RorVsWild.default_config.merge(config)
15
+ @sql_threshold = config[:sql_threshold]
16
+ @api_url = config[:api_url]
17
+ @api_key = config[:api_key]
18
+ @app_id = config[:app_id]
19
+ setup_callbacks
20
+ end
21
+
22
+ def setup_callbacks
23
+ ApplicationController.rescue_from(StandardError, &method(:after_exception))
24
+ ActiveSupport::Notifications.subscribe("sql.active_record", &method(:after_sql_query))
25
+ ActiveSupport::Notifications.subscribe("render_template.action_view", &method(:after_view_rendering))
26
+ ActiveSupport::Notifications.subscribe("process_action.action_controller", &method(:after_http_request))
27
+ ActiveSupport::Notifications.subscribe("start_processing.action_controller", &method(:before_http_request))
28
+ end
29
+
30
+ def before_http_request(name, start, finish, id, payload)
31
+ @request = {controller: payload[:controller], action: payload[:action], path: payload[:path]}
32
+ @queries = []
33
+ @views = []
34
+ @error = nil
35
+ end
36
+
37
+ def after_http_request(name, start, finish, id, payload)
38
+ request[:db_runtime] = (payload[:db_runtime] || 0).round
39
+ request[:view_runtime] = (payload[:view_runtime] || 0).round
40
+ request[:other_runtime] = compute_duration(start, finish) - request[:db_runtime] - request[:view_runtime]
41
+ request[:params] = params_filter.filter(payload[:params]) if error
42
+ Thread.new { post_request }
43
+ rescue => ex
44
+ Rails.logger.error("[RorVsWild] " + ex.inspect)
45
+ Rails.logger.error("[RorVsWild] " + ex.backtrace.join("\n[RorVsWild] "))
46
+ end
47
+
48
+ def after_sql_query(name, start, finish, id, payload)
49
+ return if !queries || payload[:name] == "EXPLAIN".freeze
50
+ runtime, sql, plan = compute_duration(start, finish), nil, nil
51
+ file, line, method = extract_file_and_line_from_call_stack(caller)
52
+ plan = explain(sql = payload[:sql], payload[:binds]) if runtime > sql_threshold
53
+ queries << {file: file, line: line, sql: sql, plan: plan, runtime: runtime}
54
+ rescue => ex
55
+ Rails.logger.error("[RorVsWild] " + ex.inspect)
56
+ Rails.logger.error("[RorVsWild] " + ex.backtrace.join("\n[RorVsWild] "))
57
+ end
58
+
59
+ def after_view_rendering(name, start, finish, id, payload)
60
+ views << {file: relative_path(payload[:identifier]), runtime: compute_duration(start, finish)} if views
61
+ end
62
+
63
+ def after_exception(exception)
64
+ file, line = exception.backtrace.first.split(":")
65
+ @error = {
66
+ exception: exception.class.to_s,
67
+ backtrace: exception.backtrace,
68
+ message: exception.message,
69
+ file: relative_path(file),
70
+ line: line.to_i
71
+ }
72
+ raise exception
73
+ end
74
+
75
+ #######################
76
+ ### Private methods ###
77
+ #######################
78
+
79
+ private
80
+
81
+ def queries
82
+ @queries
83
+ end
84
+
85
+ def views
86
+ @views
87
+ end
88
+
89
+ def slowest_views
90
+ views.sort { |h1, h2| h2[:runtime] <=> h1[:runtime] }[0, 25]
91
+ end
92
+
93
+ def slowest_queries
94
+ queries.sort { |h1, h2| h2[:runtime] <=> h1[:runtime] }[0, 25]
95
+ end
96
+
97
+ def explain(sql, binds)
98
+ rows = ActiveRecord::Base.connection.exec_query("EXPLAIN " + sql, "EXPLAIN", binds)
99
+ rows.map { |row| row["QUERY PLAN"] }.join("\n")
100
+ rescue => ex
101
+ end
102
+
103
+ def post_request
104
+ post("/requests", request: request.merge(queries: slowest_queries, views: slowest_views, error: error))
105
+ end
106
+
107
+ def extract_file_and_line_from_call_stack(stack)
108
+ file, line, method = (stack.find { |str| str.include?(Rails.root.to_s) } ||
109
+ stack.find { |str| str.include?("(irb):") }).split(":")
110
+ file.sub!(Rails.root.to_s, "")
111
+ method.sub!("block in ", "")
112
+ method.sub!("in `", "")
113
+ method.sub!("'", "")
114
+ [file, line, method]
115
+ end
116
+
117
+ def compute_duration(start, finish)
118
+ ((finish - start) * 1000).round
119
+ end
120
+
121
+ def relative_path(path)
122
+ path.sub(Rails.root.to_s, "")
123
+ end
124
+
125
+ def post(path, data)
126
+ uri = URI(api_url + path)
127
+ Net::HTTP.start(uri.host, uri.port) do |http|
128
+ post = Net::HTTP::Post.new(uri)
129
+ post.content_type = "application/json"
130
+ post.basic_auth(app_id, api_key)
131
+ post.body = data.to_json
132
+ http.request(post)
133
+ end
134
+ end
135
+
136
+ def params_filter
137
+ @params_filter ||= ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters)
138
+ end
139
+ end
data/rorvswild.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rorvswild/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rorvswild"
8
+ spec.version = Rorvswild::VERSION
9
+ spec.authors = ["Alexis Bernard"]
10
+ spec.email = ["alexis@bernard.io"]
11
+ spec.summary = "RorVsWild tracks response time of your Rails application."
12
+ spec.description = "RorVsWild helps you to improve the response time, by pointing out any slow request, query and view."
13
+ spec.homepage = "http://www.rorvswild.com"
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_development_dependency "bundler", "~> 1.6"
22
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rorvswild
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexis Bernard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ description: RorVsWild helps you to improve the response time, by pointing out any
28
+ slow request, query and view.
29
+ email:
30
+ - alexis@bernard.io
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/rorvswild.rb
41
+ - lib/rorvswild/version.rb
42
+ - rorvswild.gemspec
43
+ homepage: http://www.rorvswild.com
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.2.2
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: RorVsWild tracks response time of your Rails application.
67
+ test_files: []