Skip to content

Commit

Permalink
fix: XRay trace_id parsing length (#730)
Browse files Browse the repository at this point in the history
* Fix XRay trace_id parsing length

* Address comments, try to fix the build

* revert gemspec
  • Loading branch information
__stan__ authored Apr 30, 2021
1 parent 1e3d8fe commit 33edd58
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ instrumentation/**/*.gemfile.lock

# Vendored gems
**/vendor/**/*

# IDE Settings
/.idea/
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def inject(carrier, context: Context.current, setter: Context::Propagation.text_
end

ot_trace_id = span_context.hex_trace_id
xray_trace_id = "1-#{ot_trace_id[0..6]}-#{ot_trace_id[7..ot_trace_id.length]}"
xray_trace_id = "1-#{ot_trace_id[0..7]}-#{ot_trace_id[8..ot_trace_id.length]}"
parent_id = span_context.hex_span_id

xray_value = "Root=#{xray_trace_id};Parent=#{parent_id};Sampled=#{sampling_state}"
Expand All @@ -98,7 +98,7 @@ def parse_header(header)
# Convert an id from a hex encoded string to byte array. Assumes the input id has already been
# validated to be 35 characters in length.
def to_trace_id(hex_id)
Array(hex_id[2..8] + hex_id[10..hex_id.length]).pack('H*')
Array(hex_id[2..9] + hex_id[11..hex_id.length]).pack('H*')
end

# Convert an id from a hex encoded string to byte array.
Expand Down
32 changes: 16 additions & 16 deletions propagator/xray/test/text_map_propagator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
describe('#extract') do
it 'extracts context with trace id, span id, sampling flag, trace state' do
parent_context = OpenTelemetry::Context.empty
carrier = { 'X-Amzn-Trace-Id' => 'Root=1-80f198e-e56343ba864fe8b2a57d3eff7;Parent=e457b5a2e4d86bd1;Sampled=1;Foo=Bar;Fizz=Buzz' }
carrier = { 'X-Amzn-Trace-Id' => 'Root=1-80f198ea-e56343ba864fe8b2a57d3eff;Parent=e457b5a2e4d86bd1;Sampled=1;Foo=Bar;Fizz=Buzz' }

context = propagator.extract(carrier, context: parent_context)
extracted_context = OpenTelemetry::Trace.current_span(context).context

_(extracted_context.hex_trace_id).must_equal('80f198ee56343ba864fe8b2a57d3eff7')
_(extracted_context.hex_trace_id).must_equal('80f198eae56343ba864fe8b2a57d3eff')
_(extracted_context.hex_span_id).must_equal('e457b5a2e4d86bd1')
_(extracted_context.trace_flags).must_equal(OpenTelemetry::Trace::TraceFlags::SAMPLED)
_(extracted_context.tracestate.to_s).must_equal(OpenTelemetry::Trace::Tracestate.from_string('Foo=Bar,Fizz=Buzz').to_s)
Expand All @@ -30,33 +30,33 @@

it 'extracts context with trace id, span id, sampling flag' do
parent_context = OpenTelemetry::Context.empty
carrier = { 'X-Amzn-Trace-Id' => 'Root=1-80f198e-e56343ba864fe8b2a57d3eff7;Parent=e457b5a2e4d86bd1;Sampled=1' }
carrier = { 'X-Amzn-Trace-Id' => 'Root=1-80f198ea-e56343ba864fe8b2a57d3eff;Parent=e457b5a2e4d86bd1;Sampled=1' }

context = propagator.extract(carrier, context: parent_context)
extracted_context = OpenTelemetry::Trace.current_span(context).context

_(extracted_context.hex_trace_id).must_equal('80f198ee56343ba864fe8b2a57d3eff7')
_(extracted_context.hex_trace_id).must_equal('80f198eae56343ba864fe8b2a57d3eff')
_(extracted_context.hex_span_id).must_equal('e457b5a2e4d86bd1')
_(extracted_context.trace_flags).must_equal(OpenTelemetry::Trace::TraceFlags::SAMPLED)
_(extracted_context).must_be(:remote?)
end

it 'extracts context with trace id, span id' do
parent_context = OpenTelemetry::Context.empty
carrier = { 'X-Amzn-Trace-Id' => 'Root=1-80f198e-e56343ba864fe8b2a57d3eff7;Parent=e457b5a2e4d86bd1' }
carrier = { 'X-Amzn-Trace-Id' => 'Root=1-80f198ea-e56343ba864fe8b2a57d3eff;Parent=e457b5a2e4d86bd1' }

context = propagator.extract(carrier, context: parent_context)
extracted_context = OpenTelemetry::Trace.current_span(context).context

_(extracted_context.hex_trace_id).must_equal('80f198ee56343ba864fe8b2a57d3eff7')
_(extracted_context.hex_trace_id).must_equal('80f198eae56343ba864fe8b2a57d3eff')
_(extracted_context.hex_span_id).must_equal('e457b5a2e4d86bd1')
_(extracted_context.trace_flags).must_equal(OpenTelemetry::Trace::TraceFlags::DEFAULT)
_(extracted_context).must_be(:remote?)
end

it 'converts debug flag to sampled' do
parent_context = OpenTelemetry::Context.empty
carrier = { 'X-Amzn-Trace-Id' => 'Root=1-80f198e-e56343ba864fe8b2a57d3eff7;Parent=e457b5a2e4d86bd1;Sampled=d' }
carrier = { 'X-Amzn-Trace-Id' => 'Root=1-80f198ea-e56343ba864fe8b2a57d3eff;Parent=e457b5a2e4d86bd1;Sampled=d' }

context = propagator.extract(carrier, context: parent_context)
extracted_context = OpenTelemetry::Trace.current_span(context).context
Expand All @@ -66,7 +66,7 @@

it 'handles malformed trace id' do
parent_context = OpenTelemetry::Context.empty
carrier = { 'X-Amzn-Trace-Id' => 'Root=180f198e-e56343ba864fe8b2a57d3eff7;Parent=e457b5a2e4d86bd1;Sampled=1' }
carrier = { 'X-Amzn-Trace-Id' => 'Root=180f198ea-e56343ba864fe8b2a57d3eff;Parent=e457b5a2e4d86bd1;Sampled=1' }

context = propagator.extract(carrier, context: parent_context)

Expand All @@ -75,7 +75,7 @@

it 'handles malformed span id' do
parent_context = OpenTelemetry::Context.empty
carrier = { 'X-Amzn-Trace-Id' => 'Root=1-80f198e-e56343ba864fe8b2a57d3eff7;Parent=457b5a2e4d86bd1;Sampled=1' }
carrier = { 'X-Amzn-Trace-Id' => 'Root=1-80f198ea-e56343ba864fe8b2a57d3eff;Parent=457b5a2e4d86bd1;Sampled=1' }

context = propagator.extract(carrier, context: parent_context)

Expand All @@ -86,43 +86,43 @@
describe '#inject' do
it 'injects context with sampled trace flags' do
context = create_context(
trace_id: '80f198ee56343ba864fe8b2a57d3eff7',
trace_id: '80f198eae56343ba864fe8b2a57d3eff',
span_id: 'e457b5a2e4d86bd1',
trace_flags: TraceFlags::SAMPLED
)

carrier = {}
propagator.inject(carrier, context: context)

expected_xray = 'Root=1-80f198e-e56343ba864fe8b2a57d3eff7;Parent=e457b5a2e4d86bd1;Sampled=1'
expected_xray = 'Root=1-80f198ea-e56343ba864fe8b2a57d3eff;Parent=e457b5a2e4d86bd1;Sampled=1'
_(carrier['X-Amzn-Trace-Id']).must_equal(expected_xray)
end

it 'injects context with default trace flags' do
context = create_context(
trace_id: '80f198ee56343ba864fe8b2a57d3eff7',
trace_id: '80f198eae56343ba864fe8b2a57d3eff',
span_id: 'e457b5a2e4d86bd1',
trace_flags: TraceFlags::DEFAULT
)

carrier = {}
propagator.inject(carrier, context: context)

expected_xray = 'Root=1-80f198e-e56343ba864fe8b2a57d3eff7;Parent=e457b5a2e4d86bd1;Sampled=0'
expected_xray = 'Root=1-80f198ea-e56343ba864fe8b2a57d3eff;Parent=e457b5a2e4d86bd1;Sampled=0'
_(carrier['X-Amzn-Trace-Id']).must_equal(expected_xray)
end

it 'injects debug flag when present' do
context = create_context(
trace_id: '80f198ee56343ba864fe8b2a57d3eff7',
trace_id: '80f198eae56343ba864fe8b2a57d3eff',
span_id: 'e457b5a2e4d86bd1',
xray_debug: true
)

carrier = {}
propagator.inject(carrier, context: context)

expected_xray = 'Root=1-80f198e-e56343ba864fe8b2a57d3eff7;Parent=e457b5a2e4d86bd1;Sampled=d'
expected_xray = 'Root=1-80f198ea-e56343ba864fe8b2a57d3eff;Parent=e457b5a2e4d86bd1;Sampled=d'
_(carrier['X-Amzn-Trace-Id']).must_equal(expected_xray)
end

Expand All @@ -140,7 +140,7 @@

it 'no-ops if span id invalid' do
context = create_context(
trace_id: '80f198ee56343ba864fe8b2a57d3eff7',
trace_id: '80f198eae56343ba864fe8b2a57d3eff',
span_id: '0' * 16
)

Expand Down

0 comments on commit 33edd58

Please sign in to comment.