debian-mirror-gitlab/lib/gitlab/sidekiq_logging/structured_logger.rb

110 lines
3.4 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2020-03-13 15:44:24 +05:30
require 'active_record'
require 'active_record/log_subscriber'
2018-05-09 12:01:36 +05:30
module Gitlab
module SidekiqLogging
class StructuredLogger
2020-04-08 14:13:33 +05:30
include LogsJobs
2018-05-09 12:01:36 +05:30
def call(job, queue)
2019-12-04 20:38:33 +05:30
started_time = get_time
2018-05-09 12:01:36 +05:30
base_payload = parse_job(job)
2020-03-13 15:44:24 +05:30
ActiveRecord::LogSubscriber.reset_runtime
2018-05-09 12:01:36 +05:30
2019-12-04 20:38:33 +05:30
Sidekiq.logger.info log_job_start(base_payload)
2018-05-09 12:01:36 +05:30
yield
2019-12-04 20:38:33 +05:30
Sidekiq.logger.info log_job_done(job, started_time, base_payload)
2018-05-09 12:01:36 +05:30
rescue => job_exception
2019-12-04 20:38:33 +05:30
Sidekiq.logger.warn log_job_done(job, started_time, base_payload, job_exception)
2018-05-09 12:01:36 +05:30
raise
end
private
2019-10-12 21:52:04 +05:30
def add_instrumentation_keys!(job, output_payload)
2020-06-23 00:09:42 +05:30
output_payload.merge!(job.slice(*::Gitlab::InstrumentationHelper.keys))
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
def add_logging_extras!(job, output_payload)
output_payload.merge!(
job.select { |key, _| key.to_s.start_with?("#{ApplicationWorker::LOGGING_EXTRA_KEY}.") }
)
end
2020-06-23 00:09:42 +05:30
def add_db_counters!(job, output_payload)
output_payload.merge!(job.slice(*::Gitlab::Metrics::Subscribers::ActiveRecord::DB_COUNTERS))
end
2019-12-04 20:38:33 +05:30
def log_job_start(payload)
2018-05-09 12:01:36 +05:30
payload['message'] = "#{base_message(payload)}: start"
payload['job_status'] = 'start'
2019-12-26 22:10:19 +05:30
scheduling_latency_s = ::Gitlab::InstrumentationHelper.queue_duration_for_job(payload)
payload['scheduling_latency_s'] = scheduling_latency_s if scheduling_latency_s
2019-10-12 21:52:04 +05:30
2018-05-09 12:01:36 +05:30
payload
end
2019-12-04 20:38:33 +05:30
def log_job_done(job, started_time, payload, job_exception = nil)
2018-05-09 12:01:36 +05:30
payload = payload.dup
2019-10-12 21:52:04 +05:30
add_instrumentation_keys!(job, payload)
2020-05-24 23:13:21 +05:30
add_logging_extras!(job, payload)
2020-06-23 00:09:42 +05:30
add_db_counters!(job, payload)
2019-12-04 20:38:33 +05:30
elapsed_time = elapsed(started_time)
add_time_keys!(elapsed_time, payload)
2018-05-09 12:01:36 +05:30
message = base_message(payload)
if job_exception
2020-04-22 19:07:51 +05:30
payload['message'] = "#{message}: fail: #{payload['duration_s']} sec"
2018-05-09 12:01:36 +05:30
payload['job_status'] = 'fail'
payload['error_message'] = job_exception.message
2019-12-21 20:55:43 +05:30
payload['error_class'] = job_exception.class.name
2018-05-09 12:01:36 +05:30
else
2020-04-22 19:07:51 +05:30
payload['message'] = "#{message}: done: #{payload['duration_s']} sec"
2018-05-09 12:01:36 +05:30
payload['job_status'] = 'done'
end
2020-04-22 19:07:51 +05:30
db_duration = ActiveRecord::LogSubscriber.runtime
payload['db_duration_s'] = Gitlab::Utils.ms_to_round_sec(db_duration)
2018-05-09 12:01:36 +05:30
payload
end
2019-12-04 20:38:33 +05:30
def add_time_keys!(time, payload)
2020-05-24 23:13:21 +05:30
payload['duration_s'] = time[:duration].round(Gitlab::InstrumentationHelper::DURATION_PRECISION)
2019-12-21 20:55:43 +05:30
# ignore `cpu_s` if the platform does not support Process::CLOCK_THREAD_CPUTIME_ID (time[:cputime] == 0)
# supported OS version can be found at: https://www.rubydoc.info/stdlib/core/2.1.6/Process:clock_gettime
2020-05-24 23:13:21 +05:30
payload['cpu_s'] = time[:cputime].round(Gitlab::InstrumentationHelper::DURATION_PRECISION) if time[:cputime] > 0
2020-03-13 15:44:24 +05:30
payload['completed_at'] = Time.now.utc.to_f
2019-12-04 20:38:33 +05:30
end
def elapsed(t0)
t1 = get_time
{
duration: t1[:now] - t0[:now],
2019-12-21 20:55:43 +05:30
cputime: t1[:thread_cputime] - t0[:thread_cputime]
2019-12-04 20:38:33 +05:30
}
end
def get_time
{
now: current_time,
2019-12-21 20:55:43 +05:30
thread_cputime: defined?(Process::CLOCK_THREAD_CPUTIME_ID) ? Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID) : 0
2019-12-04 20:38:33 +05:30
}
end
2018-05-09 12:01:36 +05:30
def current_time
Gitlab::Metrics::System.monotonic_time
end
end
end
end