summaryrefslogtreecommitdiff
path: root/lib/gitlab/utils
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-15 00:08:48 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-15 00:08:48 +0000
commitb69f406585ff64b1c5140ebba775cc754fabb358 (patch)
tree9af7dfeb0c3f0f8db189a6e18c6be398a7729e2d /lib/gitlab/utils
parent866ca4e49ff74ffadf8e6f6ff663a168489c2aba (diff)
downloadgitlab-ce-b69f406585ff64b1c5140ebba775cc754fabb358.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/utils')
-rw-r--r--lib/gitlab/utils/log_limited_array.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/gitlab/utils/log_limited_array.rb b/lib/gitlab/utils/log_limited_array.rb
new file mode 100644
index 00000000000..fe8aadf9020
--- /dev/null
+++ b/lib/gitlab/utils/log_limited_array.rb
@@ -0,0 +1,27 @@
+# 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
+ # to around 10 kilobytes. Once we hit the limit, add "..." as the
+ # last item in the returned array.
+ def self.log_limited_array(array)
+ return [] unless array.is_a?(Array)
+
+ total_length = 0
+ limited_array = array.take_while do |arg|
+ total_length += arg.to_json.length
+
+ total_length <= MAXIMUM_ARRAY_LENGTH
+ end
+
+ limited_array.push('...') if total_length > MAXIMUM_ARRAY_LENGTH
+
+ limited_array
+ end
+ end
+ end
+end