Skip to content

Commit

Permalink
Merge pull request #502 from DataDog/bugfix/prevent_http_propagation_…
Browse files Browse the repository at this point in the history
…nil_context

Prevent HttpPropagator errors given a nil Context
  • Loading branch information
delner authored Aug 6, 2018
2 parents 438ce64 + 1dce53a commit 32f36f0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/ddtrace/propagation/http_propagator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ module HTTPPropagator

# inject! popolates the env with span ID, trace ID and sampling priority
def self.inject!(context, env)
# Prevent propagation from being attempted if context provided is nil.
if context.nil?
Datadog::Tracer.log.debug('Cannot inject context into env to propagate over HTTP: context is nil.'.freeze)
return
end

env[HTTP_HEADER_TRACE_ID] = context.trace_id.to_s
env[HTTP_HEADER_PARENT_ID] = context.span_id.to_s
env[HTTP_HEADER_SAMPLING_PRIORITY] = context.sampling_priority.to_s
Expand Down
9 changes: 9 additions & 0 deletions spec/ddtrace/propagation/http_propagator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
describe '#inject!' do
let(:env) { { 'something' => 'alien' } }

context 'given a nil context' do
it do
tracer.trace('caller') do |_span|
Datadog::HTTPPropagator.inject!(nil, env)
expect(env).to eq('something' => 'alien')
end
end
end

context 'given a context and env' do
context 'without any explicit sampling priority' do
it do
Expand Down
45 changes: 45 additions & 0 deletions spec/ddtrace/propagation/propagation_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'spec_helper'

require 'ddtrace'
require 'ddtrace/propagation/http_propagator'

RSpec.describe 'Context propagation' do
let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) }

describe 'when max context size is exceeded' do
let(:max_size) { 3 }

before(:each) { stub_const('Datadog::Context::DEFAULT_MAX_LENGTH', max_size) }

# Creates scenario for when size is exceeded, and yields.
# (Would rather use #around but it doesn't support stubs.)
def on_size_exceeded
tracer.trace('operation.parent') do
# Fill the trace over the capacity of the context
max_size.times do |i|
tracer.trace('operation.sibling') do |span|
yield(span) if i + 1 == max_size
end
end
end
end

context 'and the context is injected via HTTP propagation' do
let(:env) { {} }

it 'does not raise an error or propagate the trace' do
on_size_exceeded do |span|
# Verify warning message is produced.
allow(Datadog::Tracer.log).to receive(:debug)
expect { Datadog::HTTPPropagator.inject!(span.context, env) }.to_not raise_error
expect(Datadog::Tracer.log).to have_received(:debug).with(/Cannot inject context/)

# The context has reached its max size and cannot be propagated.
# Check headers aren't present.
expect(env).to_not include(Datadog::HTTPPropagator::HTTP_HEADER_TRACE_ID)
expect(env).to_not include(Datadog::HTTPPropagator::HTTP_HEADER_PARENT_ID)
end
end
end
end
end

0 comments on commit 32f36f0

Please sign in to comment.