lenjador 2.0.0 → 2.2.1

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: 33c13138df41134b3d17319ff6d02d86e5b261ab5b45eaf901ec35e19bb0667b
4
- data.tar.gz: 963d753a7188d8ce556b2f03236db1a18e9428e8d5f71d07890e039ea6dcdf51
3
+ metadata.gz: 9d2c05e9a5e41ddcfb2274063ae0b5d75387a5657e9effb6440e1d0ac9aa10e2
4
+ data.tar.gz: f5e80262d179b4320f7db1e2f1998b9a6160c57868a14dbf3d74d73420289e2a
5
5
  SHA512:
6
- metadata.gz: 77ea852dc496c9c973954da2d83de33811a6c7e37b7028ca1596aece01009944e0d476f5e8e32790c380735338283d5ee62e634f0555da7aa58b8a99a73bdb91
7
- data.tar.gz: 367a1528be10b7d2cde8f9d8cbe16080ebdf90a9d9e6c0670b42ced776018c1d1871b904434f1b6d11e18eeebbd993365573e5ec8acb21f8cb6d04eb3563f585
6
+ metadata.gz: a051c066866a827ca68e7b207a5f37f73ce7eb73bc1a33afca8523f2b51d2c44f47bd3d7d87d07ac2e7498f03298ac5120d314718c906043cf17b5ed913c6711
7
+ data.tar.gz: 212cc99147c9d533b82fb92594259ebf65eb49e9cf61e006d8460b874dec01e2cba6b4f15e898e3b199186c569fbcb488923e26ba05dc76a925292a9d1501f0f
@@ -0,0 +1,25 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby-version: ['2.6', '2.7']
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ with:
18
+ submodules: true
19
+ - name: Set up Ruby
20
+ uses: ruby/setup-ruby@v1
21
+ with:
22
+ ruby-version: ${{ matrix.ruby-version }}
23
+ bundler-cache: true
24
+ - name: Run lint & tests
25
+ run: bundle exec rake
data/.gitignore CHANGED
@@ -1,4 +1,2 @@
1
1
  node_modules/
2
2
  npm-debug.log
3
- Gemfile.lock
4
-
data/.rubocop.yml CHANGED
@@ -1,6 +1,9 @@
1
1
  inherit_gem:
2
2
  rubocop-salemove: config/default.yml
3
3
 
4
+ AllCops:
5
+ TargetRubyVersion: 2.6
6
+
4
7
  Style/FrozenStringLiteralComment:
5
8
  Enabled: yes
6
9
  EnforcedStyle: always
@@ -34,8 +37,5 @@ RSpec/MultipleExpectations:
34
37
  Style/SafeNavigation:
35
38
  Enabled: no
36
39
 
37
- Performance/RegexpMatch:
38
- Enabled: no
39
-
40
40
  Style/SingleLineMethods:
41
41
  Enabled: no
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.4.1
1
+ ruby-2.7.1
data/Gemfile.lock ADDED
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lenjador (2.2.1)
5
+ lru_redux
6
+ oj (~> 3.6)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ benchmark-ips (2.8.4)
13
+ coderay (1.1.3)
14
+ diff-lcs (1.4.4)
15
+ lru_redux (1.1.0)
16
+ method_source (1.0.0)
17
+ oj (3.11.5)
18
+ opentelemetry-api (0.17.0)
19
+ parallel (1.20.1)
20
+ parser (3.0.1.1)
21
+ ast (~> 2.4.1)
22
+ pry (0.14.1)
23
+ coderay (~> 1.1)
24
+ method_source (~> 1.0)
25
+ rainbow (3.0.0)
26
+ rake (13.0.3)
27
+ regexp_parser (2.1.1)
28
+ rexml (3.2.5)
29
+ rspec (3.10.0)
30
+ rspec-core (~> 3.10.0)
31
+ rspec-expectations (~> 3.10.0)
32
+ rspec-mocks (~> 3.10.0)
33
+ rspec-core (3.10.1)
34
+ rspec-support (~> 3.10.0)
35
+ rspec-expectations (3.10.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.10.0)
38
+ rspec-mocks (3.10.2)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.10.0)
41
+ rspec-support (3.10.2)
42
+ rubocop (0.93.1)
43
+ parallel (~> 1.10)
44
+ parser (>= 2.7.1.5)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.8)
47
+ rexml
48
+ rubocop-ast (>= 0.6.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 2.0)
51
+ rubocop-ast (1.5.0)
52
+ parser (>= 3.0.1.1)
53
+ rubocop-rspec (1.44.1)
54
+ rubocop (~> 0.87)
55
+ rubocop-ast (>= 0.7.1)
56
+ rubocop-salemove (0.0.5)
57
+ rubocop (~> 0.82)
58
+ rubocop-rspec (~> 1.15)
59
+ ruby-prof (1.4.3)
60
+ ruby-progressbar (1.11.0)
61
+ unicode-display_width (1.7.0)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ benchmark-ips
68
+ bundler
69
+ lenjador!
70
+ opentelemetry-api
71
+ pry
72
+ rake
73
+ rspec
74
+ rubocop-salemove
75
+ ruby-prof
76
+
77
+ BUNDLED WITH
78
+ 2.1.4
data/lenjador.gemspec CHANGED
@@ -4,13 +4,14 @@ lib = File.expand_path('lib', __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.name = RUBY_PLATFORM =~ /java/ ? 'lenjador-jruby' : 'lenjador'
8
- gem.version = '2.0.0'
7
+ gem.name = 'lenjador'
8
+ gem.version = '2.2.1'
9
9
  gem.authors = ['Salemove']
10
10
  gem.email = ['support@salemove.com']
11
11
  gem.description = "It's lenjadoric"
12
12
  gem.summary = 'What description said'
13
13
  gem.license = 'MIT'
14
+ gem.required_ruby_version = '>= 2.6'
14
15
 
15
16
  gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
16
17
  gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
@@ -18,18 +19,14 @@ Gem::Specification.new do |gem|
18
19
  gem.require_paths = ['lib']
19
20
 
20
21
  gem.add_dependency 'lru_redux'
21
-
22
- if RUBY_PLATFORM =~ /java/
23
- gem.add_dependency 'jrjackson'
24
- else
25
- gem.add_dependency 'oj', '~> 3.6'
26
- gem.add_development_dependency 'ruby-prof'
27
- end
22
+ gem.add_dependency 'oj', '~> 3.6'
28
23
 
29
24
  gem.add_development_dependency 'benchmark-ips'
30
25
  gem.add_development_dependency 'bundler'
26
+ gem.add_development_dependency 'opentelemetry-api'
31
27
  gem.add_development_dependency 'pry'
32
28
  gem.add_development_dependency 'rake'
33
29
  gem.add_development_dependency 'rspec'
34
30
  gem.add_development_dependency 'rubocop-salemove'
31
+ gem.add_development_dependency 'ruby-prof'
35
32
  end
@@ -8,7 +8,7 @@ class Lenjador
8
8
  attr_reader :logger
9
9
 
10
10
  def initialize(_service_name)
11
- @logger = Logger.new(STDOUT)
11
+ @logger = Logger.new($stdout)
12
12
  end
13
13
 
14
14
  def log(level, metadata = {})
@@ -5,7 +5,6 @@ class Lenjador
5
5
  class StdoutJsonAdapter
6
6
  def initialize(service_name)
7
7
  @application_name = Utils.application_name(service_name)
8
- @mutex = Mutex.new if RUBY_ENGINE == 'jruby'
9
8
  end
10
9
 
11
10
  def log(level, metadata = {})
@@ -20,13 +19,9 @@ class Lenjador
20
19
  def print_line(str)
21
20
  $stdout.puts(str)
22
21
  end
23
- elsif RUBY_ENGINE == 'jruby'
24
- def print_line(str)
25
- @mutex.synchronize { $stdout.write(str + "\n") }
26
- end
27
22
  else
28
23
  def print_line(str)
29
- $stdout.write(str + "\n")
24
+ $stdout.write("#{str}\n")
30
25
  end
31
26
  end
32
27
  end
@@ -20,7 +20,8 @@ class Lenjador
20
20
  end
21
21
 
22
22
  def process(data)
23
- if data.is_a? Hash
23
+ case data
24
+ when Hash
24
25
  data.inject({}) do |mem, (key, val)|
25
26
  if (field = @fields_to_process[key.to_s])
26
27
  send(action_method(field[:action]), mem, key, val)
@@ -28,7 +29,7 @@ class Lenjador
28
29
  mem.merge(key => process(val))
29
30
  end
30
31
  end
31
- elsif data.is_a? Array
32
+ when Array
32
33
  data.inject([]) do |mem, val|
33
34
  mem + [process(val)]
34
35
  end
@@ -7,7 +7,7 @@ require 'lenjador/preprocessors/strategies/prune'
7
7
  class Lenjador
8
8
  module Preprocessors
9
9
  class Whitelist
10
- DEFAULT_WHITELIST = %w[/id /message /correlation_id /queue].freeze
10
+ DEFAULT_WHITELIST = %w[/id /message /correlation_id /queue /trace_id /span_id].freeze
11
11
  MASK_SYMBOL = '*'
12
12
  MASKED_VALUE = MASK_SYMBOL * 5
13
13
 
@@ -45,7 +45,7 @@ class Lenjador
45
45
  def build_trie(config)
46
46
  pointers = (config[:pointers] || []) + DEFAULT_WHITELIST
47
47
 
48
- pointers.reduce(JSONPointerTrie.new(config)) do |trie, pointer|
48
+ pointers.reduce(JSONPointerTrie.new(**config)) do |trie, pointer|
49
49
  validate_pointer(pointer)
50
50
 
51
51
  trie.insert(decode(pointer))
@@ -1,11 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'time'
4
+ require 'oj'
4
5
 
5
6
  class Lenjador
6
7
  module Utils
7
8
  DECIMAL_FRACTION_OF_SECOND = 3
8
9
  NO_TRACE_INFORMATION = {}.freeze
10
+ DUMP_OPTIONS = {
11
+ mode: :custom,
12
+ time_format: :xmlschema,
13
+ second_precision: 3
14
+ }.freeze
9
15
 
10
16
  # Build logstash json compatible event
11
17
  #
@@ -43,28 +49,8 @@ class Lenjador
43
49
  end
44
50
  private_class_method :overwritable_params
45
51
 
46
- if RUBY_PLATFORM =~ /java/
47
- require 'jrjackson'
48
-
49
- DUMP_OPTIONS = {
50
- timezone: 'utc',
51
- date_format: "YYYY-MM-dd'T'HH:mm:ss.SSSX"
52
- }.freeze
53
-
54
- def self.generate_json(obj)
55
- JrJackson::Json.dump(obj, DUMP_OPTIONS)
56
- end
57
- else
58
- require 'oj'
59
- DUMP_OPTIONS = {
60
- mode: :custom,
61
- time_format: :xmlschema,
62
- second_precision: 3
63
- }.freeze
64
-
65
- def self.generate_json(obj)
66
- Oj.dump(obj, DUMP_OPTIONS)
67
- end
52
+ def self.generate_json(obj)
53
+ Oj.dump(obj, DUMP_OPTIONS)
68
54
  end
69
55
 
70
56
  def self.underscore(input)
@@ -77,17 +63,37 @@ class Lenjador
77
63
  word
78
64
  end
79
65
 
80
- # Tracing information
81
- #
82
- # Tracing information is included only if OpenTracing is defined and if it
83
- # supports method called `active_span` (version >= 0.4.1). We use
84
- # SpanContext#trace_id and SpanContext#span_id methods to retrieve tracing
85
- # information. These methods are not yet supported by the OpenTracing API,
86
- # so we first check if these methods exist. Popular tracing libraries
87
- # already implement them. These methods are likely to be added to the API
88
- # very soon: https://github.com/opentracing/specification/blob/master/rfc/trace_identifiers.md
89
66
  def self.tracing_information
90
- return NO_TRACE_INFORMATION if !defined?(OpenTracing) || !OpenTracing.respond_to?(:active_span)
67
+ tracing_information_from_opentelemetry ||
68
+ tracing_information_from_opentracing ||
69
+ NO_TRACE_INFORMATION
70
+ end
71
+ private_class_method :tracing_information
72
+
73
+ def self.tracing_information_from_opentelemetry
74
+ return nil unless defined?(OpenTelemetry)
75
+
76
+ current_span_context = OpenTelemetry::Trace.current_span.context
77
+ return nil unless current_span_context.valid?
78
+
79
+ {
80
+ trace_id: current_span_context.hex_trace_id,
81
+ span_id: current_span_context.hex_span_id
82
+ }
83
+ end
84
+ private_class_method :tracing_information_from_opentelemetry
85
+
86
+ # Tracing information is included only if OpenTracing supports method
87
+ # called `active_span` (version >= 0.4.1). We use SpanContext#trace_id and
88
+ # SpanContext#span_id methods to retrieve tracing information. These
89
+ # methods are not yet supported by the OpenTracing API, so we first check
90
+ # if these methods exist. Popular tracing libraries already implement them.
91
+ # These methods are likely to be added to the API very soon:
92
+ # https://github.com/opentracing/specification/blob/master/rfc/trace_identifiers.md
93
+ #
94
+ # @deprecated Use OpenTelemetry instead
95
+ def self.tracing_information_from_opentracing
96
+ return nil if !defined?(OpenTracing) || !OpenTracing.respond_to?(:active_span)
91
97
 
92
98
  context = OpenTracing.active_span&.context
93
99
  if context && context.respond_to?(:trace_id) && context.respond_to?(:span_id)
@@ -95,10 +101,8 @@ class Lenjador
95
101
  trace_id: context.trace_id,
96
102
  span_id: context.span_id
97
103
  }
98
- else
99
- NO_TRACE_INFORMATION
100
104
  end
101
105
  end
102
- private_class_method :tracing_information
106
+ private_class_method :tracing_information_from_opentracing
103
107
  end
104
108
  end
data/lib/lenjador.rb CHANGED
@@ -58,6 +58,12 @@ class Lenjador
58
58
 
59
59
  def fatal?; @level <= Severity::FATAL; end
60
60
 
61
+ def level=(new_level)
62
+ raise ArgumentError, "invalid log level: #{new_level}" unless new_level.is_a?(Integer)
63
+
64
+ @level = new_level
65
+ end
66
+
61
67
  private
62
68
 
63
69
  def log(level, *args, &block)
@@ -16,7 +16,7 @@ describe Lenjador::Adapters::StdoutJsonAdapter do
16
16
  end
17
17
  end
18
18
 
19
- describe '#log' do
19
+ describe '#log' do # rubocop:disable RSpec/MultipleMemoizedHelpers
20
20
  let(:adapter) { described_class.new(service_name) }
21
21
  let(:metadata) { {x: 'y'} }
22
22
  let(:event) { {a: 'b', x: 'y'} }
@@ -34,7 +34,7 @@ describe Lenjador::Adapters::StdoutJsonAdapter do
34
34
 
35
35
  it 'sends serialized event to $stdout' do
36
36
  adapter.log(info, metadata)
37
- expect(output).to eq serialized_event + "\n"
37
+ expect(output).to eq("#{serialized_event}\n")
38
38
  end
39
39
  end
40
40
 
@@ -146,14 +146,6 @@ RSpec.describe Lenjador::Preprocessors::Whitelist do
146
146
  end
147
147
  end
148
148
 
149
- context 'with whitelisted hash' do
150
- let(:pointers) { ['/data'] }
151
-
152
- it 'masks hash' do
153
- expect(processed_data).to include(data: {field: '*****'})
154
- end
155
- end
156
-
157
149
  context 'when boolean present' do
158
150
  let(:data) { {bool: true} }
159
151
 
@@ -320,14 +312,6 @@ RSpec.describe Lenjador::Preprocessors::Whitelist do
320
312
  end
321
313
  end
322
314
 
323
- context 'with whitelisted hash' do
324
- let(:pointers) { ['/data'] }
325
-
326
- it 'masks hash' do
327
- expect(processed_data).to include(data: {})
328
- end
329
- end
330
-
331
315
  def process(pointers, data)
332
316
  described_class.new(pointers: pointers, action: :prune).process(data)
333
317
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
+ require 'opentelemetry-api'
4
5
 
5
6
  describe Lenjador::Utils do
6
7
  let(:now) { Time.utc(2015, 10, 11, 23, 10, 21, 123_456) }
@@ -46,7 +47,28 @@ describe Lenjador::Utils do
46
47
  end
47
48
  end
48
49
 
49
- context 'when OpenTracing is defined' do
50
+ context 'when OpenTelemetry is defined' do
51
+ it 'includes trace information' do
52
+ provider = OpenTelemetry.tracer_provider
53
+
54
+ tracer = provider.tracer('my_app', '1.0')
55
+ recorded_event = tracer.in_span('my_task') do |_task_span|
56
+ event
57
+ end
58
+
59
+ expect(recorded_event).to include(
60
+ trace_id: instance_of(String),
61
+ span_id: instance_of(String)
62
+ )
63
+ end
64
+
65
+ it 'does not include trace information if span is invalid' do
66
+ expect(OpenTelemetry::Trace.current_span.context).not_to be_valid
67
+ expect(event).not_to include(:trace_id, :span_id)
68
+ end
69
+ end
70
+
71
+ context 'when OpenTracing is defined' do # rubocop:disable RSpec/MultipleMemoizedHelpers
50
72
  let(:trace_id) { 'trace-id' }
51
73
  let(:span_id) { 'span-id' }
52
74
 
@@ -128,4 +128,23 @@ describe Lenjador do
128
128
  end
129
129
  end
130
130
  end
131
+
132
+ it 'allows changing log level on existing instance' do
133
+ logger = described_class.build('test_service', level: 'info')
134
+
135
+ logger.level = ::Logger::DEBUG
136
+ expect(logger.debug?).to eq(true)
137
+
138
+ logger.level = ::Logger::INFO
139
+ expect(logger.debug?).to eq(false)
140
+ expect(logger.info?).to eq(true)
141
+
142
+ logger.level = ::Logger::WARN
143
+ expect(logger.info?).to eq(false)
144
+ expect(logger.warn?).to eq(true)
145
+
146
+ logger.level = ::Logger::ERROR
147
+ expect(logger.warn?).to eq(false)
148
+ expect(logger.error?).to eq(true)
149
+ end
131
150
  end
data/spec/spec_helper.rb CHANGED
@@ -5,7 +5,7 @@ Bundler.setup
5
5
 
6
6
  require 'lenjador'
7
7
 
8
- Dir[File.dirname(__FILE__) + '/support/*.rb'].each { |f| require f }
8
+ Dir["#{File.dirname(__FILE__)}/support/*.rb"].sort.each { |f| require f }
9
9
 
10
10
  RSpec.configure do |config|
11
11
  config.run_all_when_everything_filtered = true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lenjador
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salemove
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-12 00:00:00.000000000 Z
11
+ date: 2021-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lru_redux
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.6'
41
41
  - !ruby/object:Gem::Dependency
42
- name: ruby-prof
42
+ name: benchmark-ips
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: benchmark-ips
56
+ name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: bundler
70
+ name: opentelemetry-api
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: ruby-prof
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  description: It's lenjadoric
140
154
  email:
141
155
  - support@salemove.com
@@ -143,13 +157,14 @@ executables: []
143
157
  extensions: []
144
158
  extra_rdoc_files: []
145
159
  files:
160
+ - ".github/workflows/ci.yml"
146
161
  - ".gitignore"
147
162
  - ".rspec"
148
163
  - ".rubocop.yml"
149
164
  - ".ruby-gemset"
150
165
  - ".ruby-version"
151
- - ".travis.yml"
152
166
  - Gemfile
167
+ - Gemfile.lock
153
168
  - README.md
154
169
  - Rakefile
155
170
  - benchmark/whitelisting.rb
@@ -188,15 +203,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
188
203
  requirements:
189
204
  - - ">="
190
205
  - !ruby/object:Gem::Version
191
- version: '0'
206
+ version: '2.6'
192
207
  required_rubygems_version: !ruby/object:Gem::Requirement
193
208
  requirements:
194
209
  - - ">="
195
210
  - !ruby/object:Gem::Version
196
211
  version: '0'
197
212
  requirements: []
198
- rubyforge_project:
199
- rubygems_version: 2.7.6
213
+ rubygems_version: 3.1.2
200
214
  signing_key:
201
215
  specification_version: 4
202
216
  summary: What description said
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.4
4
- - 2.5
5
- - jruby