Skip to content

Commit 2f1e7e9

Browse files
Get controller labels from controller, not params (#293)
prometheus_exporter currently reads the controller and action label from action_dispatch.request.parameters. This can lead to conflicts, where there's a form parameter called "action", or "controller" which takes precedence over "which controller action is this?". This can be validated with a curl request to a rails application instrumented with prometheus_exporter: curl -v http://127.0.0.1:3000/ --data 'controller=test' Results in: # HELP http_requests_total Total HTTP requests from web app. # TYPE http_requests_total counter http_requests_total{action="other",controller="test",status="404"} 1 This commit pulls the controller instance from `action_controller.instance`, and then calls the controller_name / action_name methods, which should be accurate even when conflicting form parameters are provided.
1 parent 236bf09 commit 2f1e7e9

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/prometheus_exporter/middleware.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ def call(env)
7474
end
7575

7676
def default_labels(env, result)
77-
params = env["action_dispatch.request.parameters"]
77+
controller_instance = env["action_controller.instance"]
7878
action = controller = nil
79-
if params
80-
action = params["action"]
81-
controller = params["controller"]
79+
if controller_instance
80+
action = controller_instance.action_name
81+
controller = controller_instance.controller_name
8282
elsif (cors = env["rack.cors"]) && cors.respond_to?(:preflight?) && cors.preflight?
8383
# if the Rack CORS Middleware identifies the request as a preflight request,
8484
# the stack doesn't get to the point where controllers/actions are defined

0 commit comments

Comments
 (0)