Skip to content

Commit

Permalink
Fix stringily args (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMacTia authored Aug 25, 2020
1 parent 475517f commit ce5e099
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/sidekiq/logging/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
8 changes: 7 additions & 1 deletion spec/sidekiq/logstash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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

Expand Down

0 comments on commit ce5e099

Please sign in to comment.