Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stringify args #20

Merged
merged 1 commit into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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