traxor 0.1.17 → 0.1.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,252 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ traxor (0.1.18)
5
+ activesupport (>= 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.2.0)
11
+ actionpack (= 5.2.0)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailer (5.2.0)
15
+ actionpack (= 5.2.0)
16
+ actionview (= 5.2.0)
17
+ activejob (= 5.2.0)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.2.0)
21
+ actionview (= 5.2.0)
22
+ activesupport (= 5.2.0)
23
+ rack (~> 2.0)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.2.0)
28
+ activesupport (= 5.2.0)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.2.0)
34
+ activesupport (= 5.2.0)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.2.0)
37
+ activesupport (= 5.2.0)
38
+ activerecord (5.2.0)
39
+ activemodel (= 5.2.0)
40
+ activesupport (= 5.2.0)
41
+ arel (>= 9.0)
42
+ activestorage (5.2.0)
43
+ actionpack (= 5.2.0)
44
+ activerecord (= 5.2.0)
45
+ marcel (~> 0.3.1)
46
+ activesupport (5.2.0)
47
+ concurrent-ruby (~> 1.0, >= 1.0.2)
48
+ i18n (>= 0.7, < 2)
49
+ minitest (~> 5.1)
50
+ tzinfo (~> 1.1)
51
+ appraisal (2.2.0)
52
+ bundler
53
+ rake
54
+ thor (>= 0.14.0)
55
+ arel (9.0.0)
56
+ ast (2.4.0)
57
+ axiom-types (0.1.1)
58
+ descendants_tracker (~> 0.0.4)
59
+ ice_nine (~> 0.11.0)
60
+ thread_safe (~> 0.3, >= 0.3.1)
61
+ builder (3.2.3)
62
+ byebug (10.0.2)
63
+ codeclimate-engine-rb (0.4.1)
64
+ virtus (~> 1.0)
65
+ coderay (1.1.2)
66
+ coercible (1.0.0)
67
+ descendants_tracker (~> 0.0.1)
68
+ concurrent-ruby (1.0.5)
69
+ connection_pool (2.2.1)
70
+ crass (1.0.4)
71
+ descendants_tracker (0.0.4)
72
+ thread_safe (~> 0.3, >= 0.3.1)
73
+ diff-lcs (1.3)
74
+ docile (1.3.0)
75
+ equalizer (0.0.11)
76
+ erubi (1.7.1)
77
+ faraday (0.15.0)
78
+ multipart-post (>= 1.2, < 3)
79
+ ffi (1.9.23)
80
+ formatador (0.2.5)
81
+ globalid (0.4.1)
82
+ activesupport (>= 4.2.0)
83
+ guard (2.14.2)
84
+ formatador (>= 0.2.4)
85
+ listen (>= 2.7, < 4.0)
86
+ lumberjack (>= 1.0.12, < 2.0)
87
+ nenv (~> 0.1)
88
+ notiffany (~> 0.0)
89
+ pry (>= 0.9.12)
90
+ shellany (~> 0.0)
91
+ thor (>= 0.18.1)
92
+ guard-compat (1.2.1)
93
+ guard-rspec (4.7.3)
94
+ guard (~> 2.1)
95
+ guard-compat (~> 1.1)
96
+ rspec (>= 2.99.0, < 4.0)
97
+ i18n (1.0.1)
98
+ concurrent-ruby (~> 1.0)
99
+ ice_nine (0.11.2)
100
+ json (2.1.0)
101
+ listen (3.1.5)
102
+ rb-fsevent (~> 0.9, >= 0.9.4)
103
+ rb-inotify (~> 0.9, >= 0.9.7)
104
+ ruby_dep (~> 1.2)
105
+ loofah (2.2.2)
106
+ crass (~> 1.0.2)
107
+ nokogiri (>= 1.5.9)
108
+ lumberjack (1.0.13)
109
+ mail (2.7.0)
110
+ mini_mime (>= 0.1.1)
111
+ marcel (0.3.2)
112
+ mimemagic (~> 0.3.2)
113
+ method_source (0.9.0)
114
+ mimemagic (0.3.2)
115
+ mini_mime (1.0.0)
116
+ mini_portile2 (2.3.0)
117
+ minitest (5.11.3)
118
+ multipart-post (2.0.0)
119
+ nenv (0.3.0)
120
+ nio4r (2.3.1)
121
+ nokogiri (1.8.2)
122
+ mini_portile2 (~> 2.3.0)
123
+ notiffany (0.1.1)
124
+ nenv (~> 0.1)
125
+ shellany (~> 0.0)
126
+ parallel (1.12.1)
127
+ parser (2.5.1.0)
128
+ ast (~> 2.4.0)
129
+ powerpack (0.1.1)
130
+ pry (0.11.3)
131
+ coderay (~> 1.1.0)
132
+ method_source (~> 0.9.0)
133
+ pry-byebug (3.6.0)
134
+ byebug (~> 10.0)
135
+ pry (~> 0.10)
136
+ rack (2.0.5)
137
+ rack-protection (2.0.1)
138
+ rack
139
+ rack-test (1.0.0)
140
+ rack (>= 1.0, < 3)
141
+ rails (5.2.0)
142
+ actioncable (= 5.2.0)
143
+ actionmailer (= 5.2.0)
144
+ actionpack (= 5.2.0)
145
+ actionview (= 5.2.0)
146
+ activejob (= 5.2.0)
147
+ activemodel (= 5.2.0)
148
+ activerecord (= 5.2.0)
149
+ activestorage (= 5.2.0)
150
+ activesupport (= 5.2.0)
151
+ bundler (>= 1.3.0)
152
+ railties (= 5.2.0)
153
+ sprockets-rails (>= 2.0.0)
154
+ rails-dom-testing (2.0.3)
155
+ activesupport (>= 4.2.0)
156
+ nokogiri (>= 1.6)
157
+ rails-html-sanitizer (1.0.4)
158
+ loofah (~> 2.2, >= 2.2.2)
159
+ railties (5.2.0)
160
+ actionpack (= 5.2.0)
161
+ activesupport (= 5.2.0)
162
+ method_source
163
+ rake (>= 0.8.7)
164
+ thor (>= 0.18.1, < 2.0)
165
+ rainbow (3.0.0)
166
+ rake (12.3.1)
167
+ rb-fsevent (0.10.3)
168
+ rb-inotify (0.9.10)
169
+ ffi (>= 0.5.0, < 2)
170
+ redis (4.0.1)
171
+ reek (4.8.1)
172
+ codeclimate-engine-rb (~> 0.4.0)
173
+ parser (>= 2.5.0.0, < 2.6)
174
+ rainbow (>= 2.0, < 4.0)
175
+ rspec (3.7.0)
176
+ rspec-core (~> 3.7.0)
177
+ rspec-expectations (~> 3.7.0)
178
+ rspec-mocks (~> 3.7.0)
179
+ rspec-core (3.7.1)
180
+ rspec-support (~> 3.7.0)
181
+ rspec-expectations (3.7.0)
182
+ diff-lcs (>= 1.2.0, < 2.0)
183
+ rspec-support (~> 3.7.0)
184
+ rspec-mocks (3.7.0)
185
+ diff-lcs (>= 1.2.0, < 2.0)
186
+ rspec-support (~> 3.7.0)
187
+ rspec-support (3.7.1)
188
+ rspec_junit_formatter (0.3.0)
189
+ rspec-core (>= 2, < 4, != 2.12.0)
190
+ rubocop (0.55.0)
191
+ parallel (~> 1.10)
192
+ parser (>= 2.5)
193
+ powerpack (~> 0.1)
194
+ rainbow (>= 2.2.2, < 4.0)
195
+ ruby-progressbar (~> 1.7)
196
+ unicode-display_width (~> 1.0, >= 1.0.1)
197
+ ruby-progressbar (1.9.0)
198
+ ruby_dep (1.5.0)
199
+ shellany (0.0.1)
200
+ sidekiq (5.1.3)
201
+ concurrent-ruby (~> 1.0)
202
+ connection_pool (~> 2.2, >= 2.2.0)
203
+ rack-protection (>= 1.5.0)
204
+ redis (>= 3.3.5, < 5)
205
+ simplecov (0.16.1)
206
+ docile (~> 1.1)
207
+ json (>= 1.8, < 3)
208
+ simplecov-html (~> 0.10.0)
209
+ simplecov-html (0.10.2)
210
+ sprockets (3.7.1)
211
+ concurrent-ruby (~> 1.0)
212
+ rack (> 1, < 3)
213
+ sprockets-rails (3.2.1)
214
+ actionpack (>= 4.0)
215
+ activesupport (>= 4.0)
216
+ sprockets (>= 3.0.0)
217
+ thor (0.20.0)
218
+ thread_safe (0.3.6)
219
+ tzinfo (1.2.5)
220
+ thread_safe (~> 0.1)
221
+ unicode-display_width (1.3.2)
222
+ virtus (1.0.5)
223
+ axiom-types (~> 0.1)
224
+ coercible (~> 1.0)
225
+ descendants_tracker (~> 0.0, >= 0.0.3)
226
+ equalizer (~> 0.0, >= 0.0.9)
227
+ websocket-driver (0.7.0)
228
+ websocket-extensions (>= 0.1.0)
229
+ websocket-extensions (0.1.3)
230
+
231
+ PLATFORMS
232
+ ruby
233
+
234
+ DEPENDENCIES
235
+ activesupport (~> 5.2)
236
+ appraisal
237
+ bundler
238
+ faraday
239
+ guard-rspec
240
+ pry-byebug
241
+ rails
242
+ rake
243
+ reek
244
+ rspec
245
+ rspec_junit_formatter
246
+ rubocop
247
+ sidekiq
248
+ simplecov
249
+ traxor!
250
+
251
+ BUNDLED WITH
252
+ 1.16.1
@@ -1,11 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/notifications'
4
+
1
5
  module Traxor
2
- ActiveSupport::Notifications.subscribe('request.faraday'.freeze) do |*args|
3
- event = ActiveSupport::Notifications::Event.new(*args)
4
- url = event.payload[:url]
5
- duration = (event.duration || 0.0).to_f
6
- tags = { faraday_host: url.host, faraday_method: event.payload[:method] }
7
-
8
- Metric.count 'faraday.request.count'.freeze, 1, tags
9
- Metric.measure 'faraday.request.duration'.freeze, "#{duration.round(2)}ms", tags if duration.positive?
6
+ module Faraday
7
+ DURATION_METRIC = 'faraday.request.duration'
8
+ COUNT_METRIC = 'faraday.request.count'
9
+
10
+ def self.record(event)
11
+ url = event.payload[:url]
12
+ duration = (event.duration || 0.0).to_f
13
+ tags = { faraday_host: url.host, faraday_method: event.payload[:method] }
14
+
15
+ Metric.count COUNT_METRIC, 1, tags
16
+ Metric.measure DURATION_METRIC, "#{duration.round(2)}ms", tags if duration.positive?
17
+ end
10
18
  end
11
19
  end
20
+
21
+ ActiveSupport::Notifications.subscribe('request.faraday') do |*args|
22
+ event = ActiveSupport::Notifications::Event.new(*args)
23
+ Traxor::Faraday.record(event)
24
+ end
data/lib/traxor/metric.rb CHANGED
@@ -1,4 +1,6 @@
1
- require 'active_support/inflector/inflections'
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
2
4
 
3
5
  module Traxor
4
6
  module Metric
@@ -15,17 +17,21 @@ module Traxor
15
17
  end
16
18
 
17
19
  def self.tag_string(tags)
18
- Traxor::Tags.all.merge(tags).map do |tag_name, tag_value|
20
+ Hash(tags).merge(Traxor::Tags.all).map do |tag_name, tag_value|
19
21
  "tag##{tag_name}=#{tag_value}"
20
- end.join(' '.freeze)
22
+ end.join(' ')
21
23
  end
22
24
 
23
25
  def self.normalize_values(value)
24
- value.to_s.gsub(/::/, '.'.freeze).underscore.strip
26
+ value.to_s.gsub(/::/, '.').underscore.strip
25
27
  end
26
28
 
27
29
  def self.log(string)
28
- Traxor.logger.info(normalize_values(string))
30
+ logger.info(normalize_values(string))
31
+ end
32
+
33
+ def self.logger
34
+ Traxor.logger
29
35
  end
30
36
  end
31
37
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Traxor
2
4
  module Rack
3
5
  module Middleware
@@ -1,9 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'traxor/rack/middleware/queue_time'
2
4
 
3
5
  module Traxor
4
6
  module Rack
5
7
  module Middleware
6
8
  class Pre
9
+ MIDDLEWARE_METRIC = 'rack.request.middleware.duration'
10
+ DURATION_METRIC = 'rack.request.duration'
11
+ QUEUE_METRIC = 'rack.request.queue.duration'
12
+ REQUEST_COUNT_METRIC = 'rack.request.count'
13
+ GC_DURATION_METRIC = 'ruby.gc.duration'
14
+ GC_COUNT_METRIC = 'ruby.gc.count'
15
+ MAJOR_METRIC = 'ruby.gc.major.count'
16
+ MINOR_METRIC = 'ruby.gc.minor.count'
17
+ ALLOCATED_METRIC = 'ruby.gc.allocated_objects.count'
18
+
7
19
  def initialize(app)
8
20
  @app = app
9
21
  end
@@ -18,22 +30,34 @@ module Traxor
18
30
  Middleware.gc_stat_after = GC.stat
19
31
  Middleware.post_finish_at = Time.now.utc
20
32
 
21
- record_metrics
33
+ record_request_metrics
34
+ record_gc_metrics
22
35
 
23
36
  [status, headers, body]
24
37
  end
25
38
 
26
- def record_metrics
27
- Metric.measure 'rack.request.middleware.duration'.freeze, "#{Middleware.middleware_total.round(2)}ms" if Middleware.middleware_total.positive?
28
- Metric.measure 'rack.request.duration'.freeze, "#{Middleware.request_total.round(2)}ms" if Middleware.request_total.positive?
29
- Metric.measure 'rack.request.queue.duration'.freeze, "#{Middleware.request_queue_total.round(2)}ms" if Middleware.request_queue_total.positive?
30
- Metric.count 'rack.request.count'.freeze, 1
31
-
32
- Metric.measure 'ruby.gc.duration'.freeze, "#{(GC::Profiler.total_time * 1_000).to_f.round(2)}ms"
33
- Metric.count 'ruby.gc.count'.freeze, Middleware.gc_count
34
- Metric.count 'ruby.gc.major.count'.freeze, Middleware.gc_major_count
35
- Metric.count 'ruby.gc.minor.count'.freeze, Middleware.gc_minor_count
36
- Metric.count 'ruby.gc.allocated_objects.count'.freeze, Middleware.gc_allocated_objects_count
39
+ def record_request_metrics
40
+ if Middleware.middleware_total.positive?
41
+ Metric.measure MIDDLEWARE_METRIC, "#{Middleware.middleware_total.round(2)}ms"
42
+ end
43
+ if Middleware.request_total.positive?
44
+ Metric.measure DURATION_METRIC, "#{Middleware.request_total.round(2)}ms"
45
+ end
46
+ if Middleware.request_queue_total.positive?
47
+ Metric.measure QUEUE_METRIC, "#{Middleware.request_queue_total.round(2)}ms"
48
+ end
49
+ Metric.count REQUEST_COUNT_METRIC, 1
50
+ end
51
+
52
+ def record_gc_metrics
53
+ total_gc_time = (GC::Profiler.total_time * 1_000).to_f
54
+ if total_gc_time.positive?
55
+ Metric.measure GC_DURATION_METRIC, "#{total_gc_time.round(2)}ms"
56
+ end
57
+ Metric.count GC_COUNT_METRIC, Middleware.gc_count
58
+ Metric.count MAJOR_METRIC, Middleware.gc_major_count
59
+ Metric.count MINOR_METRIC, Middleware.gc_minor_count
60
+ Metric.count ALLOCATED_METRIC, Middleware.gc_allocated_objects_count
37
61
 
38
62
  GC::Profiler.clear
39
63
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Traxor
2
4
  module Rack
3
5
  module Middleware
4
6
  module QueueTime
5
- X_REQUEST_START = 'HTTP_X_REQUEST_START'.freeze
7
+ X_REQUEST_START = 'HTTP_X_REQUEST_START'
6
8
 
7
9
  # any timestamps before this are thrown out and the parser
8
10
  # will try again with a larger unit (2000/1/1 UTC)
@@ -13,16 +15,10 @@ module Traxor
13
15
  def self.parse(env)
14
16
  return unless env[X_REQUEST_START]
15
17
 
16
- value = env[X_REQUEST_START].to_s.sub(/t=/, ''.freeze)
18
+ value = env[X_REQUEST_START].to_s.sub(/t=/, '')
17
19
  DIVISORS.each do |divisor|
18
- begin
19
- time = Time.at(value.to_f / divisor).utc
20
- return time if time > EARLIEST_ACCEPTABLE_TIME
21
- rescue RangeError
22
- # On Ruby versions built with a 32-bit time_t, attempting to
23
- # instantiate a Time object in the far future raises a RangeError,
24
- # in which case we know we've chosen the wrong divisor.
25
- end
20
+ time = Time.at(value.to_f / divisor).utc
21
+ return time if time > EARLIEST_ACCEPTABLE_TIME
26
22
  end
27
23
 
28
24
  nil
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/module/attribute_accessors_per_thread'
1
4
  require 'traxor/rack/middleware/pre'
2
5
  require 'traxor/rack/middleware/post'
3
6
 
data/lib/traxor/rack.rb CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'traxor/rack/middleware'
@@ -1,26 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/notifications'
4
+
1
5
  module Traxor
2
- ActiveSupport::Notifications.subscribe 'start_processing.action_controller'.freeze do |*args|
3
- event = ActiveSupport::Notifications::Event.new(*args)
4
- Traxor::Tags.controller = {
5
- controller_name: event.payload[:controller],
6
- controller_action: event.payload[:action],
7
- controller_method: event.payload[:method]
8
- }
9
- end
6
+ module Rails
7
+ module ActionController
8
+ COUNT_METRIC = 'rails.action_controller.count'
9
+ TOTAL_METRIC = 'rails.action_controller.total.duration'
10
+ RUBY_METRIC = 'rails.action_controller.ruby.duration'
11
+ DB_METRIC = 'rails.action_controller.db.duration'
12
+ VIEW_METRIC = 'rails.action_controller.view.duration'
13
+ EXCEPTION_METRIC = 'rails.action_controller.exception.count'
14
+
15
+ def self.set_controller_tags(event)
16
+ Traxor::Tags.controller = {
17
+ controller_name: event.payload[:controller],
18
+ controller_action: event.payload[:action],
19
+ controller_method: event.payload[:method]
20
+ }
21
+ end
10
22
 
11
- ActiveSupport::Notifications.subscribe 'process_action.action_controller'.freeze do |*args|
12
- event = ActiveSupport::Notifications::Event.new(*args)
13
- exception = event.payload[:exception]
14
- duration = (event.duration || 0.0).to_f
15
- db_runtime = (event.payload[:db_runtime] || 0.0).to_f
16
- view_runtime = (event.payload[:view_runtime] || 0.0).to_f
17
- ruby_runtime = duration - db_runtime - view_runtime
23
+ def self.record(event)
24
+ exception = event.payload[:exception]
25
+ duration = (event.duration || 0.0).to_f
26
+ db_runtime = (event.payload[:db_runtime] || 0.0).to_f
27
+ view_runtime = (event.payload[:view_runtime] || 0.0).to_f
28
+ ruby_runtime = duration - db_runtime - view_runtime
18
29
 
19
- Metric.count 'rails.action_controller.count'.freeze, 1
20
- Metric.measure 'rails.action_controller.total.duration'.freeze, "#{duration.round(2)}ms" if duration.positive?
21
- Metric.measure 'rails.action_controller.ruby.duration'.freeze, "#{ruby_runtime.round(2)}ms" if ruby_runtime.positive?
22
- Metric.measure 'rails.action_controller.db.duration'.freeze, "#{db_runtime.round(2)}ms" if db_runtime.positive?
23
- Metric.measure 'rails.action_controller.view.duration'.freeze, "#{view_runtime.round(2)}ms" if view_runtime.positive?
24
- Metric.count 'rails.action_controller.exception.count'.freeze, 1 if exception
30
+ Metric.count COUNT_METRIC, 1
31
+ Metric.measure TOTAL_METRIC, "#{duration.round(2)}ms" if duration.positive?
32
+ Metric.measure RUBY_METRIC, "#{ruby_runtime.round(2)}ms" if ruby_runtime.positive?
33
+ Metric.measure DB_METRIC, "#{db_runtime.round(2)}ms" if db_runtime.positive?
34
+ Metric.measure VIEW_METRIC, "#{view_runtime.round(2)}ms" if view_runtime.positive?
35
+ Metric.count EXCEPTION_METRIC, 1 if exception
36
+ end
37
+ end
25
38
  end
26
39
  end
40
+
41
+ ActiveSupport::Notifications.subscribe 'start_processing.action_controller' do |*args|
42
+ event = ActiveSupport::Notifications::Event.new(*args)
43
+ Traxor::Rails::ActionController.set_controller_tags(event)
44
+ end
45
+
46
+ ActiveSupport::Notifications.subscribe 'process_action.action_controller' do |*args|
47
+ event = ActiveSupport::Notifications::Event.new(*args)
48
+ Traxor::Rails::ActionController.record(event)
49
+ end
@@ -1,8 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/notifications'
4
+
1
5
  module Traxor
2
- ActiveSupport::Notifications.subscribe 'deliver.action_mailer'.freeze do |*args|
3
- event = ActiveSupport::Notifications::Event.new(*args)
4
- tags = { action_mailer_class_name: event.payload[:mailer] }
6
+ module Rails
7
+ module ActionMailer
8
+ COUNT_METRIC = 'rails.action_mailer.sent.count'
5
9
 
6
- Metric.count 'rails.action_mailer.sent.count'.freeze, 1, tags
10
+ def self.record(event)
11
+ tags = { action_mailer_class_name: event.payload[:mailer] }
12
+ Metric.count COUNT_METRIC, 1, tags
13
+ end
14
+ end
7
15
  end
8
16
  end
17
+
18
+ ActiveSupport::Notifications.subscribe 'deliver.action_mailer' do |*args|
19
+ event = ActiveSupport::Notifications::Event.new(*args)
20
+ Traxor::Rails::ActionMailer.record(event)
21
+ end
@@ -1,24 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/notifications'
4
+
1
5
  module Traxor
2
- ActiveSupport::Notifications.subscribe 'sql.active_record'.freeze do |*args|
3
- event = ActiveSupport::Notifications::Event.new(*args)
4
- sql = event.payload[:sql].to_s.strip.upcase
5
- name = event.payload[:name].to_s.strip.upcase
6
- next if ['SCHEMA'.freeze].any?(name)
7
- tags = {}
8
- tags[:active_record_class_name] = name.split.first if name.length.positive?
9
-
10
- Metric.count 'rails.active_record.statements.count'.freeze, 1, tags
11
- Metric.count 'rails.active_record.statements.select.count'.freeze, 1, tags if sql.starts_with?('SELECT'.freeze)
12
- Metric.count 'rails.active_record.statements.insert.count'.freeze, 1, tags if sql.starts_with?('INSERT'.freeze)
13
- Metric.count 'rails.active_record.statements.update.count'.freeze, 1, tags if sql.starts_with?('UPDATE'.freeze)
14
- Metric.count 'rails.active_record.statements.delete.count'.freeze, 1, tags if sql.starts_with?('DELETE'.freeze)
15
- end
6
+ module Rails
7
+ module ActiveRecord
8
+ COUNT_METRIC = 'rails.active_record.statements.count'
9
+ SELECT_METRIC = 'rails.active_record.statements.select.count'
10
+ INSERT_METRIC = 'rails.active_record.statements.insert.count'
11
+ UPDATE_METRIC = 'rails.active_record.statements.update.count'
12
+ DELETE_METRIC = 'rails.active_record.statements.delete.count'
13
+ INSTANTIATION_METRIC = 'rails.active_record.instantiation.count'
14
+
15
+ def self.record(event)
16
+ sql = event.payload[:sql].to_s.strip.upcase
17
+ name = event.payload[:name].to_s.strip
18
+ return if name.casecmp('SCHEMA').zero?
19
+ tags = {}
20
+ tags[:active_record_class_name] = name.split.first if name.length.positive?
21
+
22
+ Metric.count COUNT_METRIC, 1, tags
23
+ Metric.count SELECT_METRIC, 1, tags if sql.start_with?('SELECT')
24
+ Metric.count INSERT_METRIC, 1, tags if sql.start_with?('INSERT')
25
+ Metric.count UPDATE_METRIC, 1, tags if sql.start_with?('UPDATE')
26
+ Metric.count DELETE_METRIC, 1, tags if sql.start_with?('DELETE')
27
+ end
16
28
 
17
- ActiveSupport::Notifications.subscribe 'instantiation.active_record'.freeze do |*args|
18
- event = ActiveSupport::Notifications::Event.new(*args)
19
- record_count = event.payload[:record_count].to_i
20
- tags = { active_record_class_name: event.payload[:class_name] }
29
+ def self.record_instantiations(event)
30
+ record_count = event.payload[:record_count].to_i
31
+ tags = { active_record_class_name: event.payload[:class_name] }
21
32
 
22
- Metric.count 'rails.active_record.instantiation.count'.freeze, record_count, tags if record_count.positive?
33
+ Metric.count INSTANTIATION_METRIC, record_count, tags if record_count.positive?
34
+ end
35
+ end
23
36
  end
24
37
  end
38
+
39
+ ActiveSupport::Notifications.subscribe 'sql.active_record' do |*args|
40
+ event = ActiveSupport::Notifications::Event.new(*args)
41
+ Traxor::Rails::ActiveRecord.record(event)
42
+ end
43
+
44
+ ActiveSupport::Notifications.subscribe 'instantiation.active_record' do |*args|
45
+ event = ActiveSupport::Notifications::Event.new(*args)
46
+ Traxor::Rails::ActiveRecord.record_instantiations(event)
47
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/engine'
4
+
5
+ module Traxor
6
+ module Rails
7
+ class Engine < ::Rails::Engine
8
+ initializer 'traxor.setup' do |app|
9
+ if ::Rails.env.development? || ::Rails.env.test?
10
+ Traxor.initialize_logger(::Rails.root.join('log', 'traxor.log'))
11
+ end
12
+
13
+ require 'traxor/rack'
14
+ app.config.middleware.insert 0, Traxor::Rack::Middleware::Pre
15
+ app.config.middleware.use Traxor::Rack::Middleware::Post
16
+
17
+ ActiveSupport.on_load :action_controller do
18
+ require 'traxor/rails/action_controller'
19
+ end
20
+ ActiveSupport.on_load :active_record do
21
+ require 'traxor/rails/active_record'
22
+ end
23
+ ActiveSupport.on_load :action_mailer do
24
+ require 'traxor/rails/action_mailer'
25
+ end
26
+ end
27
+
28
+ config.before_configuration do
29
+ require 'traxor/faraday' if defined?(Faraday)
30
+
31
+ if defined?(Sidekiq)
32
+ require 'traxor/sidekiq'
33
+ ::Sidekiq.server_middleware do |chain|
34
+ chain.add Traxor::Sidekiq
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end