2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-24 11:07:41 -04:00
|
|
|
# This Rack middleware is intended to measure the latency between
|
|
|
|
# gitlab-workhorse forwarding a request to the Rails application and the
|
|
|
|
# time this middleware is reached.
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Middleware
|
2016-05-26 11:53:21 -04:00
|
|
|
class RailsQueueDuration
|
2019-02-10 19:43:44 -05:00
|
|
|
GITLAB_RAILS_QUEUE_DURATION_KEY = 'GITLAB_RAILS_QUEUE_DURATION'
|
|
|
|
|
2016-05-24 11:07:41 -04:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
2017-08-15 13:44:37 -04:00
|
|
|
|
2016-05-24 11:07:41 -04:00
|
|
|
def call(env)
|
2016-05-25 08:28:21 -04:00
|
|
|
trans = Gitlab::Metrics.current_transaction
|
2016-08-19 06:25:52 -04:00
|
|
|
proxy_start = env['HTTP_GITLAB_WORKHORSE_PROXY_START'].presence
|
2016-05-25 08:28:21 -04:00
|
|
|
if trans && proxy_start
|
|
|
|
# Time in milliseconds since gitlab-workhorse started the request
|
2017-09-07 15:36:51 -04:00
|
|
|
duration = Time.now.to_f * 1_000 - proxy_start.to_f / 1_000_000
|
2020-07-28 08:09:49 -04:00
|
|
|
trans.set(:gitlab_transaction_rails_queue_duration_total, duration) do
|
|
|
|
multiprocess_mode :livesum
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
|
|
|
|
duration_s = Gitlab::Utils.ms_to_round_sec(duration)
|
2020-07-28 08:09:49 -04:00
|
|
|
trans.observe(:gitlab_rails_queue_duration_seconds, duration_s) do
|
|
|
|
docstring 'Measures latency between GitLab Workhorse forwarding a request to Rails'
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
env[GITLAB_RAILS_QUEUE_DURATION_KEY] = duration_s
|
2016-05-24 11:07:41 -04:00
|
|
|
end
|
2016-05-25 08:28:21 -04:00
|
|
|
|
2016-05-24 11:07:41 -04:00
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|