From d3a7bdda986949ca76df3c4932ee8c973437a743 Mon Sep 17 00:00:00 2001 From: Jan Provaznik Date: Wed, 5 Jun 2019 19:20:37 +0200 Subject: [PATCH] Moved RackTimeout observer to a different location Because there will be similar observer for PumaWorkerKiller, it makes sense to keep both on better place. --- config/initializers/rack_timeout.rb | 2 +- lib/gitlab/cluster/rack_timeout_observer.rb | 48 +++++++++++++++++++ lib/gitlab/rack_timeout_observer.rb | 46 ------------------ .../rack_timeout_observer_spec.rb | 2 +- 4 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 lib/gitlab/cluster/rack_timeout_observer.rb delete mode 100644 lib/gitlab/rack_timeout_observer.rb rename spec/lib/gitlab/{ => cluster}/rack_timeout_observer_spec.rb (96%) diff --git a/config/initializers/rack_timeout.rb b/config/initializers/rack_timeout.rb index 5c4f2dd708c..58f46b55725 100644 --- a/config/initializers/rack_timeout.rb +++ b/config/initializers/rack_timeout.rb @@ -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 diff --git a/lib/gitlab/cluster/rack_timeout_observer.rb b/lib/gitlab/cluster/rack_timeout_observer.rb new file mode 100644 index 00000000000..2bc006b8011 --- /dev/null +++ b/lib/gitlab/cluster/rack_timeout_observer.rb @@ -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 diff --git a/lib/gitlab/rack_timeout_observer.rb b/lib/gitlab/rack_timeout_observer.rb deleted file mode 100644 index 80d3f7dea60..00000000000 --- a/lib/gitlab/rack_timeout_observer.rb +++ /dev/null @@ -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 diff --git a/spec/lib/gitlab/rack_timeout_observer_spec.rb b/spec/lib/gitlab/cluster/rack_timeout_observer_spec.rb similarity index 96% rename from spec/lib/gitlab/rack_timeout_observer_spec.rb rename to spec/lib/gitlab/cluster/rack_timeout_observer_spec.rb index 3dc1a8b68fb..2adb1d7cc71 100644 --- a/spec/lib/gitlab/rack_timeout_observer_spec.rb +++ b/spec/lib/gitlab/cluster/rack_timeout_observer_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Gitlab::RackTimeoutObserver do +describe Gitlab::Cluster::RackTimeoutObserver do let(:counter) { Gitlab::Metrics::NullMetric.instance } before do