Skip to content

Commit 74b3b57

Browse files
committed
fix: Account for nil routes
It seems that there are cases in Rails functional tests where it bypasses the routing system and the `action_dispatch.route_uri_pattern` header not being set. https://github.com/rails/rails/blob/747f85f200e7bb2c1a31b4e26e5a5655e2dc0cdc/actionpack/lib/action_dispatch/http/request.rb#L160 ```console 4) SessionsController POST #create when user is not found or not persisted redirects to no access path Failure/Error: post :create, params: { provider: 'github' } NoMethodError: undefined method 'chomp' for nil # /usr/local/bundle/gems/opentelemetry-instrumentation-action_pack-0.12.0/lib/opentelemetry/instrumentation/action_pack/handlers/action_controller.rb:56:in 'OpenTelemetry::Instrumentation::ActionPack::Handlers::ActionController#to_span_name_and_attributes' # /usr/local/bundle/gems/opentelemetry-instrumentation-action_pack-0.12.0/lib/opentelemetry/instrumentation/action_pack/handlers/action_controller.rb:26:in 'OpenTelemetry::Instrumentation::ActionPack::Handlers::ActionController#start' # ./spec/controllers/sessions_controller_spec.rb:28:in 'block (4 levels) in <top (required)>' ``` Our Test suite executes the routing system so we are unable to recreate this error case. This change adds safe navigation navigation to avoid running into errors when running Functional Tests and the instrumentation is enabled. ```console irb(main):003> route_uri_pattern.chomp('(.format)') (irb):3:in '<main>': undefined method 'chomp' for nil (NoMethodError) from <internal:kernel>:168:in 'Kernel#loop' from /Users/arielvalentin/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/irb-1.15.2/exe/irb:9:in '<top (required)>' from /Users/arielvalentin/.rbenv/versions/3.4.1/bin/irb:25:in 'Kernel#load' from /Users/arielvalentin/.rbenv/versions/3.4.1/bin/irb:25:in '<main>' irb(main):004> route_uri_pattern&.chomp('(.format)') => nil ```
1 parent b4d1ec4 commit 74b3b57

File tree

1 file changed

+1
-1
lines changed
  • instrumentation/action_pack/lib/opentelemetry/instrumentation/action_pack/handlers

1 file changed

+1
-1
lines changed

instrumentation/action_pack/lib/opentelemetry/instrumentation/action_pack/handlers/action_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def finish(_name, _id, payload)
5353
# @return [Array<String, Hash>] the span name and attributes
5454
def to_span_name_and_attributes(payload)
5555
request = payload[:request]
56-
http_route = request.route_uri_pattern.chomp('(.:format)') if request.respond_to?(:route_uri_pattern)
56+
http_route = request.route_uri_pattern&.chomp('(.:format)') if request.respond_to?(:route_uri_pattern)
5757

5858
attributes = {
5959
OpenTelemetry::SemanticConventions::Trace::CODE_NAMESPACE => String(payload[:controller]),

0 commit comments

Comments
 (0)