diff options
author | Bob Van Landuyt <bob@gitlab.com> | 2019-06-12 19:00:27 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@gitlab.com> | 2019-06-12 19:00:27 +0300 |
commit | ac01310fddc4d3d52c575e15b1e7589e02186312 (patch) | |
tree | 1b92b3ddea45c939f2cc5760817ba9768aa6c7c1 /lib | |
parent | 70dad1521174b7d9e7f665ee11611368f8d71d31 (diff) | |
parent | 13364c00d57ce3150a50c23ea3eb0e6af4271d92 (diff) |
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
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/cluster/rack_timeout_observer.rb | 51 | ||||
-rw-r--r-- | lib/gitlab/rack_timeout_observer.rb | 46 |
2 files changed, 51 insertions, 46 deletions
diff --git a/lib/gitlab/cluster/rack_timeout_observer.rb b/lib/gitlab/cluster/rack_timeout_observer.rb new file mode 100644 index 00000000000..5182b2be148 --- /dev/null +++ b/lib/gitlab/cluster/rack_timeout_observer.rb @@ -0,0 +1,51 @@ +# 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 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 |