Skip to content

Commit

Permalink
fix: ensure the correct ids are serialized and propagated
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdinur committed Jun 12, 2024
1 parent e086475 commit 55624f3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
13 changes: 3 additions & 10 deletions lib/datadog/opentelemetry/sdk/propagator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ def extract(
digest = @datadog_propagator.extract(carrier)
return context unless digest

trace_id = to_otel_id(digest.trace_id)
span_id = to_otel_id(digest.span_id)
# Converts the {Numeric} Datadog id object to OpenTelemetry's byte array format.
trace_id = [format('%032x', digest.trace_id)].pack('H32')
span_id = [format('%016x', digest.trace_id)].pack('H16')

if digest.trace_state || digest.trace_flags
trace_flags = ::OpenTelemetry::Trace::TraceFlags.from_byte(digest.trace_flags)
Expand Down Expand Up @@ -78,14 +79,6 @@ def extract(
def fields
[]
end

private

# Converts the {Numeric} Datadog id object to OpenTelemetry's byte array format.
# This method currently converts an unsigned 64-bit Integer to a binary String.
def to_otel_id(dd_id)
Array(dd_id).pack('Q')
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/opentelemetry/sdk/span_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def start_datadog_span(span)
name, kwargs = span_arguments(span, attributes)

datadog_span = Tracing.trace(name, **kwargs)
# The Datadog span must have the same ID as the OpenTelemetry span
# DEV: We need to set the span ID after the span is created
datadog_span.id = span.context.hex_span_id.to_i(16)
datadog_span.trace_id = span.context.hex_trace_id.to_i(16)

datadog_span.set_error([nil, span.status.description]) unless span.status.ok?
datadog_span.set_tags(span.attributes)
Expand Down
18 changes: 8 additions & 10 deletions lib/datadog/tracing/span_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,8 @@ class SpanOperation

# Span attributes
# NOTE: In the future, we should drop the me
attr_reader \
:end_time,
:id,
:name,
:parent_id,
:resource,
:service,
:start_time,
:trace_id,
:type
attr_accessor :id, :trace_id
attr_reader :end_time, :name, :parent_id, :resource, :service, :start_time, :type
attr_accessor :links, :status

def initialize(
Expand Down Expand Up @@ -421,6 +413,12 @@ def message
end
end

# Provides a mechanism to override the automatically generated span ID
# @param id [Integer] the span ID

# Provides a mechanism to override the automatically generated trace ID
# @param trace_id [Integer] the trace ID

private

# Keep span reference private: we don't want users
Expand Down
15 changes: 13 additions & 2 deletions spec/datadog/opentelemetry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@
expect(parent).to be_root_span
expect(child.parent_id).to eq(parent.id)
end

it 'the underlying datadog spans has the same span_id as the otel spans' do
existing_span.finish
start_span.finish
# parent otel span generates a Datadog span with the same ids
expect(existing_span.context.hex_trace_id.to_i(16)).to eq(parent.trace_id)
expect(existing_span.context.hex_span_id.to_i(16)).to eq(parent.id)
# child otel span generates a Datadog span with the same ids
expect(start_span.context.hex_trace_id.to_i(16)).to eq(child.trace_id)
expect(start_span.context.hex_span_id.to_i(16)).to eq(child.id)
end
end
end

Expand Down Expand Up @@ -802,7 +813,7 @@ def headers
context 'with TraceContext headers' do
let(:carrier) do
{
'traceparent' => '00-00000000000000001111111111111111-2222222222222222-01'
'traceparent' => '00-11111111111111111111111111111111-2222222222222222-01'
}
end

Expand All @@ -817,7 +828,7 @@ def headers
otel_tracer.in_span('otel') {}
end

expect(span.trace_id).to eq(0x00000000000000001111111111111111)
expect(span.trace_id).to eq(0x11111111111111111111111111111111)
expect(span.parent_id).to eq(0x2222222222222222)
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/datadog/tracing/span_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,24 @@
end
end

describe '#id=' do
subject!(:id=) { span_op.id = id }

context 'with a valid 64 bit integer' do
let(:id) { 2 ^ 64 - 1 }
it { expect(span_op.id).to eq(id) }
end
end

describe '#trace_id=' do
subject!(:trace_id=) { span_op.trace_id = trace_id }

context 'with a valid 128 bit integer' do
let(:trace_id) { 2 ^ 128 - 1 }
it { expect(span_op.trace_id).to eq(trace_id) }
end
end

describe '#measure' do
subject(:measure) { span_op.measure(&block) }

Expand Down

0 comments on commit 55624f3

Please sign in to comment.