Moved RackTimeout observer to a different location

Because there will be similar observer for PumaWorkerKiller,
it makes sense to keep both on better place.
This commit is contained in:
Jan Provaznik 2019-06-05 19:20:37 +02:00
parent f18f3ed926
commit d3a7bdda98
4 changed files with 50 additions and 48 deletions

View file

@ -18,6 +18,6 @@ if defined?(::Puma) && !Rails.env.test?
wait_timeout: 90) wait_timeout: 90)
end end
observer = Gitlab::RackTimeoutObserver.new observer = Gitlab::Cluster::RackTimeoutObserver.new
Rack::Timeout.register_state_change_observer(:gitlab_rack_timeout, &observer.callback) Rack::Timeout.register_state_change_observer(:gitlab_rack_timeout, &observer.callback)
end end

View file

@ -0,0 +1,48 @@
# 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

View file

@ -1,46 +0,0 @@
# 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

View file

@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::RackTimeoutObserver do describe Gitlab::Cluster::RackTimeoutObserver do
let(:counter) { Gitlab::Metrics::NullMetric.instance } let(:counter) { Gitlab::Metrics::NullMetric.instance }
before do before do