Skip to content

Commit

Permalink
Fixed: Rack http_server.queue spans not being added to distributed tr…
Browse files Browse the repository at this point in the history
…aces.
  • Loading branch information
delner committed Mar 7, 2019
1 parent fe4f7f7 commit 296a107
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/ddtrace/contrib/rack/middlewares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ def call(env)
# retrieve integration settings
tracer = configuration[:tracer]

# Extract distributed tracing context before creating any spans,
# so that all spans will be added to the distributed trace.
if configuration[:distributed_tracing]
context = HTTPPropagator.extract(env)
tracer.provider.context = context if context.trace_id
end

# [experimental] create a root Span to keep track of frontend web servers
# (i.e. Apache, nginx) if the header is properly set
frontend_span = compute_queue_time(env, tracer)
Expand All @@ -53,11 +60,6 @@ def call(env)
span_type: Datadog::Ext::HTTP::TYPE
}

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

# start a new request span and attach it to the current Rack environment;
# we must ensure that the span `resource` is set later
request_span = tracer.trace(Ext::SPAN_REQUEST, trace_options)
Expand Down
26 changes: 26 additions & 0 deletions spec/ddtrace/contrib/rack/distributed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@
context 'with distributed tracing headers' do
include_context 'distributed tracing headers'
it_behaves_like 'a Rack request with distributed tracing'

context 'and request_queuing is enabled' do
let(:rack_options) { super().merge(request_queuing: true, web_service_name: web_service_name) }
let(:web_service_name) { 'frontend_web_server' }

let(:server_span) { spans.first }
let(:rack_span) { spans.last }

before(:each) do
header 'X-Request-Start', "t=#{Time.now.to_f}"
end

it 'contains a request_queuing span that belongs to the distributed trace' do
is_expected.to be_ok
expect(spans).to have(2).items

expect(server_span.name).to eq('http_server.queue')
expect(server_span.trace_id).to eq(trace_id)
expect(server_span.parent_id).to eq(parent_id)
expect(server_span.get_metric(Datadog::Ext::DistributedTracing::SAMPLING_PRIORITY_KEY)).to eq(sampling_priority)

expect(rack_span.name).to eq('rack.request')
expect(rack_span.trace_id).to eq(trace_id)
expect(rack_span.parent_id).to eq(server_span.span_id)
end
end
end

context 'without distributed tracing headers' do
Expand Down

0 comments on commit 296a107

Please sign in to comment.