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 ff0bad4 commit 50fb96a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/ddtrace/contrib/rack/middlewares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ def call(env)
# retrieve integration settings
tracer = configuration[:tracer]

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 +58,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 @@ -85,6 +85,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 50fb96a

Please sign in to comment.