Commit ac01310f authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'rack-timeout-rename' into 'master'

Moved RackTimeout observer to a different location and ignore transition states

See merge request gitlab-org/gitlab-ce!29214
parents 70dad152 13364c00
...@@ -18,6 +18,6 @@ if defined?(::Puma) && !Rails.env.test? ...@@ -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
# frozen_string_literal: true
module Gitlab
module Cluster
class RackTimeoutObserver
TRANSITION_STATES = %i(ready active).freeze
def initialize
@counter = Gitlab::Metrics.counter(:rack_requests_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
return if TRANSITION_STATES.include?(info.state)
@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 @@ ...@@ -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
...@@ -25,7 +25,7 @@ describe Gitlab::RackTimeoutObserver do ...@@ -25,7 +25,7 @@ describe Gitlab::RackTimeoutObserver do
subject { described_class.new } subject { described_class.new }
it 'increments timeout counter' do it 'increments counter' do
expect(counter) expect(counter)
.to receive(:increment) .to receive(:increment)
.with({ controller: 'foo', action: 'bar', route: nil, state: :timed_out }) .with({ controller: 'foo', action: 'bar', route: nil, state: :timed_out })
...@@ -45,7 +45,7 @@ describe Gitlab::RackTimeoutObserver do ...@@ -45,7 +45,7 @@ describe Gitlab::RackTimeoutObserver do
subject { described_class.new } subject { described_class.new }
it 'increments timeout counter' do it 'increments counter' do
allow(endpoint).to receive_message_chain('route.pattern.origin') { 'foobar' } allow(endpoint).to receive_message_chain('route.pattern.origin') { 'foobar' }
expect(counter) expect(counter)
.to receive(:increment) .to receive(:increment)
...@@ -54,5 +54,24 @@ describe Gitlab::RackTimeoutObserver do ...@@ -54,5 +54,24 @@ describe Gitlab::RackTimeoutObserver do
subject.callback.call(env) subject.callback.call(env)
end end
end end
context 'when request is being processed' do
let(:endpoint) { double }
let(:env) do
{
::Rack::Timeout::ENV_INFO_KEY => double(state: :active),
Grape::Env::API_ENDPOINT => endpoint
}
end
subject { described_class.new }
it 'does not increment counter' do
allow(endpoint).to receive_message_chain('route.pattern.origin') { 'foobar' }
expect(counter).not_to receive(:increment)
subject.callback.call(env)
end
end
end end
end end
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