getaround_utils 0.1.20 → 0.2.0

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: ac89bb57e946209df2cd90a27662dd6eafe09eaf1ad75c70e0120843600eaa73
4
- data.tar.gz: b5e42696a498a174113437cf8350b1eb742391f74588621a3bae00e12cab7fc4
3
+ metadata.gz: bdcc5b1460329ad345ef3d8a3be543c4e4d9b5b626d4c71551c698513df16a97
4
+ data.tar.gz: 876b086f9f1ef2b411409ffcfd5f2d16676fed5fa87d40020f254efc3e2d9b7c
5
5
  SHA512:
6
- metadata.gz: 20debe44bc0c094ce9dcc3cde822eb7565c6c399cc4ba5849c2a6c5bbc249c9fa96e8fda50648c9974c03b1177030b3880aab5df65c3eba6ea9632845e796d69
7
- data.tar.gz: 3017542f9d2b157a8f6ee9637c5659212a3fa3ad8803975ac2fd8b76e255487183e7b671944a6ec8a9b841890917dfb0c3ae7f3973eba0966257d7a9089c0161
6
+ metadata.gz: 42967aa90f0c6384d87d9b1c1d7fde77526b3949a854107b438c9968524736df6e6b044dd8674d92c6d53e36e4c3024debbbc1ef799fcea62a034ad7a733e94d
7
+ data.tar.gz: c23299cce88fd8bf7ecc9741ddda44e3a14d7e68b2f06fe359fcac88d635a91513c4dfe930437e65054ea30363b07d3aa52d84e7f10a09932d705ba250c120f1
@@ -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.20)
4
+ getaround_utils (0.2.0)
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, 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,15 @@
1
+ require 'ougai'
2
+ require 'getaround_utils/utils/deep_key_value'
3
+
4
+ module GetaroundUtils; end
5
+ module GetaroundUtils::Ougai; end
6
+
7
+ class GetaroundUtils::Ougai::DeepKeyValuesFormatter < Ougai::Formatters::Base
8
+ def _call(severity, _time, progname, data)
9
+ data.delete(:msg) if data[:msg] == 'No message'
10
+ data = data.except(:msg).merge(message: data[:msg])
11
+
12
+ payload = { severity: severity, progname: progname }.merge(data).compact!
13
+ GetaroundUtils::Utils::DeepKeyValue.serialize(payload) + "\n"
14
+ end
15
+ end
@@ -0,0 +1,16 @@
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
+ data.delete(:msg) if data[:msg] == 'No message'
11
+ data = data.except(:msg).merge(message: data[:msg])
12
+
13
+ payload = { severity: severity, progname: progname }.merge(data).compact!
14
+ JSON.dump(payload) + "\n"
15
+ end
16
+ 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::DeepKeyValuesFormatter.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.pop
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,6 +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
- autoload :CapturReporter, 'getaround_utils/utils/captur_reporter'
6
- 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'
@@ -1,3 +1,3 @@
1
1
  module GetaroundUtils
2
- VERSION = '0.1.20'.freeze
2
+ VERSION = '0.2.0'.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.20
4
+ version: 0.2.0
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-15 00:00:00.000000000 Z
12
+ date: 2020-09-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -203,14 +203,13 @@ 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
216
215
  - lib/getaround_utils/utils/captur_reporter.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