From 906dcc7cce34fe844c7d1a2f6b16daa8bf5d61f8 Mon Sep 17 00:00:00 2001 From: Mattia Giuffrida Date: Tue, 25 Aug 2020 10:13:56 +0100 Subject: [PATCH] Fixes #3, Supersedes #18 --- lib/sidekiq/logging/shared.rb | 15 +++++++++++++-- spec/sidekiq/logstash_spec.rb | 8 +++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/sidekiq/logging/shared.rb b/lib/sidekiq/logging/shared.rb index e089cd5..7f2d5bf 100644 --- a/lib/sidekiq/logging/shared.rb +++ b/lib/sidekiq/logging/shared.rb @@ -88,11 +88,11 @@ def process_payload(payload) payload['args'][-1] = ENCRYPTED if payload['encrypt'] # Needs to map all args to strings for ElasticSearch compatibility - payload['args'].map!(&:to_s) + deep_stringify!(payload['args']) # Needs to map all unique_args to strings for ElasticSearch # compatibility in case sidekiq-unique-jobs is used - payload['unique_args']&.map!(&:to_s) + deep_stringify!(payload['unique_args']) if payload['retry'].is_a?(Integer) payload['max_retries'] = payload['retry'] @@ -124,6 +124,17 @@ def parse_time(timestamp) def filter_args Sidekiq::Logstash.configuration.filter_args end + + def deep_stringify!(args) + case args + when Hash + Hash[args.map { |key, value| [deep_stringify!(key), deep_stringify!(value)] }] + when Array + args.map! { |val| deep_stringify!(val) } + else + args.to_s + end + end end end end diff --git a/spec/sidekiq/logstash_spec.rb b/spec/sidekiq/logstash_spec.rb index 115c691..d4e613b 100644 --- a/spec/sidekiq/logstash_spec.rb +++ b/spec/sidekiq/logstash_spec.rb @@ -45,6 +45,12 @@ def process(worker, params = [], encrypt: false) expect(buffer.string).not_to include('started') end + it 'stringifies parameters' do + hash = { a_really: { deep: { hash: [1, 2] } } } + process(SpecWorker, [false, hash]) + expect(log_message['args'].last).to eq({ 'a_really' => { 'deep' => { 'hash' => %w[1 2] } } }) + end + context 'with arguments filtered' do before do Sidekiq::Logstash.configure do |config| @@ -60,7 +66,7 @@ def process(worker, params = [], encrypt: false) it 'filter args' do process(SpecWorker, [false, { 'a_secret_param' => 'secret' }]) - expect(log_message['args'].last).to include('[FILTERED]') + expect(log_message['args'].last['a_secret_param']).to eq('[FILTERED]') end end