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

Fix Rack http_server.queue spans missing from distributed traces #709

Merged
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
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]
delner marked this conversation as resolved.
Show resolved Hide resolved
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