blob: 0ee700caf5bd68da09dbab14cd94a6b71eaf47f0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# 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
CONTROLLER_KEY = 'action_controller.instance'
ENDPOINT_KEY = 'api.endpoint'
def initialize(app)
@app = app
end
def call(env)
transaction, retval = ::Gitlab::QueryLimiting::Transaction.run do
@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}"
if controller.media_type == 'text/html'
action
else
"#{action} (#{controller.media_type})"
end
end
def action_for_grape(env)
endpoint = env[ENDPOINT_KEY]
route = begin
endpoint.route
rescue StandardError
nil
end
"#{route.request_method} #{route.path}" if route
end
end
end
end
|