getaround_utils 0.1.19 → 0.2.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f23d197d155aa4f24f2e2d745dda9d8b1c945de036cdf619155abbf226adbeb1
4
- data.tar.gz: 2517396bd5ed9a98e375d8edc390c3c1dcd79c5a33f347344d0f6125be11af57
3
+ metadata.gz: aa8204cb23f0266e9a93adc5b0d94eb12c5fe09580b2ef4d2673673a8533b774
4
+ data.tar.gz: b8f000032d02302b61e53016727ef2b45310ecdfde43a063b1a7172433d6c6c3
5
5
  SHA512:
6
- metadata.gz: 43beef4a44ae0a1e99fe96d95ce0a3dff0597e5e71ae522adbb67f4204a01d65ff76dac4b7e07575eca10165fa0f059dca42b0061bb7193f39692a10a9a187d6
7
- data.tar.gz: a05094694bd7d64b80f79425366b3ac6440107e1750a421fdf4449f4e68d34efce4646ba6704a4effb4e928ce4d55441944b8833fc49693cb9a17bfeeeb52bc7
6
+ metadata.gz: feb184c259c0ba0d331b67129f10e0ac623486bbed53554ffd427ede56f1aea0909e3c9063f78399408cdb11e816c960419961e072f0fec5b1d642f7d813bd63
7
+ data.tar.gz: 290b8c6664d68c54f97101767e31b29c614f62bcd88eafb88a2f11d05573ff8870b4c7133fbbfb10aa3ae866e4fc634275587de1656253d26102927658f7a605
@@ -6,6 +6,10 @@ inherit_gem:
6
6
  - .rubocop.yml
7
7
  - .rubocop-rspec.yml
8
8
 
9
+ Layout/MultilineMethodCallIndentation:
10
+ Description: Vertical alignment results in awkward diffs.
11
+ EnforcedStyle: indented
12
+
9
13
  Layout/MultilineAssignmentLayout:
10
14
  Description: Vertical alignment results in awkward diffs.
11
15
  EnforcedStyle: same_line
data/Gemfile CHANGED
@@ -3,5 +3,6 @@ source "https://rubygems.org"
3
3
  gemspec
4
4
 
5
5
  gem "faraday", "~> 1.0"
6
-
6
+ gem "ougai", "~> 1.8"
7
+ gem "request_store_rails", "~> 2.0"
7
8
  gem "webmock", "~> 3.7"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- getaround_utils (0.1.19)
4
+ getaround_utils (0.2.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -105,6 +105,9 @@ GEM
105
105
  nio4r (2.5.2)
106
106
  nokogiri (1.10.5)
107
107
  mini_portile2 (~> 2.4.0)
108
+ oj (3.10.14)
109
+ ougai (1.8.5)
110
+ oj (~> 3.10)
108
111
  parallel (1.18.0)
109
112
  parser (2.6.5.0)
110
113
  ast (~> 2.4.0)
@@ -149,6 +152,8 @@ GEM
149
152
  relaxed-rubocop (2.4)
150
153
  request_store (1.4.1)
151
154
  rack (>= 1.4)
155
+ request_store_rails (2.0.0)
156
+ concurrent-ruby (~> 1.0)
152
157
  rspec (3.9.0)
153
158
  rspec-core (~> 3.9.0)
154
159
  rspec-expectations (~> 3.9.0)
@@ -216,9 +221,11 @@ DEPENDENCIES
216
221
  getaround-rubocop (~> 0.1.0, >= 0.1.0)
217
222
  getaround_utils!
218
223
  lograge (~> 0.11.2)
224
+ ougai (~> 1.8)
219
225
  pry (~> 0.12.2)
220
226
  rails (~> 6.0)
221
227
  rake (~> 10.0)
228
+ request_store_rails (~> 2.0)
222
229
  rspec (~> 3.9, >= 3.9.0)
223
230
  rspec-rails (~> 3.9)
224
231
  rubocop (~> 0.75, >= 0.75.0)
@@ -227,4 +234,4 @@ DEPENDENCIES
227
234
  webmock (~> 3.7)
228
235
 
229
236
  BUNDLED WITH
230
- 2.1.0
237
+ 2.1.4
data/README.md CHANGED
@@ -14,6 +14,16 @@ require 'getaround_utils/railties/lograge'
14
14
 
15
15
  For more details, [read the spec](spec/getaround_utils/railties/lograge_spec.rb)
16
16
 
17
+ ### GetaroundUtils::Railties::Ougai
18
+
19
+ Enables structured log with Ougai.
20
+ ```
21
+ # config/application.rb
22
+ require 'getaround_utils/railties/ougai'
23
+ ```
24
+
25
+ For more details, [read the spec](spec/getaround_utils/railties/ougai_spec.rb)
26
+
17
27
  ## Mixins
18
28
 
19
29
  ### GetaroundUtils::Mixins::Loggable
@@ -38,33 +48,6 @@ MyClass.new.action # :info message="hello" origin="MyClass" static="value" dynam
38
48
 
39
49
  For more details, [read the spec](spec/getaround_utils/mixins/loggable_spec.rb)
40
50
 
41
-
42
- ## Patches
43
-
44
- ### GetaroundUtils::Patches::KeyValueLogTags
45
-
46
- Enables parse-able key-value tags in ActiveRecord::TaggedLogger
47
- ```
48
- # config/application.rb
49
- require 'getaround_utils/patches/key_value_log_tags'
50
- GetaroundUtils::Patches::KeyValueLogTags.enable
51
- ```
52
-
53
- For more details, [read the spec](spec/getaround_utils/patches/key_value_log_tags_spec.rb)
54
-
55
-
56
- ### GetaroundUtils::Patches::KeyValueSidekiqExceptions
57
-
58
- Enables parse-able exception logging from Sidekiq
59
- ```
60
- # config/application.rb
61
- require 'getaround_utils/patches/key_value_sidekiq_exceptions'
62
- GetaroundUtils::Patches::KeyValueSidekiqExceptions.enable
63
- ```
64
-
65
- For more details, [read the spec](spec/getaround_utils/patches/key_value_sidekiq_exceptions_spec.rb)
66
-
67
-
68
51
  ## Misc
69
52
 
70
53
  ### GetaroundUtils::LogFormatters::DeepKeyValue
@@ -1,8 +1,4 @@
1
+ require 'getaround_utils/mixins'
2
+ require 'getaround_utils/ougai'
3
+ require 'getaround_utils/utils'
1
4
  require 'getaround_utils/version'
2
-
3
- module GetaroundUtils
4
- autoload :LogFormatters, 'getaround_utils/log_formatters'
5
- autoload :Mixins, 'getaround_utils/mixins'
6
- autoload :Patches, 'getaround_utils/patches'
7
- autoload :Utils, 'getaround_utils/utils'
8
- end
@@ -1,3 +1 @@
1
- module GetaroundUtils::Mixins
2
- autoload :Loggable, 'getaround_utils/mixins/loggable'
3
- end
1
+ require 'getaround_utils/mixins/loggable'
@@ -33,10 +33,7 @@ module GetaroundUtils::Mixins::Loggable
33
33
  end
34
34
 
35
35
  def loggable_log(severity, message, payload = {})
36
- payload = { message: message }.merge(payload)
37
36
  base_append_infos_to_loggable(payload)
38
-
39
- message = GetaroundUtils::Utils::DeepKeyValue.serialize(payload.compact)
40
- base_loggable_logger.send(severity.to_sym, message)
37
+ base_loggable_logger.send(severity.to_sym, msg: message, **payload)
41
38
  end
42
39
  end
@@ -0,0 +1,2 @@
1
+ require 'getaround_utils/ougai/deep_key_value_formatter'
2
+ require 'getaround_utils/ougai/json_formatter'
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ougai'
4
+ require 'getaround_utils/utils/deep_key_value'
5
+
6
+ module GetaroundUtils; end
7
+ module GetaroundUtils::Ougai; end
8
+
9
+ class GetaroundUtils::Ougai::DeepKeyValueFormatter < Ougai::Formatters::Base
10
+ def _call(severity, _time, progname, data)
11
+ message = data.delete(:msg)
12
+ data[:message] = message if message != 'No message'
13
+
14
+ payload = { severity: severity, progname: progname }.merge(data).compact
15
+ GetaroundUtils::Utils::DeepKeyValue.serialize(payload) + "\n"
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module GetaroundUtils; end
6
+ module GetaroundUtils::Ougai; end
7
+
8
+ class GetaroundUtils::Ougai::JsonFormatter < Ougai::Formatters::Base
9
+ def _call(severity, _time, progname, data)
10
+ message = data.delete(:msg)
11
+ data = { caption: message }.merge(data) \
12
+ unless message == 'No message'
13
+
14
+ payload = { severity: severity, progname: progname }.merge(data).compact
15
+ JSON.dump(payload) + "\n"
16
+ end
17
+ end
@@ -1,5 +1,4 @@
1
1
  require 'rails/railtie'
2
- require 'getaround_utils/log_formatters/deep_key_value'
3
2
  require 'lograge'
4
3
 
5
4
  module GetaroundUtils; end
@@ -26,6 +25,5 @@ class GetaroundUtils::Railties::Lograge < Rails::Railtie
26
25
  end
27
26
 
28
27
  config.lograge.enabled = true
29
- config.lograge.formatter = GetaroundUtils::LogFormatters::DeepKeyValue.for_lograge
30
28
  config.lograge.custom_options = ->(event) { event.payload[:lograge] }
31
29
  end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'getaround_utils/ougai/deep_key_value_formatter'
4
+ require 'request_store'
5
+ require 'rails/railtie'
6
+ require 'ougai'
7
+
8
+ module GetaroundUtils; end
9
+ module GetaroundUtils::Railties; end
10
+
11
+ # Rails-compatible Ougai Logger
12
+ # https://github.com/tilfin/ougai/wiki/Use-as-Rails-logger#define-a-custom-logger
13
+ class OugaiRailsLogger < Ougai::Logger
14
+ include ActiveSupport::LoggerThreadSafeLevel
15
+ include Rails::VERSION::MAJOR < 6 ? LoggerSilence : ActiveSupport::LoggerSilence
16
+ end
17
+
18
+ # Patch for ActiveSupport::TaggedLogging
19
+ # https://github.com/tilfin/ougai/wiki/Use-as-Rails-logger#with-activesupporttaggedlogging
20
+ module OugaiTaggedLoggingFormatter
21
+ def call(severity, time, progname, data)
22
+ data = { msg: data.to_s } unless data.is_a?(Hash)
23
+ data[:tags] = current_tags if current_tags.any?
24
+ _call(severity, time, progname, data)
25
+ end
26
+ end
27
+
28
+ # Custom middleware to persist request metadatas
29
+ # https://github.com/tilfin/ougai/issues/73#issuecomment-475866224
30
+ # https://github.com/tilfin/ougai/issues/107#issuecomment-636050223
31
+ class OugaiRequestStoreMiddleware
32
+ def initialize(app)
33
+ @app = app
34
+ end
35
+
36
+ def call(env)
37
+ RequestStore.store[:ougai] = { request_id: env['action_dispatch.request_id'] }
38
+ @app.call(env)
39
+ end
40
+ end
41
+
42
+ class GetaroundUtils::Railties::Ougai < Rails::Railtie
43
+ log_level = ENV.fetch('LOG_LEVEL', :info).to_sym
44
+
45
+ config.ougai_logger = OugaiRailsLogger.new(STDOUT)
46
+ config.ougai_logger.after_initialize if Rails::VERSION::MAJOR < 6
47
+ config.ougai_logger.level = log_level
48
+ config.ougai_logger.formatter = GetaroundUtils::Ougai::DeepKeyValueFormatter.new
49
+ config.ougai_logger.before_log = lambda do |data|
50
+ request_store = RequestStore.store[:ougai] || {}
51
+ data.merge!(request_store) if request_store&.any?
52
+
53
+ sidekiq_context = Thread.current[:sidekiq_context] || {}
54
+ data.merge!(sidekiq: sidekiq_context) if sidekiq_context&.any?
55
+ end
56
+
57
+ initializer :getaround_utils_ougai, before: :initialize_logger do |app|
58
+ LOG_LEVELS = {
59
+ Logger::DEBUG => :debug,
60
+ Logger::INFO => :info,
61
+ Logger::WARN => :warn,
62
+ Logger::ERROR => :error,
63
+ Logger::FATAL => :fatal,
64
+ }.freeze
65
+
66
+ app.config.log_level = LOG_LEVELS[config.ougai_logger.level] || :info
67
+ app.config.logger = config.ougai_logger
68
+ end
69
+
70
+ initializer :getaround_utils_ougai_middleware do |app|
71
+ app.config.app_middleware.insert_after ActionDispatch::RequestId, OugaiRequestStoreMiddleware
72
+ end
73
+
74
+ initializer :getaround_utils_ougai_activesupport do
75
+ ActiveSupport::TaggedLogging::Formatter.prepend OugaiTaggedLoggingFormatter
76
+ end
77
+
78
+ initializer :getaround_utils_ougai_lograge do |app|
79
+ next unless defined?(Lograge)
80
+
81
+ app.config.lograge.logger = app.config.logger
82
+ app.config.lograge.formatter = Lograge::Formatters::Raw.new
83
+ end
84
+
85
+ initializer :getaround_utils_ougai_sidekiq do
86
+ next unless defined?(Sidekiq)
87
+
88
+ # https://github.com/tilfin/ougai/wiki/Customize-Sidekiq-logger
89
+ Sidekiq.logger = config.ougai_logger
90
+
91
+ Sidekiq.configure_server do |config|
92
+ config.error_handlers.shift
93
+ config.error_handlers << lambda do |ex, ctx|
94
+ Sidekiq.logger.warn(ex, job: ctx[:job])
95
+ end
96
+ end
97
+ end
98
+ end
@@ -1,5 +1,4 @@
1
- module GetaroundUtils::Utils
2
- autoload :AsyncQueue, 'getaround_utils/utils/async_queue'
3
- autoload :DeepKeyValue, 'getaround_utils/utils/deep_key_value'
4
- autoload :HttpReporter, 'getaround_utils/utils/http_reporter'
5
- end
1
+ require 'getaround_utils/utils/async_queue'
2
+ require 'getaround_utils/utils/deep_key_value'
3
+ require 'getaround_utils/utils/http_reporter'
4
+ require 'getaround_utils/utils/captur_reporter'
@@ -2,58 +2,48 @@ module GetaroundUtils; end
2
2
  module GetaroundUtils::Utils; end
3
3
 
4
4
  class GetaroundUtils::Utils::AsyncQueue
5
- class << self
6
- include GetaroundUtils::Mixins::Loggable
5
+ include GetaroundUtils::Mixins::Loggable
7
6
 
8
- MAX_QUEUE_SIZE = 100
9
- MUTEX = Mutex.new
7
+ MAX_QUEUE_SIZE = 1000
8
+ BUFFER_SIZE = 50
10
9
 
11
- def perform
12
- raise NotImplementedError
13
- end
10
+ def initialize
11
+ @queue = []
12
+ @mutex = Mutex.new
13
+ @closed = false
14
+ @worker = Thread.new(&method(:thread_run))
15
+ at_exit { terminate }
16
+ end
14
17
 
15
- def perform_async(*args)
16
- start_once!
18
+ def perform
19
+ raise NotImplementedError
20
+ end
17
21
 
18
- if @queue.size > MAX_QUEUE_SIZE
19
- loggable('warn', 'Queue is overflowing')
20
- return
22
+ def push(payload)
23
+ @mutex.synchronize do
24
+ if @queue.size >= MAX_QUEUE_SIZE
25
+ loggable_log(:error, 'queue overflow')
26
+ else
27
+ @queue.push(payload)
21
28
  end
22
-
23
- @queue.push(args)
24
29
  end
30
+ end
25
31
 
26
- def start_once!
27
- MUTEX.synchronize do
28
- return unless @parent.nil?
29
-
30
- @parent = Process.pid
31
- @queue = Queue.new
32
-
33
- @worker = Thread.new do
34
- while args = @queue.pop
35
- begin
36
- perform(*args)
37
- rescue ClosedQueueError
38
- nil
39
- rescue StandardError => e
40
- loggable('error', e.message, class: e.class.to_s, backtrace: e.backtrace)
41
- end
42
- end
43
- end
44
-
45
- at_exit { terminate }
46
- end
47
- end
32
+ def thread_run
33
+ loop do
34
+ buffer = @mutex.synchronize { @queue.shift(BUFFER_SIZE) }
35
+ loggable_log(:debug, 'thread_run', buffer_size: buffer.size)
36
+ return if @closed && buffer.empty?
48
37
 
49
- def terminate
50
- @queue&.close
51
- @worker&.join
38
+ perform(buffer) unless buffer.empty?
39
+ sleep(1) unless @mutex.synchronize { @queue.any? }
40
+ rescue StandardError => e
41
+ loggable_log(:error, e.message, class: e.class.to_s, backtrace: e.backtrace)
52
42
  end
43
+ end
53
44
 
54
- def reset
55
- terminate
56
- @parent = nil
57
- end
45
+ def terminate
46
+ @mutex.synchronize { @closed = true }
47
+ @worker&.join
58
48
  end
59
49
  end
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+ require 'faraday'
3
+ require 'getaround_utils/utils/async_queue'
4
+
5
+ class GetaroundUtils::Utils::CapturReporter < GetaroundUtils::Utils::AsyncQueue
6
+ CAPTUR_URL = ENV['CAPTUR_URL']
7
+
8
+ def perform(events)
9
+ return unless CAPTUR_URL&.match('^https?://')
10
+
11
+ Faraday.post(CAPTUR_URL) do |req|
12
+ req.options[:open_timeout] = 1
13
+ req.options[:timeout] = 1
14
+ req.headers = { 'Content-Type': 'application/json' }
15
+ req.body = JSON.generate(events: events, metas: metas)
16
+ end
17
+ end
18
+
19
+ def metas
20
+ {}
21
+ end
22
+
23
+ def push(uuid:, type:, anonymous_id:, timestamp: nil, attributes: {})
24
+ super(
25
+ uuid: uuid,
26
+ type: type,
27
+ timestamp: timestamp || Time.now.iso8601,
28
+ anonymous_id: anonymous_id,
29
+ attributes: attributes,
30
+ )
31
+ end
32
+ end
@@ -2,47 +2,48 @@ module GetaroundUtils; end
2
2
  module GetaroundUtils::Utils; end
3
3
 
4
4
  module GetaroundUtils::Utils::DeepKeyValue
5
+ def self.escape(value, max_length = 512)
6
+ value = value[1...-1] if value =~ /^".*"$/
7
+ value = "#{value[0...max_length]} ..." if value.length >= max_length
8
+ value.inspect
9
+ end
10
+
5
11
  def self.serialize(data)
6
12
  case data
7
13
  when Array
8
- flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ')
14
+ flattify(data).map { |key, value| "#{key}=#{serialize(value)}" }.join(' ')
9
15
  when Hash
10
- flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ')
16
+ flattify(data).map { |key, value| "#{key}=#{serialize(value)}" }.join(' ')
11
17
  when Numeric
12
18
  data.to_s
13
19
  when String
14
- data =~ /^".*"$/ ? data : data.inspect
20
+ escape(data)
15
21
  else
16
- data.to_s.inspect
22
+ escape(data.to_s)
17
23
  end
18
24
  end
19
25
 
20
26
  # https://stackoverflow.com/questions/48836464/how-to-flatten-a-hash-making-each-key-a-unique-value
21
- def self.flattify(value, result = {}, path = [], max_length = 512, max_depth = 5)
27
+ def self.flattify(value, result = {}, path = [], max_depth = 5)
22
28
  if path.length > max_depth
23
- result[path.join(".")] = '"..."'
29
+ result[path.join(".")] = '...'
24
30
  return result
25
31
  end
26
32
  case value
27
33
  when Array
28
34
  value.each.with_index(0) do |v, i|
29
- flattify(v, result, path + [i], max_length, max_depth)
35
+ flattify(v, result, path + [i], max_depth)
30
36
  end
31
37
  when Hash
32
38
  value.each do |k, v|
33
- flattify(v, result, path + [k], max_length, max_depth)
39
+ flattify(v, result, path + [k], max_depth)
34
40
  end
35
41
  when Numeric
36
- result[path.join(".")] = value.to_s
42
+ result[path.join(".")] = value
37
43
  when String
38
- value = if value =~ /^".*"$/
39
- value.length >= max_length ? %{#{value[0...max_length]} ..."} : value
40
- else
41
- value.length >= max_length ? %{#{value[0...max_length]} ...}.inspect : value.inspect
42
- end
43
44
  result[path.join(".")] = value
44
45
  else
45
- flattify(value.to_s, result, path, max_length, max_depth)
46
+ flattify(value.to_s, result, path, max_depth)
46
47
  end
47
48
  result
48
49
  end
@@ -1,29 +1,11 @@
1
- require 'json'
2
- require 'faraday'
3
- require 'getaround_utils/utils/async_queue'
4
-
5
1
  class GetaroundUtils::Utils::HttpReporter
6
- class AsyncQueue < GetaroundUtils::Utils::AsyncQueue
7
- def self.perform(url:, params: {}, headers: {}, body: nil)
8
- Faraday.post(url) do |req|
9
- req.options[:open_timeout] = 1
10
- req.options[:timeout] = 1
11
- req.params = params
12
- req.headers = headers
13
- req.body = body
14
- end
15
- end
16
- end
2
+ include GetaroundUtils::Mixins::Loggable
17
3
 
18
- def initialize(url:)
19
- @url = url
4
+ def initialize(_)
5
+ loggable_log(:warn, 'use of deprecated class')
20
6
  end
21
7
 
22
- def report(event)
23
- AsyncQueue.perform_async(
24
- url: @url,
25
- headers: { 'Content-Type' => 'application/json' },
26
- body: JSON.generate(event)
27
- )
8
+ def report(_)
9
+ loggable_log(:warn, 'use of deprecated class')
28
10
  end
29
11
  end
@@ -1,3 +1,3 @@
1
1
  module GetaroundUtils
2
- VERSION = '0.1.19'.freeze
2
+ VERSION = '0.2.6'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: getaround_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Drivy
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-02-04 00:00:00.000000000 Z
12
+ date: 2020-09-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -203,16 +203,16 @@ files:
203
203
  - README.md
204
204
  - getaround_utils.gemspec
205
205
  - lib/getaround_utils.rb
206
- - lib/getaround_utils/log_formatters.rb
207
- - lib/getaround_utils/log_formatters/deep_key_value.rb
208
206
  - lib/getaround_utils/mixins.rb
209
207
  - lib/getaround_utils/mixins/loggable.rb
210
- - lib/getaround_utils/patches.rb
211
- - lib/getaround_utils/patches/key_value_log_tags.rb
212
- - lib/getaround_utils/patches/key_value_sidekiq_exceptions.rb
208
+ - lib/getaround_utils/ougai.rb
209
+ - lib/getaround_utils/ougai/deep_key_value_formatter.rb
210
+ - lib/getaround_utils/ougai/json_formatter.rb
213
211
  - lib/getaround_utils/railties/lograge.rb
212
+ - lib/getaround_utils/railties/ougai.rb
214
213
  - lib/getaround_utils/utils.rb
215
214
  - lib/getaround_utils/utils/async_queue.rb
215
+ - lib/getaround_utils/utils/captur_reporter.rb
216
216
  - lib/getaround_utils/utils/deep_key_value.rb
217
217
  - lib/getaround_utils/utils/http_reporter.rb
218
218
  - lib/getaround_utils/version.rb
@@ -1,3 +0,0 @@
1
- module GetaroundUtils::LogFormatters
2
- autoload :DeepKeyValue, 'getaround_utils/log_formatters/deep_key_value'
3
- end
@@ -1,63 +0,0 @@
1
- module GetaroundUtils; end
2
- module GetaroundUtils::LogFormatters; end
3
-
4
- require 'getaround_utils/utils/deep_key_value'
5
-
6
- class GetaroundUtils::LogFormatters::DeepKeyValue
7
- module Shared
8
- def normalize(message)
9
- if message.is_a?(Hash)
10
- GetaroundUtils::Utils::DeepKeyValue.serialize(message.compact)
11
- elsif message.is_a?(String) && message.match(/^[^ =]+=/)
12
- message
13
- else
14
- GetaroundUtils::Utils::DeepKeyValue.serialize(message: message.to_s)
15
- end
16
- end
17
- end
18
-
19
- class Base
20
- include GetaroundUtils::LogFormatters::DeepKeyValue::Shared
21
-
22
- def call(severity, _datetime, appname, message)
23
- payload = { severity: severity, appname: appname }
24
- "#{normalize(payload)} #{normalize(message)}\n"
25
- end
26
- end
27
-
28
- def self.new
29
- Base.new
30
- end
31
-
32
- class Lograge
33
- def call(message)
34
- message = message.compact if message.is_a?(Hash)
35
- GetaroundUtils::Utils::DeepKeyValue.serialize(message)
36
- end
37
- end
38
-
39
- def self.for_lograge
40
- Lograge.new
41
- end
42
-
43
- class Sidekiq < Base
44
- def sidekiq_context
45
- context = Thread.current&.fetch(:sidekiq_context, nil)
46
- context.is_a?(Hash) ? context : {}
47
- end
48
-
49
- def sidekiq_tid
50
- Thread.current&.fetch('sidekiq_tid', nil) || (Thread.current&.object_id ^ ::Process.pid).to_s(36)
51
- end
52
-
53
- def call(severity, _datetime, appname, message)
54
- payload = { severity: severity, appname: appname }
55
- sidekiq = sidekiq_context.merge(tid: sidekiq_tid).compact
56
- "#{normalize(payload)} #{normalize(message)} #{normalize(sidekiq: sidekiq)}\n"
57
- end
58
- end
59
-
60
- def self.for_sidekiq
61
- Sidekiq.new
62
- end
63
- end
@@ -1,4 +0,0 @@
1
- module GetaroundUtils::Patches
2
- autoload :KeyValueLogTags, 'getaround_utils/patches/key_value_log_tags'
3
- autoload :KeyValueSidekiqExceptions, 'getaround_utils/patches/key_value_sidekiq_exceptions'
4
- end
@@ -1,53 +0,0 @@
1
- require 'getaround_utils/log_formatters/deep_key_value'
2
-
3
- module GetaroundUtils; end
4
- module GetaroundUtils::Patches; end
5
-
6
- ##
7
- # Augment the ActiveRecord::TaggedLogging tag formatting
8
- #
9
- # Tags are defined either as String, Proc or a property of request
10
- #
11
- # Originally they will be formatted as `[tag_value]`
12
- # This patch will instead attempt to serialize the tag with GetaroundUtils::DeepKeyValueSerializer
13
- #
14
- # ie:
15
- # - for a String `value` it would yield `"value"`
16
- # - for a Symbol `:request_id` it would yield `request_id="request_id_value"`
17
- # - for a Proc `-> {key: :val}` it would yield `key="value"`
18
-
19
- class GetaroundUtils::Patches::KeyValueLogTags
20
- module TaggedLoggingFormatter
21
- include GetaroundUtils::LogFormatters::DeepKeyValue::Shared
22
-
23
- def tags_text
24
- "#{current_tags.join(' ')} " if current_tags.any?
25
- end
26
-
27
- def call(severity, datetime, appname, message)
28
- original_method = method(__method__).super_method.super_method
29
- message = "#{tags_text}#{normalize(message)}"
30
- original_method.call(severity, datetime, appname, message)
31
- end
32
- end
33
-
34
- module RackLogger
35
- def compute_tags(request)
36
- @taggers.collect do |tag|
37
- case tag
38
- when Proc
39
- GetaroundUtils::Utils::DeepKeyValue.serialize(tag.call(request))
40
- when Symbol
41
- GetaroundUtils::Utils::DeepKeyValue.serialize(tag => request.send(tag))
42
- else
43
- GetaroundUtils::Utils::DeepKeyValue.serialize(tag)
44
- end
45
- end
46
- end
47
- end
48
-
49
- def self.enable
50
- ActiveSupport::TaggedLogging::Formatter.prepend TaggedLoggingFormatter
51
- Rails::Rack::Logger.prepend RackLogger
52
- end
53
- end
@@ -1,22 +0,0 @@
1
- require 'sidekiq/exception_handler'
2
- require 'getaround_utils/utils/deep_key_value'
3
-
4
- module GetaroundUtils; end
5
- module GetaroundUtils::Patches; end
6
-
7
- class GetaroundUtils::Patches::KeyValueSidekiqExceptions
8
- module ExceptionHandlerLogger
9
- def call(exception, ctx)
10
- payload = {}
11
- payload[:message] = exception.message
12
- payload[:exception] = exception.class.name
13
- payload[:backtrace] = exception.backtrace&.join("\n")
14
- payload[:sidekiq] = ctx
15
- Sidekiq.logger.warn(GetaroundUtils::Utils::DeepKeyValue.serialize(payload.compact))
16
- end
17
- end
18
-
19
- def self.enable
20
- Sidekiq::ExceptionHandler::Logger.prepend ExceptionHandlerLogger
21
- end
22
- end