Skip to content

Commit

Permalink
Added: distributed_tracing option to Sinatra.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Jan 24, 2018
1 parent c804129 commit a1b6180
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Where `options` is an optional `Hash` that accepts the following parameters:
| --- | --- | --- |
| ``service_name`` | Service name used for `sinatra` instrumentation | sinatra |
| ``resource_script_names`` | Prepend resource names with script name | ``false`` |
| ``distributed_tracing`` | Enables [distributed tracing](#Distributed_Tracing) so that this service trace is connected with a trace of another service if tracing headers are received | `false` |
| ``tracer`` | A ``Datadog::Tracer`` instance used to instrument the application. Usually you don't need to set that. | ``Datadog.tracer`` |

### Rack
Expand Down
8 changes: 7 additions & 1 deletion lib/ddtrace/contrib/sinatra/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'ddtrace/ext/app_types'
require 'ddtrace/ext/errors'
require 'ddtrace/ext/http'
require 'ddtrace/propagation/http_propagator'

sinatra_vs = Gem::Version.new(Sinatra::VERSION)
sinatra_min_vs = Gem::Version.new('1.4.0')
Expand All @@ -29,8 +30,8 @@ module Tracer
end

option :tracer, default: Datadog.tracer

option :resource_script_names, default: false
option :distributed_tracing, default: false

def route(verb, action, *)
# Keep track of the route name when the app is instantiated for an
Expand Down Expand Up @@ -83,6 +84,11 @@ def render(engine, data, *)

tracer = Datadog.configuration[:sinatra][:tracer]

if Datadog.configuration[:sinatra][:distributed_tracing]
context = HTTPPropagator.extract(request.env)
tracer.provider.context = context if context.trace_id
end

span = tracer.trace('sinatra.request',
service: Datadog.configuration[:sinatra][:service_name],
span_type: Datadog::Ext::HTTP::TYPE)
Expand Down
26 changes: 26 additions & 0 deletions test/contrib/sinatra/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ def test_request
assert_nil(span.parent)
end

def test_distributed_request
# Enable distributed tracing
Datadog.configuration.use(:sinatra, distributed_tracing: true)

response = get '/request', {}, {
'HTTP_X_DATADOG_TRACE_ID' => '1',
'HTTP_X_DATADOG_PARENT_ID' => '2',
'HTTP_X_DATADOG_SAMPLING_PRIORITY' => Datadog::Ext::Priority::USER_KEEP.to_s
}

assert_equal(200, response.status)

# Check spans
spans = @writer.spans
assert_equal(1, spans.length)

# Check span
span = spans[0]
assert_equal(1, span.trace_id)
assert_equal(2, span.parent_id)
assert_equal(2.0, span.get_metric('_sampling_priority_v1'))
ensure
# Disable distributed tracing
Datadog.configuration.use(:sinatra, distributed_tracing: false)
end

def test_bad_request
get '/bad-request'
assert_equal(400, last_response.status)
Expand Down

0 comments on commit a1b6180

Please sign in to comment.