Skip to content

Commit

Permalink
Instrument Rodauth controller and current route
Browse files Browse the repository at this point in the history
Passing `RodauthApp` for the controller works for logging, but it was
breaking some APM agents like NewRelic, which relied on the constant
name referencing a controller class, so I needed to make the Rodauth
app quack enough like a controller.

We change this by passing the Rodauth controller and current Rodauth
route as the controller and action. This should improve APM
integrations, because it makes it easier to differentiate between
different Rodauth routes. Even though the Rodauth controller is not
actually processing the request, I think this is close enough and prefer
this trade-off.
  • Loading branch information
janko committed Apr 8, 2024
1 parent 59b3183 commit 4a4af03
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 13 deletions.
6 changes: 0 additions & 6 deletions lib/rodauth/rails/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ def self.rodauth!(name)
rodauth(name) or fail Rodauth::Rails::Error, "unknown rodauth configuration: #{name.inspect}"
end

# The newrelic_rpm gem expects this when we pass the roda class as
# :controller in instrumentation payload.
def self.controller_path
name.underscore
end

module RequestMethods
# Automatically route the prefix if it hasn't been routed already. This
# way people only have to update prefix in their Rodauth configurations.
Expand Down
4 changes: 2 additions & 2 deletions lib/rodauth/rails/feature/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def rails_instrument_request
request = rails_request

raw_payload = {
controller: self.class.roda_class.name,
action: "call",
controller: rails_controller.name,
action: current_route.to_s,
request: request,
params: request.filtered_parameters,
headers: request.headers,
Expand Down
2 changes: 1 addition & 1 deletion rodauth-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "railties", ">= 5.0", "< 8"
spec.add_dependency "rodauth", "~> 2.30"
spec.add_dependency "rodauth", "~> 2.34"
spec.add_dependency "roda", "~> 3.76"
spec.add_dependency "sequel-activerecord_connection", "~> 1.1"
spec.add_dependency "rodauth-model", "~> 0.2"
Expand Down
8 changes: 4 additions & 4 deletions test/integration/instrumentation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class InstrumentationTest < IntegrationTest
visit "/login"
end

assert_match /Processing by RodauthApp#call as HTML/, logged
assert_match /Processing by RodauthController#login as HTML/, logged
refute_match /Parameters/, logged
if ::Rails.gem_version >= Gem::Version.new("6.0")
assert_match /Completed 200 OK in \d+ms \(Views: \d+ms | ActiveRecord: \d+\.\d+ms | Allocations: \d+\)/, logged
Expand All @@ -22,7 +22,7 @@ class InstrumentationTest < IntegrationTest
{ "CONTENT_TYPE" => "application/json", "HTTP_ACCEPT" => "application/json" }
end

assert_match /Processing by RodauthApp#call as JSON/, logged
assert_match /Processing by ActionController::API#login as JSON/, logged
assert_match /Parameters: {"login"=>"user@example\.com", "password"=>"secret"}/, logged
assert_match /Completed 401 Unauthorized/, logged
end
Expand All @@ -32,7 +32,7 @@ class InstrumentationTest < IntegrationTest
visit "/change-password"
end

assert_match /Processing by RodauthApp#call as HTML/, logged
assert_match /Processing by RodauthController#change_password as HTML/, logged
assert_match /Redirected to \/login/, logged
assert_match /Completed 302 Found/, logged
end
Expand All @@ -42,7 +42,7 @@ class InstrumentationTest < IntegrationTest
visit "/login?early_return=true"
end

assert_match /Processing by RodauthApp#call as HTML/, logged
assert_match /Processing by RodauthController#login as HTML/, logged
assert_match /Completed 201 Created in \d+ms/, logged
end

Expand Down

0 comments on commit 4a4af03

Please sign in to comment.