From ea25784bde6bac1f1822fde0c1659f0967fc1353 Mon Sep 17 00:00:00 2001 From: David Elner Date: Mon, 30 Jul 2018 14:14:43 -0400 Subject: [PATCH 1/2] Fixed: HttpPropagator#inject! raising error when nil Context given. --- lib/ddtrace/propagation/http_propagator.rb | 6 ++++++ spec/ddtrace/propagation/http_propagator_spec.rb | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/ddtrace/propagation/http_propagator.rb b/lib/ddtrace/propagation/http_propagator.rb index ed6270335e..cb1ba2bb0f 100644 --- a/lib/ddtrace/propagation/http_propagator.rb +++ b/lib/ddtrace/propagation/http_propagator.rb @@ -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 diff --git a/spec/ddtrace/propagation/http_propagator_spec.rb b/spec/ddtrace/propagation/http_propagator_spec.rb index 6f51c0118c..1bbf84db71 100644 --- a/spec/ddtrace/propagation/http_propagator_spec.rb +++ b/spec/ddtrace/propagation/http_propagator_spec.rb @@ -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 From 1dce53a04f66d828747e5e9bb744a1e9bb49604d Mon Sep 17 00:00:00 2001 From: David Elner Date: Mon, 30 Jul 2018 17:31:36 -0400 Subject: [PATCH 2/2] Added: Context propagation integration spec. --- .../propagation_integration_spec.rb | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 spec/ddtrace/propagation/propagation_integration_spec.rb diff --git a/spec/ddtrace/propagation/propagation_integration_spec.rb b/spec/ddtrace/propagation/propagation_integration_spec.rb new file mode 100644 index 0000000000..51197163e8 --- /dev/null +++ b/spec/ddtrace/propagation/propagation_integration_spec.rb @@ -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