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? ...@@ -28,7 +28,7 @@ unless Gitlab::Runtime.sidekiq?
payload = { payload = {
time: Time.now.utc.iso8601(3), 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], remote_ip: event.payload[:remote_ip],
user_id: event.payload[:user_id], user_id: event.payload[:user_id],
username: event.payload[:username], username: event.payload[:username],
......
...@@ -30,7 +30,8 @@ module Gitlab ...@@ -30,7 +30,8 @@ module Gitlab
.each_pair .each_pair
.map { |k, v| { key: k, value: utf8_encode_values(v) } } .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 end
def utf8_encode_values(data) def utf8_encode_values(data)
......
...@@ -6,9 +6,9 @@ module Gitlab ...@@ -6,9 +6,9 @@ module Gitlab
MAXIMUM_ARRAY_LENGTH = 10.kilobytes MAXIMUM_ARRAY_LENGTH = 10.kilobytes
# Prepare an array for logging by limiting its JSON representation # Prepare an array for logging by limiting its JSON representation
# to around 10 kilobytes. Once we hit the limit, add "..." as the # to around 10 kilobytes. Once we hit the limit, add the sentinel
# last item in the returned array. # value as the last item in the returned array.
def self.log_limited_array(array) def self.log_limited_array(array, sentinel: '...')
return [] unless array.is_a?(Array) return [] unless array.is_a?(Array)
total_length = 0 total_length = 0
...@@ -18,7 +18,7 @@ module Gitlab ...@@ -18,7 +18,7 @@ module Gitlab
total_length <= MAXIMUM_ARRAY_LENGTH total_length <= MAXIMUM_ARRAY_LENGTH
end end
limited_array.push('...') if total_length > MAXIMUM_ARRAY_LENGTH limited_array.push(sentinel) if total_length > MAXIMUM_ARRAY_LENGTH
limited_array limited_array
end end
......
...@@ -17,7 +17,7 @@ describe 'lograge', type: :request do ...@@ -17,7 +17,7 @@ describe 'lograge', type: :request do
end end
let(:limited_params) do 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 end
context 'for API requests' do context 'for API requests' do
......
...@@ -18,15 +18,29 @@ describe Gitlab::Utils::LogLimitedArray do ...@@ -18,15 +18,29 @@ describe Gitlab::Utils::LogLimitedArray do
end end
context 'when the array exceeds the limit' do 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 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)) expect(described_class.log_limited_array(long_array))
.to eq(long_array.take(1) + ['...']) .to eq(long_array.take(1) + ['...'])
end end
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 context 'when the array contains arrays and hashes' do
it 'calculates the size based on the JSON representation' do it 'calculates the size based on the JSON representation' do
long_array = [ 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