2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
# This is needed for sidekiq-cluster
|
|
|
|
require 'json'
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
module Gitlab
|
|
|
|
module SidekiqLogging
|
|
|
|
class JSONFormatter
|
2021-12-11 22:18:48 +05:30
|
|
|
TIMESTAMP_FIELDS = %w[created_at scheduled_at enqueued_at started_at retried_at failed_at completed_at].freeze
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def call(severity, timestamp, progname, data)
|
|
|
|
output = {
|
|
|
|
severity: severity,
|
|
|
|
time: timestamp.utc.iso8601(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
case data
|
|
|
|
when String
|
|
|
|
output[:message] = data
|
|
|
|
when Hash
|
|
|
|
output.merge!(data)
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
# jobstr is redundant and can include information we wanted to
|
|
|
|
# exclude (like arguments)
|
|
|
|
output.delete(:jobstr)
|
|
|
|
|
|
|
|
convert_to_iso8601!(output)
|
|
|
|
convert_retry_to_integer!(output)
|
|
|
|
process_args!(output)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
output.to_json + "\n"
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
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-22 19:07:51 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
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-06-23 00:09:42 +05:30
|
|
|
def process_args!(payload)
|
|
|
|
return unless payload['args']
|
|
|
|
|
|
|
|
payload['args'] = Gitlab::ErrorTracking::Processor::SidekiqProcessor
|
|
|
|
.loggable_arguments(payload['args'], payload['class'])
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|