Skip to content

Commit

Permalink
Added: resource_script_names flag for Sinatra tracer.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Dec 20, 2017
1 parent 6cc2c27 commit b5c6cff
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
10 changes: 9 additions & 1 deletion lib/ddtrace/contrib/sinatra/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ module Tracer

option :tracer, default: Datadog.tracer

option :resource_script_names, default: false

def route(verb, action, *)
# Keep track of the route name when the app is instantiated for an
# incoming request.
condition do
@datadog_route = "#{request.script_name}#{action}"
# If the option to prepend script names is enabled, then
# prepend the script name from the request onto the action.
@datadog_route = if Datadog.configuration[:sinatra][:resource_script_names]
"#{request.script_name}#{action}"
else
action
end
end

super
Expand Down
73 changes: 59 additions & 14 deletions test/contrib/sinatra/multi_app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

class MultiAppTest < TracerTestBase
def app
@use_multi_app ? multi_app : single_app
end

def multi_app
Rack::Builder.new do
map '/one' do
run FirstTestApp
Expand All @@ -15,8 +19,12 @@ def app
end.to_app
end

def single_app
FirstTestApp
end

def setup
@writer = FauxWriter.new()
@writer = FauxWriter.new
FirstTestApp.set :datadog_test_writer, @writer
SecondTestApp.set :datadog_test_writer, @writer

Expand All @@ -26,36 +34,73 @@ def setup
super
end

def teardown
disable_script_names!
end

def enable_script_names!
Datadog.configuration[:sinatra][:resource_script_names] = true
end

def disable_script_names!
Datadog.configuration[:sinatra][:resource_script_names] = false
end

# Test for when a single, normal app is setup.
# script_name is ''
# (To make sure we aren't breaking normal Sinatra apps.)
def test_resource_name_without_script_name
first_path = '/one/endpoint'
get first_path, {}, 'SCRIPT_NAME' => ''
@use_multi_app = false
enable_script_names!

get '/endpoint'

spans = @writer.spans.select { |s| s.name == 'sinatra.request' }
assert_equal(1, spans.length)

spans.first.tap do |span|
assert_equal('GET /endpoint', span.resource)
end
end

# Test for when a multi-app is setup.
# script_name is the sub-app's base prefix.
# e.g. '/one' in this example.
# (To make sure we aren't adding script names when disabled.)
def test_resource_name_with_script_name_disabled
@use_multi_app = true
disable_script_names!

get '/one/endpoint'

spans = @writer.spans.select { |s| s.name == 'sinatra.request' }
assert_equal(1, spans.length)

spans.first.tap do |span|
assert_equal("GET #{first_path}", span.resource)
assert_equal('GET /endpoint', span.resource)
end
end

# Test for when a multi-app is setup.
# script_name is the sub-app's base prefix.
# e.g. '/one' and '/two' in this example.
# (To make sure we are adding script names when enabled.)
def test_resource_name_with_script_name
first_path = '/one/endpoint'
first_script = '/foo'
get first_path, {}, 'SCRIPT_NAME' => first_script
@use_multi_app = true
enable_script_names!

second_path = '/two/endpoint'
second_script = '/bar'
get second_path, {}, 'SCRIPT_NAME' => second_script
get '/one/endpoint'
get '/two/endpoint'

spans = @writer.spans.select { |s| s.name == 'sinatra.request' }
assert_equal(2, spans.length)

spans.last.tap do |span|
assert_equal("GET #{first_script}#{first_path}", span.resource)
spans.first.tap do |span|
assert_equal('GET /one/endpoint', span.resource)
end

spans.first.tap do |span|
assert_equal("GET #{second_script}#{second_path}", span.resource)
spans.last.tap do |span|
assert_equal('GET /two/endpoint', span.resource)
end
end
end

0 comments on commit b5c6cff

Please sign in to comment.