Skip to content

Commit

Permalink
Add http.route tag setting to rack middleware as fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
y9v committed Aug 26, 2024
1 parent 7609f54 commit 360b496
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gemfiles/ruby_3.3_rails7.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gemfiles/ruby_3.3_rails71.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/datadog/tracing/contrib/rack/middlewares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ def set_request_tags!(trace, request_span, env, status, headers, response, origi
request_span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_USER_AGENT, user_agent)
end

if request_span.get_tag(Tracing::Metadata::Ext::HTTP::TAG_ROUTE).nil?
request_span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_ROUTE, env['PATH_INFO'])
end

HeaderTagging.tag_request_headers(request_span, request_header_collection, configuration)
HeaderTagging.tag_response_headers(request_span, headers, configuration) if headers

Expand Down
66 changes: 66 additions & 0 deletions spec/datadog/tracing/contrib/rack/http_route_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'datadog/tracing/contrib/support/spec_helper'

require 'rack/test'

require 'datadog'
require 'datadog/tracing/contrib/rack/middlewares'

RSpec.describe 'Rack testing for http.route' do
include Rack::Test::Methods

before do
Datadog.configure do |c|
c.tracing.instrument :rack
end
end

after do
Datadog.configuration.tracing[:rack].reset!
end

let(:app) do
app = rack_app

Rack::Builder.new do
use Datadog::Tracing::Contrib::Rack::TraceMiddleware

%w[/ /rack].each do |route|
map route do
run app
end
end
end.to_app
end

let(:rack_app) do
Rack::Builder.new do
map '/hello/world' do
run ->(_env) { [200, { 'content-type' => 'text/plain' }, 'hello world'] }
end

map '/hello/:id' do
run ->(_env) { [200, { 'content-type' => 'text/plain' }, "hello #{params[:id]}"] }
end
end
end

it 'sets http.route tag on request to base route' do
response = get('/hello/world')

expect(response).to be_ok
expect(request_span.get_tag('http.route')).to eq('/hello/world')
end

it 'sets http.route tag on requst to nested app route' do
response = get('/rack/hello/world')

expect(response).to be_ok
expect(request_span.get_tag('http.route')).to eq('/rack/hello/world')
end

def request_span
spans.detect do |span|
span.name == Datadog::Tracing::Contrib::Rack::Ext::SPAN_REQUEST
end
end
end

0 comments on commit 360b496

Please sign in to comment.