Skip to content

Commit 1e9853a

Browse files
akahnarielvalentin
andauthored
feat: Strip Rails (.:format) suffix from http.route (#1375)
* chore: Strip Rails (.:format) suffix from http.route * test: Add test for http.route cleaning --------- Co-authored-by: Ariel Valentin <[email protected]>
1 parent d0444df commit 1e9853a

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

+2-2
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 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]),
@@ -63,7 +63,7 @@ def to_span_name_and_attributes(payload)
6363
attributes[OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET] = request.filtered_path if request.filtered_path != request.fullpath
6464

6565
if @span_naming == :semconv
66-
return ["#{request.method} #{http_route.gsub('(.:format)', '')}", attributes] if http_route
66+
return ["#{request.method} #{http_route}", attributes] if http_route
6767

6868
return ["#{request.method} /#{payload.dig(:params, :controller)}/#{payload.dig(:params, :action)}", attributes]
6969
end

instrumentation/action_pack/test/opentelemetry/instrumentation/action_pack/handlers/action_controller_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858
_(span.attributes['code.function']).must_equal 'ok'
5959
end
6060

61+
it 'strips (:format) from http.route' do
62+
skip "Rails #{Rails.gem_version} does not define ActionDispatch::Request#route_uri_pattern" if Rails.gem_version < Gem::Version.new('7.1')
63+
64+
get 'items/1234'
65+
66+
_(span.attributes['http.route']).must_equal '/items/:id'
67+
end
68+
6169
it 'does not memoize data across requests' do
6270
get '/ok'
6371
get '/items/new'

0 commit comments

Comments
 (0)