getaround_utils 0.1.6 → 0.1.7

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
- SHA256:
3
- metadata.gz: 482b5de4b125e852c88c47c579abf2fc0b1dc3cb529cfae3297867bd9230dd77
4
- data.tar.gz: 10aa0c47f8f1c8c2fb121364b04106f3d301616976fee63a8328be497edafe65
2
+ SHA1:
3
+ metadata.gz: b8b08a78e3737d628b9e809bb89c670252fc37ba
4
+ data.tar.gz: 34aa0786e2093c01af30981539cf971c275e5f14
5
5
  SHA512:
6
- metadata.gz: 903d6190ddd4afb07c60807aaf0ad4d027ec631ec71c2df264ce4cc288cfd103e0d22417f672fd996e1cd0d1556ec7f92a78b0f3f10ebe2cf9e511808392de5e
7
- data.tar.gz: eb1428e5f52272c858d54413155177a6d8f1c87fad418a67efa3012ed27489444b15f5f71cd6e79c9c617446d22173353d1c5a09d63ffd79cc335b7c09464312
6
+ metadata.gz: 96e165d62becbae525d2c717b2fa503149745ec3d8ca74ca70677d373dded88acdcdd67aadb05aa1474c89dff88ed2e4e34b2ceef2a001d2b13f391c0deecc50
7
+ data.tar.gz: 92a70522d2ba91b312bf5c985d3394f18a54d2dd7e2217ab1fbf973b5f666cafc26c309be3ee11287cf3f604a22bc903b666404b6068823307b2983ebf328940
data/.rubocop.yml CHANGED
@@ -5,3 +5,15 @@ inherit_gem:
5
5
  getaround-rubocop:
6
6
  - .rubocop.yml
7
7
  - .rubocop-rspec.yml
8
+
9
+ Layout/MultilineAssignmentLayout:
10
+ Description: Vertical alignment results in awkward diffs.
11
+ EnforcedStyle: same_line
12
+
13
+ Layout/ElseAlignment:
14
+ Description: Vertical alignment results in awkward diffs.
15
+ Enabled: false
16
+
17
+ Layout/EndAlignment:
18
+ Description: Vertical alignment results in awkward diffs.
19
+ EnforcedStyleAlignWith: start_of_line
data/README.md CHANGED
@@ -14,6 +14,31 @@ 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
+ ## Mixins
18
+
19
+ ### GetaroundUtils::Mixins::Loggable
20
+
21
+ Enables lograge (http logs) with favored default.
22
+ ```
23
+ class MyClass
24
+ include Getaround::Mixins::Loggable
25
+
26
+ def append_infos_to_loggable(payload)
27
+ payload[:static] = 'value'
28
+ end
29
+
30
+ def action
31
+ loggable(:info, dynamic: 'value')
32
+ end
33
+ end
34
+
35
+ MyClass.new.action # :info { origin: 'MyClass', static: 'value', dynamic: 'value' }
36
+
37
+ ```
38
+
39
+ For more details, [read the spec](spec/getaround_utils/mixins/loggable.rb)
40
+
41
+
17
42
  ## Patches
18
43
 
19
44
  ### GetaroundUtils::Patches::KeyValueLogTags
@@ -1,62 +1,55 @@
1
1
  module GetaroundUtils; end
2
2
  module GetaroundUtils::LogFormatters; end
3
3
 
4
- module GetaroundUtils::LogFormatters
5
- class DeepKeyValue
6
- def serialize(data)
7
- case data
8
- when Array
9
- flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ')
10
- when Hash
11
- flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ')
12
- when Numeric
13
- data.to_s
14
- when String
15
- data =~ /^".*"$/ ? data : data.inspect
16
- else
17
- data.to_s.inspect
18
- end
4
+ require 'getaround_utils/utils/deep_key_value_serializer'
5
+
6
+ ##
7
+ # Format logs using key=value notation
8
+ #
9
+ # This logger leverage the fact that ruby Logger does not especially expect message to be string
10
+ # It will attempt to serialize message it is a string otherwise adding it as message=value
11
+
12
+ class GetaroundUtils::LogFormatters::DeepKeyValue < GetaroundUtils::Utils::DeepKeyValueSerializer
13
+ def call(severity, _datetime, appname, message)
14
+ payload = { severity: severity, appname: appname }
15
+ if message.is_a?(Hash)
16
+ "#{serialize(payload.merge(message).compact)}\n"
17
+ else
18
+ "#{serialize(payload.merge(message: message.to_s).compact)}\n"
19
19
  end
20
+ end
20
21
 
21
- # https://stackoverflow.com/questions/48836464/how-to-flatten-a-hash-making-each-key-a-unique-value
22
- def flattify(value, result = {}, path = [])
23
- case value
24
- when Array
25
- value.each.with_index(0) do |v, i|
26
- flattify(v, result, path + [i])
27
- end
28
- when Hash
29
- value.each do |k, v|
30
- flattify(v, result, path + [k])
31
- end
32
- when Numeric
33
- result[path.join(".")] = value.to_s
34
- when String
35
- result[path.join(".")] = value =~ /^".*"$/ ? value : value.inspect
36
- else
37
- result[path.join(".")] = value.to_s.inspect
38
- end
39
- result
22
+ module Lograge
23
+ def call(data)
24
+ data.compact! if data.is_a?(Hash)
25
+ serialize(data)
40
26
  end
27
+ end
28
+
29
+ ##
30
+ # Return a lograge-style LogFormatter
31
+ #
32
+ # This formatter will only take one argument and serialize it
41
33
 
34
+ def self.for_lograge
35
+ new.extend(Lograge)
36
+ end
37
+
38
+ module Sidekiq
42
39
  def call(severity, datetime, appname, message)
43
- payload = { severity: severity, datetime: datetime, appname: appname }
44
- if message.is_a?(Hash)
45
- payload.merge!(message)
46
- else
47
- payload[:message] = message.to_s
48
- end
49
- serialize(payload)
40
+ payload = { tid: Thread.current['sidekiq_tid'] }
41
+ payload.merge!(Thread.current[:sidekiq_context] || {})
42
+ "#{super.chomp} #{serialize(sidekiq: payload.compact)}\n"
50
43
  end
44
+ end
51
45
 
52
- module Lograge
53
- def call(data)
54
- serialize(data)
55
- end
56
- end
46
+ ##
47
+ # Return a sidekiq-style LogFormatter
48
+ #
49
+ # This formatter replicates the default Sidekiq LogFormatter behavior of merging context
50
+ # values from the current Thread's store
57
51
 
58
- def self.for_lograge
59
- new.extend(Lograge)
60
- end
52
+ def self.for_sidekiq
53
+ new.extend(Sidekiq)
61
54
  end
62
55
  end
@@ -0,0 +1,33 @@
1
+ require 'logger'
2
+ module GetaroundUtils; end
3
+ module GetaroundUtils::Mixins; end
4
+
5
+ module GetaroundUtils::Mixins::Loggable
6
+ def class_name
7
+ @class_name ||= is_a?(Class) ? name : self.class.name
8
+ end
9
+
10
+ def base_append_infos_to_loggable(payload)
11
+ payload[:origin] = class_name
12
+ return unless respond_to?(:append_infos_to_loggable)
13
+
14
+ append_infos_to_loggable(payload)
15
+ end
16
+
17
+ def base_loggable_logger
18
+ @base_loggable_logger ||= if respond_to?(:logger)
19
+ logger
20
+ elsif defined?(Rails)
21
+ Rails.logger
22
+ else
23
+ Logger.new(STDOUT)
24
+ end
25
+ end
26
+
27
+ def loggable(severity, payload)
28
+ extra_infos = {}
29
+ base_append_infos_to_loggable(extra_infos)
30
+ payload = payload.merge(extra_infos).compact
31
+ base_loggable_logger.send(severity.to_sym, payload)
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module GetaroundUtils::Mixins
2
+ autoload :Loggable, 'getaround_utils/mixins/loggable'
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'rails/railtie'
2
+ module GetaroundUtils; end
3
+ module GetaroundUtils::Patches; end
4
+
5
+ ##
6
+ # Fixes the ActiveSupport::TaggedLogging::Formatter string coercion
7
+ #
8
+ # Ruby Logger and child classes don't necessarilly expect message to be string.
9
+ # In theory, they should preserve the message value intact until it is passed to the formatter.
10
+ #
11
+ # ActiveSupport::TaggedLogging breaks this by injecting the tags via string concatenation,
12
+ # which coerces messages of any type into String.
13
+ # This patches works around this by instead appending the tags to the result of the
14
+ # original formatter result, which is garanteed to be a string
15
+
16
+ class GetaroundUtils::Patches::FixTaggedLoggingStringCoercion
17
+ module TaggedLoggingFormatter
18
+ def fixed_tags_text
19
+ " #{tags_text.strip}" if tags_text
20
+ end
21
+
22
+ def call(severity, datetime, appname, message)
23
+ original_method = method(__method__).super_method.super_method
24
+ payload = original_method.call(severity, datetime, appname, message)
25
+ "#{payload.chomp}#{fixed_tags_text}\n"
26
+ end
27
+ end
28
+
29
+ def self.enable
30
+ ActiveSupport::TaggedLogging::Formatter.prepend TaggedLoggingFormatter
31
+ end
32
+ end
@@ -1,9 +1,22 @@
1
1
  require 'rails/railtie'
2
- require 'getaround_utils/log_formatters/deep_key_value'
2
+ require 'getaround_utils/utils/deep_key_value_serializer'
3
3
 
4
4
  module GetaroundUtils; end
5
5
  module GetaroundUtils::Patches; end
6
6
 
7
+ ##
8
+ # Augment the ActiveRecord::TaggedLogging tag formatting
9
+ #
10
+ # Tags are defined either as String, Proc or a property of request
11
+ #
12
+ # Originally they will be formatted as `[tag_value]`
13
+ # This patch will instead attempt to serialize the tag with GetaroundUtils::DeepKeyValueSerializer
14
+ #
15
+ # ie:
16
+ # - for a String `value` it would yield `"value"`
17
+ # - for a Symbol `:request_id` it would yield `request_id="request_id_value"`
18
+ # - for a Proc `-> {key: :val}` it would yield `key="value"`
19
+
7
20
  class GetaroundUtils::Patches::KeyValueLogTags
8
21
  module TaggedLoggingFormatter
9
22
  def tags_text
@@ -12,16 +25,19 @@ class GetaroundUtils::Patches::KeyValueLogTags
12
25
  end
13
26
 
14
27
  module RackLogger
28
+ def kv_formatter
29
+ @kv_formatter ||= GetaroundUtils::Utils::DeepKeyValueSerializer.new
30
+ end
31
+
15
32
  def compute_tags(request)
16
- @kv_formatter ||= GetaroundUtils::LogFormatters::DeepKeyValue.new
17
33
  @taggers.collect do |tag|
18
34
  case tag
19
35
  when Proc
20
- @kv_formatter.serialize(tag.call(request))
36
+ kv_formatter.serialize(tag.call(request))
21
37
  when Symbol
22
- @kv_formatter.serialize(tag => request.send(tag))
38
+ kv_formatter.serialize(tag => request.send(tag))
23
39
  else
24
- @kv_formatter.serialize(tag => tag)
40
+ kv_formatter.serialize(tag)
25
41
  end
26
42
  end
27
43
  end
@@ -0,0 +1,4 @@
1
+ module GetaroundUtils::Patches
2
+ autoload :FixTaggedLoggingStringCoercion, 'getaround_utils/patches/fix_tagged_logging_string_coercion'
3
+ autoload :KeyValueLogTags, 'getaround_utils/patches/key_value_log_tags'
4
+ end
@@ -27,7 +27,5 @@ class GetaroundUtils::Railties::Lograge < Rails::Railtie
27
27
 
28
28
  config.lograge.enabled = true
29
29
  config.lograge.formatter = GetaroundUtils::LogFormatters::DeepKeyValue.for_lograge
30
- config.lograge.custom_options = lambda do |event|
31
- event.payload[:lograge].compact
32
- end
30
+ config.lograge.custom_options = ->(event) { event.payload[:lograge] }
33
31
  end
@@ -0,0 +1,40 @@
1
+ module GetaroundUtils; end
2
+ module GetaroundUtils::Utils; end
3
+
4
+ class GetaroundUtils::Utils::DeepKeyValueSerializer
5
+ def serialize(data)
6
+ case data
7
+ when Array
8
+ flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ')
9
+ when Hash
10
+ flattify(data).map { |key, value| "#{key}=#{value}" }.join(' ')
11
+ when Numeric
12
+ data.to_s
13
+ when String
14
+ data =~ /^".*"$/ ? data : data.inspect
15
+ else
16
+ data.to_s.inspect
17
+ end
18
+ end
19
+
20
+ # https://stackoverflow.com/questions/48836464/how-to-flatten-a-hash-making-each-key-a-unique-value
21
+ def flattify(value, result = {}, path = [])
22
+ case value
23
+ when Array
24
+ value.each.with_index(0) do |v, i|
25
+ flattify(v, result, path + [i])
26
+ end
27
+ when Hash
28
+ value.each do |k, v|
29
+ flattify(v, result, path + [k])
30
+ end
31
+ when Numeric
32
+ result[path.join(".")] = value.to_s
33
+ when String
34
+ result[path.join(".")] = value =~ /^".*"$/ ? value : value.inspect
35
+ else
36
+ result[path.join(".")] = value.to_s.inspect
37
+ end
38
+ result
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module GetaroundUtils::Utils
2
+ autoload :DeepKeyValueSerializer, 'getaround_utils/utils/deep_key_value_serializer'
3
+ end
@@ -1,3 +1,3 @@
1
1
  module GetaroundUtils
2
- VERSION = '0.1.6'.freeze
2
+ VERSION = '0.1.7'.freeze
3
3
  end
@@ -2,4 +2,7 @@ require 'getaround_utils/version'
2
2
 
3
3
  module GetaroundUtils
4
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'
5
8
  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.6
4
+ version: 0.1.7
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: 2019-11-08 00:00:00.000000000 Z
12
+ date: 2019-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -29,20 +29,20 @@ dependencies:
29
29
  name: getaround-rubocop
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
34
  version: 0.1.0
35
- - - "~>"
35
+ - - ">="
36
36
  - !ruby/object:Gem::Version
37
37
  version: 0.1.0
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  requirements:
42
- - - ">="
42
+ - - "~>"
43
43
  - !ruby/object:Gem::Version
44
44
  version: 0.1.0
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: 0.1.0
48
48
  - !ruby/object:Gem::Dependency
@@ -105,22 +105,22 @@ dependencies:
105
105
  name: rspec
106
106
  requirement: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: 3.9.0
111
108
  - - "~>"
112
109
  - !ruby/object:Gem::Version
113
110
  version: '3.9'
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 3.9.0
114
114
  type: :development
115
115
  prerelease: false
116
116
  version_requirements: !ruby/object:Gem::Requirement
117
117
  requirements:
118
- - - ">="
119
- - !ruby/object:Gem::Version
120
- version: 3.9.0
121
118
  - - "~>"
122
119
  - !ruby/object:Gem::Version
123
120
  version: '3.9'
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 3.9.0
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: rspec-rails
126
126
  requirement: !ruby/object:Gem::Requirement
@@ -139,42 +139,42 @@ dependencies:
139
139
  name: rubocop
140
140
  requirement: !ruby/object:Gem::Requirement
141
141
  requirements:
142
- - - ">="
143
- - !ruby/object:Gem::Version
144
- version: 0.75.0
145
142
  - - "~>"
146
143
  - !ruby/object:Gem::Version
147
144
  version: '0.75'
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: 0.75.0
148
148
  type: :development
149
149
  prerelease: false
150
150
  version_requirements: !ruby/object:Gem::Requirement
151
151
  requirements:
152
- - - ">="
153
- - !ruby/object:Gem::Version
154
- version: 0.75.0
155
152
  - - "~>"
156
153
  - !ruby/object:Gem::Version
157
154
  version: '0.75'
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: 0.75.0
158
158
  - !ruby/object:Gem::Dependency
159
159
  name: rubocop-rspec
160
160
  requirement: !ruby/object:Gem::Requirement
161
161
  requirements:
162
- - - ">="
163
- - !ruby/object:Gem::Version
164
- version: 0.36.0
165
162
  - - "~>"
166
163
  - !ruby/object:Gem::Version
167
164
  version: '1.36'
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: 0.36.0
168
168
  type: :development
169
169
  prerelease: false
170
170
  version_requirements: !ruby/object:Gem::Requirement
171
171
  requirements:
172
- - - ">="
173
- - !ruby/object:Gem::Version
174
- version: 0.36.0
175
172
  - - "~>"
176
173
  - !ruby/object:Gem::Version
177
174
  version: '1.36'
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: 0.36.0
178
178
  description: Shared base utility classes for Getaround Backend Applications.
179
179
  email:
180
180
  - oss@drivy.com
@@ -190,8 +190,14 @@ files:
190
190
  - lib/getaround_utils.rb
191
191
  - lib/getaround_utils/log_formatters.rb
192
192
  - lib/getaround_utils/log_formatters/deep_key_value.rb
193
+ - lib/getaround_utils/mixins.rb
194
+ - lib/getaround_utils/mixins/loggable.rb
195
+ - lib/getaround_utils/patches.rb
196
+ - lib/getaround_utils/patches/fix_tagged_logging_string_coercion.rb
193
197
  - lib/getaround_utils/patches/key_value_log_tags.rb
194
198
  - lib/getaround_utils/railties/lograge.rb
199
+ - lib/getaround_utils/utils.rb
200
+ - lib/getaround_utils/utils/deep_key_value_serializer.rb
195
201
  - lib/getaround_utils/version.rb
196
202
  homepage: https://github.com/drivy
197
203
  licenses:
@@ -212,7 +218,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
218
  - !ruby/object:Gem::Version
213
219
  version: '0'
214
220
  requirements: []
215
- rubygems_version: 3.0.3
221
+ rubyforge_project:
222
+ rubygems_version: 2.5.2.3
216
223
  signing_key:
217
224
  specification_version: 4
218
225
  summary: Backend shared utility classes