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

Extra testing from W3C spec #2460

Merged
merged 2 commits into from
Dec 5, 2022
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
7 changes: 5 additions & 2 deletions lib/datadog/tracing/distributed/trace_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,11 @@ def extract_traceparent(traceparent)
def parse_traceparent_string(traceparent)
return unless traceparent

version, trace_id, parent_id, trace_flags = traceparent.split('-')
return unless version == SPEC_VERSION
version, trace_id, parent_id, trace_flags, extra = traceparent.strip.split('-')

return if extra # Extra fields are not valid
return unless version == SPEC_VERSION # Different version
return if trace_id.size != 32 || parent_id.size != 16 || trace_flags.size != 2 # Invalid field sizes

[Integer(trace_id, 16), Integer(parent_id, 16), Integer(trace_flags, 16)]
rescue ArgumentError # Conversion to integer failed
Expand Down
53 changes: 45 additions & 8 deletions spec/datadog/tracing/distributed/trace_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,26 +263,55 @@
{ prepare_key['traceparent'] => traceparent,
prepare_key['tracestate'] => tracestate }
end
let(:traceparent) { '00-00000000000000000000000000c0ffee-0000000000000bee-00' }
let(:traceparent) { "#{version}-#{trace_id}-#{parent_id}-#{trace_flags}" }
let(:version) { '00' }
let(:trace_id) { '00000000000000000000000000c0ffee' }
let(:parent_id) { '0000000000000bee' }
let(:trace_flags) { '00' }
let(:tracestate) { '' }

let(:digest) { extract }

context 'with traceparent fields with incorrect sizes' do
context 'version with incorrect size' do
let(:version) { '0' }
it { is_expected.to be_nil }
end

context 'trace_id with incorrect size' do
let(:trace_id) { 'c0ffee' }
it { is_expected.to be_nil }
end

context 'parent_id with incorrect size' do
let(:parent_id) { 'fee' }
it { is_expected.to be_nil }
end

context 'trace flags with incorrect size' do
let(:trace_flags) { '0' }
it { is_expected.to be_nil }
end

context 'more fields than expected' do
let(:traceparent) { '00-00000000000000000000000000c0ffee-0000000000000bee-01-FFFFF' }
it { is_expected.to be_nil }
end
end

context 'without data' do
let(:data) { {} }
it { is_expected.to be nil }
end

context 'with valid trace_id and parent_id' do
let(:traceparent) { '00-00000000000000000000000000c0ffee-0000000000000bee-00' }

it { expect(digest.trace_id).to eq(0xC0FFEE) }
it { expect(digest.span_id).to eq(0xBEE) }
it { expect(digest.trace_origin).to be nil }
it { expect(digest.trace_sampling_priority).to eq(0) }

context 'and trace_id larger than 64 bits' do
let(:traceparent) { '00-ace00000000000000000000000c0ffee-0000000000000bee-00' }
let(:trace_id) { 'ace00000000000000000000000c0ffee' }

it { expect(digest.trace_id).to eq(0xC0FFEE) }
it { expect(digest.trace_distributed_id).to eq(0xACE00000000000000000000000C0FFEE) }
Expand All @@ -306,7 +335,7 @@
expected_priority = args[:expected_priority]

context "with sampled flag #{sampled_flag} and incoming sampling priority #{priority.inspect}" do
let(:traceparent) { "00-00000000000000000000000000c0ffee-0000000000000bee-0#{sampled_flag}" }
let(:trace_flags) { "0#{sampled_flag}" }
let(:tracestate) { "dd=s:#{priority}" }

it "return sampling priority #{expected_priority}" do
Expand Down Expand Up @@ -362,18 +391,26 @@
end

context 'but version not 00' do
let(:traceparent) { '01-00000000000000000000000000c0ffee-0000000000000bee-00' }
let(:version) { '01' }
it { is_expected.to be nil }
end

context 'trailing whitespace' do
let(:traceparent) { '00-00000000000000000000000000c0ffee-0000000000000bee-00 ' }

it { expect(digest.trace_id).to eq(0xC0FFEE) }
it { expect(digest.span_id).to eq(0xBEE) }
it { expect(digest.trace_sampling_priority).to eq(0) }
end
end

context 'with only trace_id' do
let(:traceparent) { '00-00000000000000000000000000c0ffee-0000000000000000-00' }
let(:parent_id) { '0000000000000000' }
it { is_expected.to be nil }
end

context 'with only parent_id' do
let(:traceparent) { '00-00000000000000000000000000000000-0000000000000bee-00' }
let(:trace_id) { '00000000000000000000000000000000' }
it { is_expected.to be nil }
end
end
Expand Down