debian-mirror-gitlab/lib/gitlab/query_limiting/middleware.rb

56 lines
1.3 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
# frozen_string_literal: true
module Gitlab
module QueryLimiting
# Middleware for reporting (or raising) when a request performs more than a
# certain amount of database queries.
class Middleware
2019-12-04 20:38:33 +05:30
CONTROLLER_KEY = 'action_controller.instance'
ENDPOINT_KEY = 'api.endpoint'
2018-03-17 18:26:18 +05:30
def initialize(app)
@app = app
end
def call(env)
2021-10-27 15:23:28 +05:30
transaction, retval = ::Gitlab::QueryLimiting::Transaction.run do
2018-03-17 18:26:18 +05:30
@app.call(env)
end
transaction.action = action_name(env)
transaction.act_upon_results
retval
end
def action_name(env)
if env[CONTROLLER_KEY]
action_for_rails(env)
elsif env[ENDPOINT_KEY]
action_for_grape(env)
end
end
private
def action_for_rails(env)
controller = env[CONTROLLER_KEY]
action = "#{controller.class.name}##{controller.action_name}"
2020-03-13 15:44:24 +05:30
if controller.media_type == 'text/html'
2018-03-17 18:26:18 +05:30
action
else
2020-03-13 15:44:24 +05:30
"#{action} (#{controller.media_type})"
2018-03-17 18:26:18 +05:30
end
end
def action_for_grape(env)
endpoint = env[ENDPOINT_KEY]
route = endpoint.route rescue nil
"#{route.request_method} #{route.path}" if route
end
end
end
end