getaround_utils 0.1.15 → 0.2.0
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 +4 -4
- data/.rubocop.yml +4 -0
- data/Gemfile +2 -1
- data/Gemfile.lock +9 -2
- data/README.md +13 -30
- data/lib/getaround_utils.rb +3 -7
- data/lib/getaround_utils/mixins.rb +1 -3
- data/lib/getaround_utils/mixins/loggable.rb +10 -5
- data/lib/getaround_utils/ougai.rb +2 -0
- data/lib/getaround_utils/ougai/deep_key_value_formatter.rb +15 -0
- data/lib/getaround_utils/ougai/json_formatter.rb +16 -0
- data/lib/getaround_utils/railties/lograge.rb +0 -2
- data/lib/getaround_utils/railties/ougai.rb +98 -0
- data/lib/getaround_utils/utils.rb +4 -5
- data/lib/getaround_utils/utils/async_queue.rb +33 -41
- data/lib/getaround_utils/utils/captur_reporter.rb +32 -0
- data/lib/getaround_utils/utils/deep_key_value.rb +16 -15
- data/lib/getaround_utils/utils/http_reporter.rb +5 -23
- data/lib/getaround_utils/version.rb +1 -1
- metadata +7 -7
- data/lib/getaround_utils/log_formatters.rb +0 -3
- data/lib/getaround_utils/log_formatters/deep_key_value.rb +0 -63
- data/lib/getaround_utils/patches.rb +0 -4
- data/lib/getaround_utils/patches/key_value_log_tags.rb +0 -53
- data/lib/getaround_utils/patches/key_value_sidekiq_exceptions.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bdcc5b1460329ad345ef3d8a3be543c4e4d9b5b626d4c71551c698513df16a97
|
4
|
+
data.tar.gz: 876b086f9f1ef2b411409ffcfd5f2d16676fed5fa87d40020f254efc3e2d9b7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42967aa90f0c6384d87d9b1c1d7fde77526b3949a854107b438c9968524736df6e6b044dd8674d92c6d53e36e4c3024debbbc1ef799fcea62a034ad7a733e94d
|
7
|
+
data.tar.gz: c23299cce88fd8bf7ecc9741ddda44e3a14d7e68b2f06fe359fcac88d635a91513c4dfe930437e65054ea30363b07d3aa52d84e7f10a09932d705ba250c120f1
|
data/.rubocop.yml
CHANGED
@@ -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
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
getaround_utils (0.
|
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.
|
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
|
@@ -21,14 +31,14 @@ For more details, [read the spec](spec/getaround_utils/railties/lograge_spec.rb)
|
|
21
31
|
Enables lograge (http logs) with favored default.
|
22
32
|
```
|
23
33
|
class MyClass
|
24
|
-
include
|
34
|
+
include GetaroundUtils::Mixins::Loggable
|
25
35
|
|
26
36
|
def append_infos_to_loggable(payload)
|
27
37
|
payload[:static] = 'value'
|
28
38
|
end
|
29
39
|
|
30
40
|
def action
|
31
|
-
|
41
|
+
loggable_log(:info, 'hello', dynamic: 'value')
|
32
42
|
end
|
33
43
|
end
|
34
44
|
|
@@ -36,34 +46,7 @@ MyClass.new.action # :info message="hello" origin="MyClass" static="value" dynam
|
|
36
46
|
|
37
47
|
```
|
38
48
|
|
39
|
-
For more details, [read the spec](spec/getaround_utils/mixins/
|
40
|
-
|
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
|
-
|
49
|
+
For more details, [read the spec](spec/getaround_utils/mixins/loggable_spec.rb)
|
67
50
|
|
68
51
|
## Misc
|
69
52
|
|
data/lib/getaround_utils.rb
CHANGED
@@ -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
|
@@ -15,7 +15,7 @@ module GetaroundUtils::Mixins::Loggable
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def base_loggable_logger
|
18
|
-
@base_loggable_logger ||= if respond_to?(:logger)
|
18
|
+
@base_loggable_logger ||= if respond_to?(:logger) && !logger.nil?
|
19
19
|
logger
|
20
20
|
elsif defined?(Rails)
|
21
21
|
Rails.logger
|
@@ -25,10 +25,15 @@ module GetaroundUtils::Mixins::Loggable
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def loggable(severity, message, payload = {})
|
28
|
-
|
29
|
-
|
28
|
+
base_loggable_logger.send(
|
29
|
+
:warn,
|
30
|
+
"Deprecated usage of GetaroundUtils::Mixins::Loggable#loggable(*args). Please use GetaroundUtils::Mixins::Loggable#loggable_log(*args) instead"
|
31
|
+
)
|
32
|
+
loggable_log(severity, message, payload)
|
33
|
+
end
|
30
34
|
|
31
|
-
|
32
|
-
|
35
|
+
def loggable_log(severity, message, payload = {})
|
36
|
+
base_append_infos_to_loggable(payload)
|
37
|
+
base_loggable_logger.send(severity.to_sym, message, payload)
|
33
38
|
end
|
34
39
|
end
|
@@ -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,5 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
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,56 +2,48 @@ module GetaroundUtils; end
|
|
2
2
|
module GetaroundUtils::Utils; end
|
3
3
|
|
4
4
|
class GetaroundUtils::Utils::AsyncQueue
|
5
|
-
|
6
|
-
include GetaroundUtils::Mixins::Loggable
|
5
|
+
include GetaroundUtils::Mixins::Loggable
|
7
6
|
|
8
|
-
|
9
|
-
|
7
|
+
MAX_QUEUE_SIZE = 1000
|
8
|
+
BUFFER_SIZE = 50
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
16
|
-
|
18
|
+
def perform
|
19
|
+
raise NotImplementedError
|
20
|
+
end
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
@queue = Queue.new
|
32
|
-
|
33
|
-
@worker = Thread.new do
|
34
|
-
while args = @queue.pop
|
35
|
-
perform(*args)
|
36
|
-
end
|
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
|
-
|
43
|
-
at_exit { terminate }
|
44
|
-
end
|
45
|
-
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?
|
46
37
|
|
47
|
-
|
48
|
-
@queue
|
49
|
-
|
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)
|
50
42
|
end
|
43
|
+
end
|
51
44
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
45
|
+
def terminate
|
46
|
+
@mutex.synchronize { @closed = true }
|
47
|
+
@worker&.join
|
56
48
|
end
|
57
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
|
20
|
+
escape(data)
|
15
21
|
else
|
16
|
-
data.to_s
|
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 = [],
|
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],
|
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],
|
39
|
+
flattify(v, result, path + [k], max_depth)
|
34
40
|
end
|
35
41
|
when Numeric
|
36
|
-
result[path.join(".")] = value
|
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,
|
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
|
-
|
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(
|
19
|
-
|
4
|
+
def initialize(_)
|
5
|
+
loggable_log(:warn, 'use of deprecated class')
|
20
6
|
end
|
21
7
|
|
22
|
-
def report(
|
23
|
-
|
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
|
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.
|
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-
|
12
|
+
date: 2020-09-11 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/
|
211
|
-
- lib/getaround_utils/
|
212
|
-
- lib/getaround_utils/
|
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,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,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
|