2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-03-04 16:07:54 -05:00
|
|
|
# This is needed for sidekiq-cluster
|
|
|
|
require 'json'
|
|
|
|
|
2018-04-01 03:46:52 -04:00
|
|
|
module Gitlab
|
|
|
|
module SidekiqLogging
|
|
|
|
class JSONFormatter
|
2020-01-10 19:08:28 -05:00
|
|
|
TIMESTAMP_FIELDS = %w[created_at enqueued_at started_at retried_at failed_at completed_at].freeze
|
|
|
|
|
2018-04-01 03:46:52 -04:00
|
|
|
def call(severity, timestamp, progname, data)
|
|
|
|
output = {
|
|
|
|
severity: severity,
|
|
|
|
time: timestamp.utc.iso8601(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
case data
|
|
|
|
when String
|
|
|
|
output[:message] = data
|
|
|
|
when Hash
|
2020-01-10 19:08:28 -05:00
|
|
|
convert_to_iso8601!(data)
|
2020-05-07 17:09:26 -04:00
|
|
|
convert_retry_to_integer!(data)
|
2020-04-07 05:09:13 -04:00
|
|
|
stringify_args!(data)
|
2018-04-01 03:46:52 -04:00
|
|
|
output.merge!(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
output.to_json + "\n"
|
|
|
|
end
|
2020-01-10 19:08:28 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def convert_to_iso8601!(payload)
|
|
|
|
TIMESTAMP_FIELDS.each do |key|
|
|
|
|
value = payload[key]
|
|
|
|
payload[key] = format_time(value) if value.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_time(timestamp)
|
|
|
|
return timestamp unless timestamp.is_a?(Numeric)
|
|
|
|
|
|
|
|
Time.at(timestamp).utc.iso8601(3)
|
|
|
|
end
|
2020-04-07 05:09:13 -04:00
|
|
|
|
2020-05-07 17:09:26 -04:00
|
|
|
def convert_retry_to_integer!(payload)
|
|
|
|
payload['retry'] =
|
|
|
|
case payload['retry']
|
|
|
|
when Integer
|
|
|
|
payload['retry']
|
|
|
|
when false, nil
|
|
|
|
0
|
|
|
|
when true
|
|
|
|
Sidekiq::JobRetry::DEFAULT_MAX_RETRY_ATTEMPTS
|
|
|
|
else
|
|
|
|
-1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 05:09:13 -04:00
|
|
|
def stringify_args!(payload)
|
|
|
|
payload['args'] = Gitlab::Utils::LogLimitedArray.log_limited_array(payload['args'].map(&:to_s)) if payload['args']
|
|
|
|
end
|
2018-04-01 03:46:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|