Commit d3a7bdda authored by Jan Provaznik's avatar Jan Provaznik

Moved RackTimeout observer to a different location

Because there will be similar observer for PumaWorkerKiller,
it makes sense to keep both on better place.
parent f18f3ed9
......@@ -18,6 +18,6 @@ if defined?(::Puma) && !Rails.env.test?
wait_timeout: 90)
end
observer = Gitlab::RackTimeoutObserver.new
observer = Gitlab::Cluster::RackTimeoutObserver.new
Rack::Timeout.register_state_change_observer(:gitlab_rack_timeout, &observer.callback)
end
# frozen_string_literal: true
module Gitlab
module Cluster
class RackTimeoutObserver
def initialize
@counter = Gitlab::Metrics.counter(:rack_state_total, 'Number of requests in a given rack state')
end
# returns the Proc to be used as the observer callback block
def callback
method(:log_timeout_exception)
end
private
def log_timeout_exception(env)
info = env[::Rack::Timeout::ENV_INFO_KEY]
return unless info
@counter.increment(labels(info, env))
end
def labels(info, env)
params = controller_params(env) || grape_params(env) || {}
{
controller: params['controller'],
action: params['action'],
route: params['route'],
state: info.state
}
end
def controller_params(env)
env['action_dispatch.request.parameters']
end
def grape_params(env)
endpoint = env[Grape::Env::API_ENDPOINT]
route = endpoint&.route&.pattern&.origin
return unless route
{ 'route' => route }
end
end
end
end
# frozen_string_literal: true
module Gitlab
class RackTimeoutObserver
def initialize
@counter = Gitlab::Metrics.counter(:rack_state_total, 'Number of requests in a given rack state')
end
# returns the Proc to be used as the observer callback block
def callback
method(:log_timeout_exception)
end
private
def log_timeout_exception(env)
info = env[::Rack::Timeout::ENV_INFO_KEY]
return unless info
@counter.increment(labels(info, env))
end
def labels(info, env)
params = controller_params(env) || grape_params(env) || {}
{
controller: params['controller'],
action: params['action'],
route: params['route'],
state: info.state
}
end
def controller_params(env)
env['action_dispatch.request.parameters']
end
def grape_params(env)
endpoint = env[Grape::Env::API_ENDPOINT]
route = endpoint&.route&.pattern&.origin
return unless route
{ 'route' => route }
end
end
end
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe Gitlab::RackTimeoutObserver do
describe Gitlab::Cluster::RackTimeoutObserver do
let(:counter) { Gitlab::Metrics::NullMetric.instance }
before do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment