rorvswild 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c19e1237b3b9c450b21a294e2c13f7aa2d5ff2ee
4
- data.tar.gz: 485ddeb9246497e3994ee81805b2ba3e26f084a9
3
+ metadata.gz: a59fa9bea7794855a7efa8ba8421e6d9aa5cfd07
4
+ data.tar.gz: 2989fc278d1ad9c9ef1a3c308763154ee05b349e
5
5
  SHA512:
6
- metadata.gz: f36090b6aa9a61a8846cc7c6a3ade26ed05181a18b088cc88b8ae78c3cd8ec83496749bffa7a5947edd531e9eb87f1c5bb2b618360bc4bfe8897642a60a549b7
7
- data.tar.gz: 5330075509becd02e23b9fa0599426ee8ba05d46fe8f9524cb483315662aac8d21591fa865151b3b980cb50c716893c01d5ff0fb3e3f305e5789e8c7443e3538
6
+ metadata.gz: 0510261d172670aa8a3b4e9ec9536a5a84b010464560e4d2f268d6a43b3f1d2c86fa80c9e302da2f8999dec748007fbef31397a151113805e4c947d3371bcd4f
7
+ data.tar.gz: 3429bf64bc6895e0aff2258d3fa621d1ccd46c9cbc483d6fc15df67f5b68058177d983707662df1e629c9f44d53dec6770fb3305279fc8daa5f787c78b6039ff
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in rorvswild.gemspec
4
4
  gemspec
5
+
6
+ gem "mocha"
7
+ gem "top_tests"
@@ -1,3 +1,3 @@
1
1
  class Rorvswild
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/rorvswild.rb CHANGED
@@ -2,9 +2,22 @@ require "rorvswild/version"
2
2
 
3
3
  module RorVsWild
4
4
  def self.new(*args)
5
+ warn "WARNING: RorVsWild.new is deprecated. Use RorVsWild::Client.new instead."
5
6
  Client.new(*args) # Compatibility with 0.0.1
6
7
  end
7
8
 
9
+ def self.register_default_client(client)
10
+ @default_client = client
11
+ end
12
+
13
+ def self.default_client
14
+ @default_client
15
+ end
16
+
17
+ def self.measure_job(code)
18
+ default_client ? default_client.measure_job(code) : eval(code)
19
+ end
20
+
8
21
  class Client
9
22
  def self.default_config
10
23
  {
@@ -24,6 +37,7 @@ module RorVsWild
24
37
  @api_key = config[:api_key]
25
38
  @app_id = config[:app_id]
26
39
  setup_callbacks
40
+ RorVsWild.register_default_client(self)
27
41
  end
28
42
 
29
43
  def setup_callbacks
@@ -32,8 +46,8 @@ module RorVsWild
32
46
  ActiveSupport::Notifications.subscribe("process_action.action_controller", &method(:after_http_request))
33
47
  ActiveSupport::Notifications.subscribe("start_processing.action_controller", &method(:before_http_request))
34
48
 
35
- this = self
36
- ApplicationController.rescue_from(StandardError) { |exception| this.after_exception(exception, self) }
49
+ client = self
50
+ ActionController::Base.rescue_from(StandardError) { |exception| client.after_exception(exception, self) }
37
51
  end
38
52
 
39
53
  def before_http_request(name, start, finish, id, payload)
@@ -93,6 +107,33 @@ module RorVsWild
93
107
  raise exception
94
108
  end
95
109
 
110
+ def measure_job(code)
111
+ @queries = []
112
+ @job = {name: code}
113
+ started_at = Time.now
114
+ cpu_time_offset = cpu_time
115
+ eval(code)
116
+ rescue => exception
117
+ file, line = exception.backtrace.first.split(":")
118
+ job[:error] = {
119
+ line: line.to_i,
120
+ file: relative_path(file),
121
+ message: exception.message,
122
+ backtrace: exception.backtrace,
123
+ exception: exception.class.to_s,
124
+ }
125
+ raise
126
+ ensure
127
+ job[:runtime] = (Time.now - started_at) * 1000
128
+ job[:cpu_runtime] = cpu_time - cpu_time_offset
129
+ post_job
130
+ end
131
+
132
+ def cpu_time
133
+ time = Process.times
134
+ time.utime + time.stime + time.cutime + time.cstime
135
+ end
136
+
96
137
  #######################
97
138
  ### Private methods ###
98
139
  #######################
@@ -107,6 +148,10 @@ module RorVsWild
107
148
  @views
108
149
  end
109
150
 
151
+ def job
152
+ @job
153
+ end
154
+
110
155
  def push_query(query)
111
156
  if query[:sql] || query[:plan]
112
157
  queries << query
@@ -140,6 +185,12 @@ module RorVsWild
140
185
  log_error(exception)
141
186
  end
142
187
 
188
+ def post_job
189
+ post("/jobs", job: job.merge(queries: slowest_queries))
190
+ rescue => exception
191
+ log_error(exception)
192
+ end
193
+
143
194
  def extract_file_and_line_from_call_stack(stack)
144
195
  return unless location = stack.find { |str| str.include?(Rails.root.to_s) }
145
196
  file, line, method = location.split(":")
@@ -0,0 +1,51 @@
1
+ root_path = File.expand_path(File.dirname(File.dirname(__FILE__)))
2
+ $LOAD_PATH.unshift(root_path + "/lib")
3
+
4
+ require "rorvswild"
5
+
6
+ require "minitest/autorun"
7
+ require 'mocha/mini_test'
8
+ require "top_tests"
9
+
10
+ class RorVsWildTest < MiniTest::Unit::TestCase
11
+ include TopTests
12
+
13
+ def test_measure_job
14
+ client.expects(:post_job)
15
+ assert_equal(2, client.measure_job("1 + 1"))
16
+ assert_equal("1 + 1", client.send(:job)[:name])
17
+ assert(client.send(:job)[:runtime] > 0)
18
+ assert_equal(0, client.send(:job)[:cpu_runtime])
19
+ end
20
+
21
+ def test_measure_job_when_raising
22
+ client.expects(:post_job)
23
+ assert_raises(RuntimeError) { client.measure_job("raise 'error'") }
24
+ assert_equal(("raise 'error'"), client.send(:job)[:name])
25
+ assert(client.send(:job)[:cpu_runtime])
26
+ assert(client.send(:job)[:runtime])
27
+ assert(client.send(:job)[:error])
28
+ end
29
+
30
+ private
31
+
32
+ def client
33
+ if !@client
34
+ RorVsWild::Client.any_instance.stubs(:setup_callbacks)
35
+ @client ||= RorVsWild::Client.new({})
36
+ @client.stubs(:post_request)
37
+ @client.stubs(:post_task)
38
+ end
39
+ @client
40
+ end
41
+ end
42
+
43
+ # Simulate Rails.root
44
+
45
+ require "pathname"
46
+
47
+ module Rails
48
+ def self.root
49
+ Pathname.new("foo")
50
+ end
51
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rorvswild
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Bernard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-15 00:00:00.000000000 Z
11
+ date: 2015-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,6 +39,7 @@ files:
39
39
  - lib/rorvswild.rb
40
40
  - lib/rorvswild/version.rb
41
41
  - rorvswild.gemspec
42
+ - test/ror_vs_wild_test.rb
42
43
  homepage: http://www.rorvswild.com
43
44
  licenses:
44
45
  - MIT
@@ -64,4 +65,5 @@ signing_key:
64
65
  specification_version: 4
65
66
  summary: Simple Ruby on Rails application monitoring for hardcore developers with
66
67
  no time to waste.
67
- test_files: []
68
+ test_files:
69
+ - test/ror_vs_wild_test.rb