Module: Gitlab::Utils::LogLimitedArray

Defined in:
lib/gitlab/utils/log_limited_array.rb

Constant Summary collapse

MAXIMUM_ARRAY_LENGTH =
10.kilobytes

Class Method Summary collapse

Class Method Details

.log_limited_array(array, sentinel: '...') ⇒ Object

Prepare an array for logging by limiting its JSON representation to around 10 kilobytes. Once we hit the limit, add the sentinel value as the last item in the returned array.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gitlab/utils/log_limited_array.rb', line 11

def self.log_limited_array(array, sentinel: '...')
  return [] unless array.is_a?(Array) || array.is_a?(Enumerator::Lazy)

  total_length = 0
  limited_array = array.take_while do |arg|
    total_length += JsonSizeEstimator.estimate(arg)

    total_length <= MAXIMUM_ARRAY_LENGTH
  end.to_a

  limited_array.push(sentinel) if total_length > MAXIMUM_ARRAY_LENGTH

  limited_array
end