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

Add script name to resource for Sinatra if available #283

Merged
merged 2 commits into from
Dec 20, 2017
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
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 = 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
11 changes: 11 additions & 0 deletions test/contrib/sinatra/first_test_app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'contrib/sinatra/tracer_test_base'

class MultiAppTest < TracerTestBase
class FirstTestApp < Sinatra::Base
register Datadog::Contrib::Sinatra::Tracer

get '/endpoint' do
'1'
end
end
end
106 changes: 106 additions & 0 deletions test/contrib/sinatra/multi_app_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
require 'contrib/sinatra/tracer_test_base'
require 'contrib/sinatra/first_test_app'
require 'contrib/sinatra/second_test_app'

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
end

map '/two' do
run SecondTestApp
end
end.to_app
end

def single_app
FirstTestApp
end

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

tracer = Datadog::Tracer.new(writer: @writer, enabled: true)
Datadog.configuration[:sinatra][:tracer] = tracer

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
@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 /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
@use_multi_app = true
enable_script_names!

get '/one/endpoint'
get '/two/endpoint'

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

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

spans.last.tap do |span|
assert_equal('GET /two/endpoint', span.resource)
end
end
end
11 changes: 11 additions & 0 deletions test/contrib/sinatra/second_test_app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'contrib/sinatra/tracer_test_base'

class MultiAppTest < TracerTestBase
class SecondTestApp < Sinatra::Base
register Datadog::Contrib::Sinatra::Tracer

get '/endpoint' do
'2'
end
end
end