debian-mirror-gitlab/lib/gitlab/metrics/sidekiq_middleware.rb

36 lines
1.1 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module Gitlab
module Metrics
# Sidekiq middleware for tracking jobs.
#
# This middleware is intended to be used as a server-side middleware.
class SidekiqMiddleware
2020-06-23 00:09:42 +05:30
def call(worker, payload, queue)
2018-03-17 18:26:18 +05:30
trans = BackgroundTransaction.new(worker.class)
begin
2016-08-24 12:49:21 +05:30
# Old gitlad-shell messages don't provide enqueued_at/created_at attributes
2020-06-23 00:09:42 +05:30
enqueued_at = payload['enqueued_at'] || payload['created_at'] || 0
trans.set(:sidekiq_queue_duration, Time.current.to_f - enqueued_at)
trans.run { yield }
2016-09-13 17:45:13 +05:30
rescue Exception => error # rubocop: disable Lint/RescueException
trans.add_event(:sidekiq_exception)
raise error
2020-06-23 00:09:42 +05:30
ensure
add_info_to_payload(payload, trans)
end
end
2020-06-23 00:09:42 +05:30
private
def add_info_to_payload(payload, trans)
payload[:db_count] = trans.get(:db_count, :counter).to_i
payload[:db_write_count] = trans.get(:db_write_count, :counter).to_i
payload[:db_cached_count] = trans.get(:db_cached_count, :counter).to_i
end
end
end
end