diff --git a/lib/ddtrace/contrib/rack/middlewares.rb b/lib/ddtrace/contrib/rack/middlewares.rb index 1092f81b1a..16feb88b8d 100644 --- a/lib/ddtrace/contrib/rack/middlewares.rb +++ b/lib/ddtrace/contrib/rack/middlewares.rb @@ -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) @@ -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) diff --git a/spec/ddtrace/contrib/rack/distributed_spec.rb b/spec/ddtrace/contrib/rack/distributed_spec.rb index de13aa804b..0ff367ec3e 100644 --- a/spec/ddtrace/contrib/rack/distributed_spec.rb +++ b/spec/ddtrace/contrib/rack/distributed_spec.rb @@ -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