discourse / prometheus_exporter

A framework for collecting and aggregating prometheus metrics
MIT License
525 stars 153 forks source link

Get controller labels from controller, not params #293

Open richardTowers opened 9 months ago

richardTowers commented 9 months ago

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.

I haven't added any new tests for this, but I have tested it locally (by pointing a local rails app at a local copy of the gem, and doing the curl request above). Please let me know if you'd like a test (I might need a hint as to where to put it though).