Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Strip Rails (.:format) suffix from http.route #1375

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def finish(_name, _id, payload)
# @return [Array<String, Hash>] the span name and attributes
def to_span_name_and_attributes(payload)
request = payload[:request]
http_route = request.route_uri_pattern if request.respond_to?(:route_uri_pattern)
http_route = request.route_uri_pattern.chomp('(.:format)') if request.respond_to?(:route_uri_pattern)

attributes = {
OpenTelemetry::SemanticConventions::Trace::CODE_NAMESPACE => String(payload[:controller]),
Expand All @@ -63,7 +63,7 @@ def to_span_name_and_attributes(payload)
attributes[OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET] = request.filtered_path if request.filtered_path != request.fullpath

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

return ["#{request.method} /#{payload.dig(:params, :controller)}/#{payload.dig(:params, :action)}", attributes]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
_(span.attributes['code.function']).must_equal 'ok'
end

it 'strips (:format) from http.route' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test can probably be consolidated with another one but I see why being explicit here is helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I noticed the tests were a bit disjointed, but I thought I would leave that to the maintainers or someone more familiar with the project, and to a future PR.

There's an existing describe block for span naming, broken down by Rails version. Maybe there could be another one for span attributes, broken down by Rails version, that this could go in. Alternatively, there could be separate top-level blocks for Rails versions, each with naming and span sections within.

skip "Rails #{Rails.gem_version} does not define ActionDispatch::Request#route_uri_pattern" if Rails.gem_version < Gem::Version.new('7.1')

get 'items/1234'

_(span.attributes['http.route']).must_equal '/items/:id'
end

it 'does not memoize data across requests' do
get '/ok'
get '/items/new'
Expand Down
Loading