traxor 0.1.13 → 0.1.14

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
  SHA256:
3
- metadata.gz: 7dea12e48a7280a76156a4caa29b2c0588a5e81ff0e136b385bba2254a964411
4
- data.tar.gz: 62bf229220daa96172fc80e4899d6f042e57d1876cebb656964db3e2decad7bf
3
+ metadata.gz: f7e644afc68fb318da43bec6e34a9a4f6662fcf7cacf116a94d39e10ba172452
4
+ data.tar.gz: 16db21962c34d7d84db4b7aff652be9272e095cb3e205d31fdea8f8a0d7b793d
5
5
  SHA512:
6
- metadata.gz: 78d0d2b367d8ee4863b4561015a0944ffcb19e8f1ff400e20270c9e045eb6b49b33d708fd6bfcfbeb0370477c8a68440bcf4053b0af92a2534c7ca3e0e008a14
7
- data.tar.gz: 74aebfdd96c5d6012d8168f223b925a7a1e6e751f41c8866b9954bea4ccf0393f478701421773304df413ac29f2c0e635ed39f843ec942bd72076664ce992100
6
+ metadata.gz: b54e4ccbe47465d45e4e4d31cff7485cdfca76f520bc5e8922f19fd936ffc4b874d006746cd45c6d0d4100a0e467f91a80a6e7fb6578a6affe2bf13887cb15f8
7
+ data.tar.gz: d22e7c0713f62730e30ad4afbe575a72a9dca698370a458673744395875fec3dd0c9471b27f24110b940a7d36daca96e8a820bd357dd36553de617c366023438
data/.circleci/config.yml CHANGED
@@ -26,6 +26,18 @@ jobs:
26
26
  - ./vendor/bundle
27
27
  key: v1-dependencies-{{ checksum "Gemfile.lock" }}
28
28
 
29
+ - run:
30
+ name: Bundler-Audit
31
+ command: bin/lois bundler-audit -g $GITHUB_CREDENTIALS
32
+
33
+ - run:
34
+ name: Rubocop
35
+ command: bin/lois rubocop -g $GITHUB_CREDENTIALS
36
+
37
+ - run:
38
+ name: Reek
39
+ command: bin/lois reek -g $GITHUB_CREDENTIALS
40
+
29
41
  - run:
30
42
  name: Setup Code Climate test-reporter
31
43
  command: |
@@ -44,5 +56,7 @@ jobs:
44
56
  path: results
45
57
  - store_artifacts:
46
58
  path: results
59
+ - store_artifacts:
60
+ path: lois
47
61
  - store_artifacts:
48
62
  path: coverage
data/.reek ADDED
@@ -0,0 +1,9 @@
1
+ ---
2
+ exclude_paths:
3
+ - vendor/bundle
4
+
5
+ DuplicateMethodCall:
6
+ enabled: false
7
+
8
+ IrresponsibleModule:
9
+ enabled: false
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ DisplayCopNames: true
5
+
6
+ Rails:
7
+ Enabled: true
8
+
9
+ Style/Documentation:
10
+ Enabled: false
11
+
12
+ Metrics/LineLength:
13
+ Max: 100
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- traxor (0.1.13)
4
+ traxor (0.1.14)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -54,7 +54,11 @@ GEM
54
54
  descendants_tracker (~> 0.0.4)
55
55
  ice_nine (~> 0.11.0)
56
56
  thread_safe (~> 0.3, >= 0.3.1)
57
+ brakeman (4.2.1)
57
58
  builder (3.2.3)
59
+ bundler-audit (0.6.0)
60
+ bundler (~> 1.2)
61
+ thor (~> 0.18)
58
62
  byebug (10.0.2)
59
63
  codeclimate-engine-rb (0.4.1)
60
64
  virtus (~> 1.0)
@@ -74,10 +78,21 @@ GEM
74
78
  multipart-post (>= 1.2, < 3)
75
79
  globalid (0.4.1)
76
80
  activesupport (>= 4.2.0)
81
+ httparty (0.16.2)
82
+ multi_xml (>= 0.5.2)
77
83
  i18n (1.0.1)
78
84
  concurrent-ruby (~> 1.0)
79
85
  ice_nine (0.11.2)
80
86
  json (2.1.0)
87
+ lois (0.1.6)
88
+ activesupport
89
+ brakeman
90
+ bundler-audit
91
+ httparty
92
+ reek
93
+ rubocop
94
+ simplecov
95
+ thor
81
96
  loofah (2.2.2)
82
97
  crass (~> 1.0.2)
83
98
  nokogiri (>= 1.5.9)
@@ -90,6 +105,7 @@ GEM
90
105
  mini_mime (1.0.0)
91
106
  mini_portile2 (2.3.0)
92
107
  minitest (5.11.3)
108
+ multi_xml (0.6.0)
93
109
  multipart-post (2.0.0)
94
110
  nio4r (2.3.0)
95
111
  nokogiri (1.8.2)
@@ -104,7 +120,7 @@ GEM
104
120
  pry-byebug (3.6.0)
105
121
  byebug (~> 10.0)
106
122
  pry (~> 0.10)
107
- rack (2.0.4)
123
+ rack (2.0.5)
108
124
  rack-protection (2.0.1)
109
125
  rack
110
126
  rack-test (1.0.0)
@@ -162,6 +178,8 @@ GEM
162
178
  rainbow (>= 2.2.2, < 4.0)
163
179
  ruby-progressbar (~> 1.7)
164
180
  unicode-display_width (~> 1.0, >= 1.0.1)
181
+ rubocop-rspec (1.25.1)
182
+ rubocop (>= 0.53.0)
165
183
  ruby-progressbar (1.9.0)
166
184
  sidekiq (5.1.3)
167
185
  concurrent-ruby (~> 1.0)
@@ -184,7 +202,7 @@ GEM
184
202
  thread_safe (0.3.6)
185
203
  tzinfo (1.2.5)
186
204
  thread_safe (~> 0.1)
187
- unicode-display_width (1.3.1)
205
+ unicode-display_width (1.3.2)
188
206
  virtus (1.0.5)
189
207
  axiom-types (~> 0.1)
190
208
  coercible (~> 1.0)
@@ -200,6 +218,7 @@ PLATFORMS
200
218
  DEPENDENCIES
201
219
  bundler
202
220
  faraday
221
+ lois
203
222
  pry
204
223
  pry-byebug
205
224
  rails
@@ -208,6 +227,7 @@ DEPENDENCIES
208
227
  rspec
209
228
  rspec_junit_formatter
210
229
  rubocop
230
+ rubocop-rspec
211
231
  sidekiq
212
232
  simplecov
213
233
  traxor!
data/bin/lois ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'lois' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
+
29
+ load Gem.bin_path('lois', 'lois')
@@ -1,13 +1,11 @@
1
1
  module Traxor
2
- ActiveSupport::Notifications.subscribe('request.faraday') do |*args|
2
+ ActiveSupport::Notifications.subscribe('request.faraday'.freeze) do |*args|
3
3
  event = ActiveSupport::Notifications::Event.new(*args)
4
4
  url = event.payload[:url]
5
5
  duration = event.duration || 0.0
6
- tags = { host: normalize_name(url.host), method: normalize_name(event.payload[:method]) }
6
+ tags = { faraday_host: url.host, faraday_method: event.payload[:method] }
7
7
 
8
- Metric.count 'faraday.request.count', 1, tags
9
- Metric.count "faraday.request.count.#{tags[:host]}", 1, tags
10
- Metric.measure 'farday.request.duration', "#{duration.to_f.round(2)}ms", tags
11
- Metric.measure "farday.request.duration.#{tags[:host]}", "#{duration.to_f.round(2)}ms", tags
8
+ Metric.count 'faraday.request.count'.freeze, 1, tags
9
+ Metric.measure 'faraday.request.duration'.freeze, "#{duration.to_f.round(2)}ms", tags
12
10
  end
13
11
  end
data/lib/traxor/metric.rb CHANGED
@@ -1,25 +1,31 @@
1
+ require 'active_support/inflector/inflections'
2
+
1
3
  module Traxor
2
4
  module Metric
3
5
  def self.count(name, value, tags = {})
4
- logger.info("count##{name}=#{value} #{tag_string(tags)}".strip)
6
+ log("count##{name}=#{value} #{tag_string(tags)}")
5
7
  end
6
8
 
7
9
  def self.measure(name, value, tags = {})
8
- logger.info("measure##{name}=#{value} #{tag_string(tags)}".strip)
10
+ log("measure##{name}=#{value} #{tag_string(tags)}")
9
11
  end
10
12
 
11
13
  def self.sample(name, value, tags = {})
12
- logger.info("sample##{name}=#{value} #{tag_string(tags)}".strip)
14
+ log("sample##{name}=#{value} #{tag_string(tags)}")
13
15
  end
14
16
 
15
17
  def self.tag_string(tags)
16
- tags.map do |tag_name, tag_value|
18
+ Traxor::Tags.all.merge(tags).map do |tag_name, tag_value|
17
19
  "tag##{tag_name}=#{tag_value}"
18
- end.join(' ')
20
+ end.join(' '.freeze)
21
+ end
22
+
23
+ def self.normalize_values(value)
24
+ value.to_s.gsub(/::/, '.'.freeze).underscore.strip
19
25
  end
20
26
 
21
- def self.logger
22
- @logger ||= Traxor.config.logger
27
+ def self.log(string)
28
+ Traxor.logger.info(normalize_values(string))
23
29
  end
24
30
  end
25
31
  end
@@ -7,9 +7,9 @@ module Traxor
7
7
  end
8
8
 
9
9
  def call(env)
10
- env['traxor.rack.middleware.pre_middleware_end'] = Time.now.to_f
10
+ Middleware.pre_finish_at = Time.now.utc
11
11
  status, headers, response = @app.call(env)
12
- env['traxor.rack.middleware.post_middleware_start'] = Time.now.to_f
12
+ Middleware.post_start_at = Time.now.utc
13
13
 
14
14
  [status, headers, response]
15
15
  end
@@ -1,79 +1,30 @@
1
+ require 'traxor/rack/middleware/queue_time'
2
+
1
3
  module Traxor
2
4
  module Rack
3
5
  module Middleware
4
6
  class Pre
5
- # any timestamps before this are thrown out and the parser
6
- # will try again with a larger unit (2000/1/1 UTC)
7
- EARLIEST_ACCEPTABLE_TIME = Time.at(946684800)
8
-
9
- DIVISORS = [1_000_000, 1_000, 1]
10
-
11
7
  def initialize(app)
12
8
  @app = app
13
9
  end
14
10
 
15
11
  def call(env)
16
- env['traxor.rack.middleware.pre_middleware_start'] = Time.now.to_f
17
- queue_duration = nil
18
- request_start = env['HTTP_X_REQUEST_START']
19
- if request_start
20
- parsed = parse_request_queue(request_start)
21
- queue_duration = (env['traxor.rack.middleware.pre_middleware_start'].to_f - parsed.to_f) * 1_000
22
- end
23
- status, headers, body = @app.call(env)
24
- env['traxor.rack.middleware.post_middleware_end'] = Time.now.to_f
25
-
26
- controller = env['action_controller.instance']
27
- times = [
28
- 'traxor.rack.middleware.pre_middleware_start',
29
- 'traxor.rack.middleware.pre_middleware_end',
30
- 'traxor.rack.middleware.post_middleware_start',
31
- 'traxor.rack.middleware.post_middleware_end'
32
- ]
33
-
34
- tags = {}
12
+ Middleware.request_start_at = QueueTime.parse(env)
35
13
 
36
- if times.all? { |t| env[t].present? }
37
- pre_time = (env['traxor.rack.middleware.pre_middleware_end'].to_f - env['traxor.rack.middleware.pre_middleware_start'].to_f)
38
- post_time = (env['traxor.rack.middleware.post_middleware_end'].to_f - env['traxor.rack.middleware.post_middleware_start'].to_f)
39
- middleware_time = (pre_time + post_time) * 1_000
40
- total_time = (env['traxor.rack.middleware.post_middleware_end'].to_f - env['traxor.rack.middleware.pre_middleware_start'].to_f) * 1_000
41
-
42
- if controller
43
- method = env['REQUEST_METHOD'].to_s
44
- tags = { controller: Traxor.normalize_name(controller.class), action: Traxor.normalize_name(controller.action_name), method: Traxor.normalize_name(method) }
45
- controller_path = tags.values.join('.')
46
-
47
- Metric.measure 'rack.request.middleware.duration', "#{middleware_time.round(2)}ms", tags
48
- Metric.measure "rack.request.middleware.duration.#{controller_path}", "#{middleware_time.round(2)}ms", tags
49
- Metric.measure "rack.request.queue.duration.#{controller_path}", "#{queue_duration.round(2)}ms", tags if queue_duration
50
- Metric.measure "rack.request.duration.#{controller_path}", "#{total_time.round(2)}ms", tags
51
- Metric.count "rack.request.count.#{controller_path}", 1, tags
52
- end
53
-
54
- Metric.measure 'rack.request.duration', "#{total_time.round(2)}ms", tags
55
- end
14
+ Middleware.pre_start_at = Time.now.utc
15
+ status, headers, body = @app.call(env)
16
+ Middleware.post_finish_at = Time.now.utc
56
17
 
57
- Metric.measure 'rack.request.queue.duration', "#{queue_duration.round(2)}ms", tags if queue_duration
58
- Metric.count 'rack.request.count', 1, tags
18
+ record_metrics
59
19
 
60
20
  [status, headers, body]
61
21
  end
62
22
 
63
- def parse_request_queue(string)
64
- value = string.to_s.gsub(/t=/, '')
65
- DIVISORS.each do |divisor|
66
- begin
67
- t = Time.at(value.to_f / divisor)
68
- return t if t > EARLIEST_ACCEPTABLE_TIME
69
- rescue RangeError
70
- # On Ruby versions built with a 32-bit time_t, attempting to
71
- # instantiate a Time object in the far future raises a RangeError,
72
- # in which case we know we've chosen the wrong divisor.
73
- end
74
- end
75
-
76
- nil
23
+ def record_metrics
24
+ Metric.measure 'rack.request.middleware.duration'.freeze, "#{Middleware.middleware_total.round(2)}ms" if Middleware.middleware_total.positive?
25
+ Metric.measure 'rack.request.duration'.freeze, "#{Middleware.request_total.round(2)}ms" if Middleware.request_total.positive?
26
+ Metric.measure 'rack.request.queue.duration'.freeze, "#{Middleware.request_queue_total.round(2)}ms" if Middleware.request_queue_total.positive?
27
+ Metric.count 'rack.request.count'.freeze, 1
77
28
  end
78
29
  end
79
30
  end
@@ -0,0 +1,33 @@
1
+ module Traxor
2
+ module Rack
3
+ module Middleware
4
+ module QueueTime
5
+ X_REQUEST_START = 'HTTP_X_REQUEST_START'.freeze
6
+
7
+ # any timestamps before this are thrown out and the parser
8
+ # will try again with a larger unit (2000/1/1 UTC)
9
+ EARLIEST_ACCEPTABLE_TIME = Time.at(946_684_800).utc
10
+
11
+ DIVISORS = [1_000_000, 1_000, 1].freeze
12
+
13
+ def self.parse(env)
14
+ return unless env[X_REQUEST_START]
15
+
16
+ value = env[X_REQUEST_START].to_s.sub(/t=/, ''.freeze)
17
+ 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
26
+ end
27
+
28
+ nil
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ require 'traxor/rack/middleware/pre'
2
+ require 'traxor/rack/middleware/post'
3
+
4
+ module Traxor
5
+ module Rack
6
+ module Middleware
7
+ thread_mattr_accessor :pre_start_at,
8
+ :pre_finish_at,
9
+ :post_start_at,
10
+ :post_finish_at,
11
+ :request_start_at
12
+
13
+ def self.time_before
14
+ pre_finish_at.to_f - pre_start_at.to_f
15
+ end
16
+
17
+ def self.time_after
18
+ post_finish_at.to_f - post_start_at.to_f
19
+ end
20
+
21
+ def self.middleware_total
22
+ (time_before + time_after) * 1_000
23
+ end
24
+
25
+ def self.request_total
26
+ (post_finish_at.to_f - pre_start_at.to_f) * 1_000
27
+ end
28
+
29
+ def self.request_queue_total
30
+ (pre_start_at.to_f - request_start_at.to_f) * 1_000
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1 @@
1
+ require 'traxor/rack/middleware'
@@ -1,36 +1,26 @@
1
1
  module Traxor
2
- ActiveSupport::Notifications.subscribe 'process_action.action_controller' do |*args|
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
10
+
11
+ ActiveSupport::Notifications.subscribe 'process_action.action_controller'.freeze do |*args|
3
12
  event = ActiveSupport::Notifications::Event.new(*args)
4
- request = ActionDispatch::Request.new(event.payload[:headers])
5
13
  exception = event.payload[:exception]
6
- controller = normalize_name(event.payload[:controller])
7
- action = normalize_name(event.payload[:action])
8
14
  duration = event.duration || 0.0
9
- db_runtime = event.payload[:db_runtime] || 0
10
- view_runtime = event.payload[:view_runtime] || 0
15
+ db_runtime = event.payload[:db_runtime] || 0.0
16
+ view_runtime = event.payload[:view_runtime] || 0.0
11
17
  ruby_runtime = duration.to_f - db_runtime.to_f - view_runtime.to_f
12
18
 
13
- controller_path = normalize_name("#{controller.underscore}.#{action.underscore}.#{request.method.downcase}")
14
- tags = { controller: controller, action: action, method: normalize_name(request.method) }
15
-
16
- Metric.count 'rails.action_controller.count', 1, tags
17
- Metric.count "rails.action_controller.count.#{controller_path}", 1, tags
18
-
19
- Metric.measure 'rails.action_controller.total.duration', "#{duration.to_f.round(2)}ms", tags
20
- Metric.measure "rails.action_controller.total.duration.#{controller_path}", "#{duration.to_f.round(2)}ms", tags
21
-
22
- Metric.measure 'rails.action_controller.ruby.duration', "#{ruby_runtime.to_f.round(2)}ms", tags
23
- Metric.measure "rails.action_controller.ruby.duration.#{controller_path}", "#{ruby_runtime.to_f.round(2)}ms", tags
24
-
25
- Metric.measure 'rails.action_controller.db.duration', "#{db_runtime.to_f.round(2)}ms", tags
26
- Metric.measure "rails.action_controller.db.duration.#{controller_path}", "#{db_runtime.to_f.round(2)}ms", tags
27
-
28
- Metric.measure 'rails.action_controller.view.duration', "#{view_runtime.to_f.round(2)}ms", tags
29
- Metric.measure "rails.action_controller.view.duration.#{controller_path}", "#{view_runtime.to_f.round(2)}ms", tags
30
-
31
- if exception.present?
32
- Metric.count 'rails.action_controller.exception.count', 1, tags
33
- Metric.count "rails.action_controller.exception.count.#{controller_path}", 1, tags
34
- end
19
+ Metric.count 'rails.action_controller.count'.freeze, 1
20
+ Metric.measure 'rails.action_controller.total.duration'.freeze, "#{duration.to_f.round(2)}ms"
21
+ Metric.measure 'rails.action_controller.ruby.duration'.freeze, "#{ruby_runtime.to_f.round(2)}ms"
22
+ Metric.measure 'rails.action_controller.db.duration'.freeze, "#{db_runtime.to_f.round(2)}ms"
23
+ Metric.measure 'rails.action_controller.view.duration'.freeze, "#{view_runtime.to_f.round(2)}ms"
24
+ Metric.count 'rails.action_controller.exception.count'.freeze, 1 if exception
35
25
  end
36
26
  end
@@ -1,9 +1,8 @@
1
1
  module Traxor
2
- ActiveSupport::Notifications.subscribe 'deliver.action_mailer' do |*args|
2
+ ActiveSupport::Notifications.subscribe 'deliver.action_mailer'.freeze do |*args|
3
3
  event = ActiveSupport::Notifications::Event.new(*args)
4
- tags = { mailer: normalize_name(event.payload[:mailer]) }
4
+ tags = { action_mailer_class_name: event.payload[:mailer] }
5
5
 
6
- Metric.count 'rails.action_mailer.sent.count', 1, tags
7
- Metric.count "rails.action_mailer.sent.count.#{tags[:mailer]}", 1, tags
6
+ Metric.count 'rails.action_mailer.sent.count'.freeze, 1, tags
8
7
  end
9
8
  end
@@ -1,26 +1,24 @@
1
1
  module Traxor
2
- ActiveSupport::Notifications.subscribe 'sql.active_record' do |*args|
2
+ ActiveSupport::Notifications.subscribe 'sql.active_record'.freeze do |*args|
3
3
  event = ActiveSupport::Notifications::Event.new(*args)
4
4
  sql = event.payload[:sql].to_s.strip.upcase
5
5
  name = event.payload[:name].to_s.strip.upcase
6
- next if ['SCHEMA'].any?(name)
6
+ next if ['SCHEMA'.freeze].any?(name)
7
7
  tags = {}
8
- tags[:name] = normalize_name(name.split.first) if name.length.positive?
8
+ tags[:active_record_class_name] = name.split.first if name.length.positive?
9
9
 
10
- Metric.count 'rails.active_record.statements.count', 1, tags
11
- Metric.count 'rails.active_record.statements.select.count', 1, tags if sql.starts_with?('SELECT')
12
- Metric.count 'rails.active_record.statements.insert.count', 1, tags if sql.starts_with?('INSERT')
13
- Metric.count 'rails.active_record.statements.update.count', 1, tags if sql.starts_with?('UPDATE')
14
- Metric.count 'rails.active_record.statements.delete.count', 1, tags if sql.starts_with?('DELETE')
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
15
  end
16
16
 
17
- ActiveSupport::Notifications.subscribe 'instantiation.active_record' do |*args|
17
+ ActiveSupport::Notifications.subscribe 'instantiation.active_record'.freeze do |*args|
18
18
  event = ActiveSupport::Notifications::Event.new(*args)
19
19
  record_count = event.payload[:record_count]
20
- class_name = normalize_name(event.payload[:class_name])
21
- tags = { class: class_name }
20
+ tags = { active_record_class_name: event.payload[:class_name] }
22
21
 
23
- Metric.count 'rails.active_record.instantiation.count', record_count, tags
24
- Metric.count "rails.active_record.instantiation.count.#{class_name}", record_count, tags
22
+ Metric.count 'rails.active_record.instantiation.count'.freeze, record_count, tags
25
23
  end
26
24
  end
data/lib/traxor/rails.rb CHANGED
@@ -1,12 +1,12 @@
1
+ require 'traxor/rack'
2
+
1
3
  module Traxor
2
4
  class Rails < ::Rails::Engine
3
- initializer 'traxor.setup' do |app|
4
- Traxor.configure do |config|
5
- config.logger = ::Rails.logger
5
+ initializer 'traxor.setup'.freeze do |app|
6
+ if ::Rails.env.development? || ::Rails.env.test?
7
+ Traxor.initialize_logger(::Rails.root.join('log'.freeze, 'traxor.log'.freeze))
6
8
  end
7
9
 
8
- require 'traxor/rack/middleware/pre'
9
- require 'traxor/rack/middleware/post'
10
10
  app.config.middleware.insert 0, Traxor::Rack::Middleware::Pre
11
11
  app.config.middleware.use Traxor::Rack::Middleware::Post
12
12
 
@@ -25,8 +25,8 @@ module Traxor
25
25
  require 'traxor/faraday' if defined?(Faraday)
26
26
 
27
27
  if defined?(Sidekiq)
28
+ require 'traxor/sidekiq'
28
29
  ::Sidekiq.server_middleware do |chain|
29
- require 'traxor/sidekiq'
30
30
  chain.add Traxor::Sidekiq
31
31
  end
32
32
  end
@@ -3,18 +3,18 @@ require 'benchmark'
3
3
  module Traxor
4
4
  class Sidekiq
5
5
  def call(worker, _job, queue)
6
- tags = { worker: Traxor.normalize_name(worker.class.name), queue: Traxor.normalize_name(queue) }
6
+ tags = Traxor::Tags.sidekiq = {
7
+ sidekiq_worker: worker.class.name,
8
+ sidekiq_queue: queue
9
+ }
7
10
  begin
8
11
  time = Benchmark.ms do
9
12
  yield
10
13
  end
11
- Metric.measure 'sidekiq.worker.duration', "#{time.round(2)}ms", tags
12
- Metric.measure "sidekiq.worker.duration.#{tags[:worker]}", "#{time.round(2)}ms", tags
13
- Metric.count 'sidekiq.worker.count', 1, tags
14
- Metric.count "sidekiq.worker.count.#{tags[:worker]}", 1, tags
14
+ Metric.measure 'sidekiq.worker.duration'.freeze, "#{time.round(2)}ms", tags
15
+ Metric.count 'sidekiq.worker.count'.freeze, 1, tags
15
16
  rescue StandardError
16
- Metric.count 'sidekiq.worker.exception.count', 1, tags
17
- Metric.count "sidekiq.worker.exception.count.#{tags[:worker]}", 1, tags
17
+ Metric.count 'sidekiq.worker.exception.count'.freeze, 1, tags
18
18
  raise
19
19
  end
20
20
  end
@@ -0,0 +1,11 @@
1
+ require 'active_support/core_ext/module/attribute_accessors_per_thread'
2
+
3
+ module Traxor
4
+ module Tags
5
+ thread_mattr_accessor :controller, :sidekiq
6
+
7
+ def self.all
8
+ (controller || {}).merge(sidekiq || {})
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Traxor
2
- VERSION = '0.1.13'.freeze
2
+ VERSION = '0.1.14'.freeze
3
3
  end
data/lib/traxor.rb CHANGED
@@ -1,25 +1,22 @@
1
- require 'active_support/configurable'
2
- require 'active_support/inflector/inflections'
3
1
  require 'logger'
4
2
  require 'traxor/faraday' if defined?(Faraday)
5
3
  require 'traxor/metric'
6
- require 'traxor/rails' if defined?(Rails)
4
+ require 'traxor/rack' if defined?(Rack)
5
+ require 'traxor/rails' if defined?(Rails::Engine)
7
6
  require 'traxor/sidekiq' if defined?(Sidekiq)
7
+ require 'traxor/tags'
8
8
  require 'traxor/version'
9
9
 
10
10
  module Traxor
11
- include ActiveSupport::Configurable
12
- config_accessor :logger do
13
- Logger.new(STDOUT).tap do |l|
14
- l.level = Logger::INFO
15
- end
16
- end
17
-
18
- def self.configure
19
- yield config
11
+ def self.logger
12
+ defined?(@logger) ? @logger : initialize_logger
20
13
  end
21
14
 
22
- def self.normalize_name(value)
23
- value.to_s.gsub(/::/, '.').underscore
15
+ def self.initialize_logger(log_target = STDOUT)
16
+ @logger = Logger.new(log_target, level: Logger::INFO, progname: name)
17
+ @logger.formatter = proc do |severity, _time, progname, msg|
18
+ "[#{progname}] #{severity} : #{msg}\n"
19
+ end
20
+ @logger
24
21
  end
25
22
  end
data/traxor.gemspec CHANGED
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency 'bundler'
26
26
  spec.add_development_dependency 'faraday'
27
+ spec.add_development_dependency 'lois'
27
28
  spec.add_development_dependency 'pry'
28
29
  spec.add_development_dependency 'pry-byebug'
29
30
  spec.add_development_dependency 'rails'
@@ -32,6 +33,7 @@ Gem::Specification.new do |spec|
32
33
  spec.add_development_dependency 'rspec'
33
34
  spec.add_development_dependency 'rspec_junit_formatter'
34
35
  spec.add_development_dependency 'rubocop'
36
+ spec.add_development_dependency 'rubocop-rspec'
35
37
  spec.add_development_dependency 'sidekiq'
36
38
  spec.add_development_dependency 'simplecov'
37
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traxor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Hansen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-24 00:00:00.000000000 Z
11
+ date: 2018-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: lois
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: pry
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -164,6 +178,20 @@ dependencies:
164
178
  - - ">="
165
179
  - !ruby/object:Gem::Version
166
180
  version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rubocop-rspec
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
167
195
  - !ruby/object:Gem::Dependency
168
196
  name: sidekiq
169
197
  requirement: !ruby/object:Gem::Requirement
@@ -201,7 +229,9 @@ extra_rdoc_files: []
201
229
  files:
202
230
  - ".circleci/config.yml"
203
231
  - ".gitignore"
232
+ - ".reek"
204
233
  - ".rspec"
234
+ - ".rubocop.yml"
205
235
  - ".travis.yml"
206
236
  - Gemfile
207
237
  - Gemfile.lock
@@ -209,6 +239,7 @@ files:
209
239
  - Rakefile
210
240
  - bin/code_climate_reek
211
241
  - bin/console
242
+ - bin/lois
212
243
  - bin/rake
213
244
  - bin/reek
214
245
  - bin/rspec
@@ -217,13 +248,17 @@ files:
217
248
  - lib/traxor.rb
218
249
  - lib/traxor/faraday.rb
219
250
  - lib/traxor/metric.rb
251
+ - lib/traxor/rack.rb
252
+ - lib/traxor/rack/middleware.rb
220
253
  - lib/traxor/rack/middleware/post.rb
221
254
  - lib/traxor/rack/middleware/pre.rb
255
+ - lib/traxor/rack/middleware/queue_time.rb
222
256
  - lib/traxor/rails.rb
223
257
  - lib/traxor/rails/action_controller.rb
224
258
  - lib/traxor/rails/action_mailer.rb
225
259
  - lib/traxor/rails/active_record.rb
226
260
  - lib/traxor/sidekiq.rb
261
+ - lib/traxor/tags.rb
227
262
  - lib/traxor/version.rb
228
263
  - traxor.gemspec
229
264
  homepage: https://github.com/ketiko/traxor