Skip to content

Commit 75ba22b

Browse files
fix: Account for nil routes (#1527)
* 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 ``` * squash: Add comments To explain the lack of test coverage
1 parent eae73d6 commit 75ba22b

File tree

1 file changed

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

1 file changed

+4
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ 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+
# 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.
57+
# Our Test suite executes the routing system so we are unable to recreate this error case.
58+
# https://github.com/rails/rails/blob/747f85f200e7bb2c1a31b4e26e5a5655e2dc0cdc/actionpack/lib/action_dispatch/http/request.rb#L160
59+
http_route = request.route_uri_pattern&.chomp('(.:format)') if request.respond_to?(:route_uri_pattern)
5760

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

0 commit comments

Comments
 (0)