Commit 31091452 authored by Sean McGivern's avatar Sean McGivern

Use sentinel value for truncated param logging

Our Elasticsearch mapping on GitLab.com requires params to be an array
of hashes, each hash with a `key` and `value` key. This does not apply
to our Sidekiq arguments logging, as Sidekiq arguments can be of
different types anyway, and do not have keys.
parent c9003a71
......@@ -28,7 +28,7 @@ unless Gitlab::Runtime.sidekiq?
payload = {
time: Time.now.utc.iso8601(3),
params: Gitlab::Utils::LogLimitedArray.log_limited_array(params),
params: Gitlab::Utils::LogLimitedArray.log_limited_array(params, sentinel: { key: 'truncated', value: '...' }),
remote_ip: event.payload[:remote_ip],
user_id: event.payload[:user_id],
username: event.payload[:username],
......
......@@ -30,7 +30,8 @@ module Gitlab
.each_pair
.map { |k, v| { key: k, value: utf8_encode_values(v) } }
Gitlab::Utils::LogLimitedArray.log_limited_array(params_array)
Gitlab::Utils::LogLimitedArray.log_limited_array(params_array,
sentinel: { key: 'truncated', value: '...' })
end
def utf8_encode_values(data)
......
......@@ -6,9 +6,9 @@ module Gitlab
MAXIMUM_ARRAY_LENGTH = 10.kilobytes
# Prepare an array for logging by limiting its JSON representation
# to around 10 kilobytes. Once we hit the limit, add "..." as the
# last item in the returned array.
def self.log_limited_array(array)
# to around 10 kilobytes. Once we hit the limit, add the sentinel
# value as the last item in the returned array.
def self.log_limited_array(array, sentinel: '...')
return [] unless array.is_a?(Array)
total_length = 0
......@@ -18,7 +18,7 @@ module Gitlab
total_length <= MAXIMUM_ARRAY_LENGTH
end
limited_array.push('...') if total_length > MAXIMUM_ARRAY_LENGTH
limited_array.push(sentinel) if total_length > MAXIMUM_ARRAY_LENGTH
limited_array
end
......
......@@ -17,7 +17,7 @@ describe 'lograge', type: :request do
end
let(:limited_params) do
large_params.slice(:a, :b).map { |k, v| { key: k.to_s, value: v } } + ['...']
large_params.slice(:a, :b).map { |k, v| { key: k.to_s, value: v } } + [{ key: 'truncated', value: '...' }]
end
context 'for API requests' do
......
......@@ -18,15 +18,29 @@ describe Gitlab::Utils::LogLimitedArray do
end
context 'when the array exceeds the limit' do
it 'replaces arguments after the limit with an ellipsis string' do
let(:long_array) do
half_limit = described_class::MAXIMUM_ARRAY_LENGTH / 2
long_array = ['a' * half_limit, 'b' * half_limit, 'c']
['a' * half_limit, 'b' * half_limit, 'c']
end
context 'when no sentinel value is passed' do
it 'replaces arguments after the limit with an ellipsis string' do
expect(described_class.log_limited_array(long_array))
.to eq(long_array.take(1) + ['...'])
end
end
context 'when a sentinel value is passed' do
it 'replaces arguments after the limit with the sentinel' do
sentinel = { truncated: true }
expect(described_class.log_limited_array(long_array, sentinel: sentinel))
.to eq(long_array.take(1) + [sentinel])
end
end
end
context 'when the array contains arrays and hashes' do
it 'calculates the size based on the JSON representation' do
long_array = [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment