Skip to content

Commit

Permalink
Fix resource name when Grape paths contain a leading slash (#4033)
Browse files Browse the repository at this point in the history
* fix grape leading slash resource issue

* don't use .delete_prefix

---------

Co-authored-by: Sergey Fedorov <oni.strech@gmail.com>
  • Loading branch information
jethrodaniel and Strech authored Oct 30, 2024
1 parent 9e42d1b commit 34f4808
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/datadog/tracing/contrib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,11 @@ def endpoint_expand_path(endpoint)
route_path = endpoint.options[:path]
namespace = endpoint.routes.first && endpoint.routes.first.namespace || ''

parts = (namespace.split('/') + route_path).reject { |p| p.blank? || p.eql?('/') }
parts.join('/').prepend('/')
path = (namespace.split('/') + route_path)
.reject { |p| p.blank? || p.eql?('/') }
.join('/')
path.prepend('/') if path[0] != '/'
path
end

def service_name
Expand Down
33 changes: 33 additions & 0 deletions spec/datadog/tracing/contrib/grape/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
'OK'
end
end

get '/path/with/leading/slash' do
'leading-slash'
end
end
)
end
Expand Down Expand Up @@ -126,6 +130,10 @@
'OK'
end
end

get '/path/with/leading/slash' do
'leading-slash'
end
end
)

Expand Down Expand Up @@ -719,6 +727,19 @@
end
end

context 'when the path has a leading slash' do
subject(:response) { get '/path/with/leading/slash' }

before do
is_expected.to be_ok
end

it 'sets the resource correctly' do
expect(trace.name).to eq('grape.endpoint_run')
expect(trace.resource).to eq('TestingAPI GET /path/with/leading/slash')
end
end

context 'when tracing is disabled' do
subject(:response) { get '/base/success' }

Expand Down Expand Up @@ -911,5 +932,17 @@
expect(trace.resource).to eq('RackTestingAPI GET /span_resource_rack/span_resource')
end
end

context 'when the path has a leading slash' do
subject(:response) { get '/api/path/with/leading/slash' }

before do
is_expected.to be_ok
end

it 'sets the resource correctly' do
expect(trace.resource).to eq('RackTestingAPI GET /path/with/leading/slash')
end
end
end
end

0 comments on commit 34f4808

Please sign in to comment.