debian-mirror-gitlab/lib/gitlab/utils/log_limited_array.rb

28 lines
752 B
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Gitlab
module Utils
module LogLimitedArray
MAXIMUM_ARRAY_LENGTH = 10.kilobytes
# Prepare an array for logging by limiting its JSON representation
2020-04-08 14:13:33 +05:30
# 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: '...')
2020-03-13 15:44:24 +05:30
return [] unless array.is_a?(Array)
total_length = 0
limited_array = array.take_while do |arg|
2020-04-08 14:13:33 +05:30
total_length += JsonSizeEstimator.estimate(arg)
2020-03-13 15:44:24 +05:30
total_length <= MAXIMUM_ARRAY_LENGTH
end
2020-04-08 14:13:33 +05:30
limited_array.push(sentinel) if total_length > MAXIMUM_ARRAY_LENGTH
2020-03-13 15:44:24 +05:30
limited_array
end
end
end
end