Skip to content

Commit

Permalink
Extract trace id format helper method (#3670)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu authored May 29, 2024
1 parent 2aa8bb2 commit cf95fce
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/datadog/tracing/correlation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ def to_log_format
end

def trace_id
if Datadog.configuration.tracing.trace_id_128_bit_logging_enabled &&
!Tracing::Utils::TraceId.to_high_order(@trace_id).zero?
Kernel.format('%032x', @trace_id)
else
Tracing::Utils::TraceId.to_low_order(@trace_id).to_s
end
Correlation.format_trace_id(@trace_id)
end
end

Expand All @@ -97,6 +92,15 @@ def identifier_from_digest(digest)
trace_id: digest.trace_id,
)
end

def format_trace_id(trace_id)
if Datadog.configuration.tracing.trace_id_128_bit_logging_enabled &&
!Tracing::Utils::TraceId.to_high_order(trace_id).zero?
Kernel.format('%032x', trace_id)
else
Tracing::Utils::TraceId.to_low_order(trace_id).to_s
end
end
end
end
end
68 changes: 68 additions & 0 deletions spec/datadog/tracing/correlation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,74 @@
end
end

describe '#trace_id' do
context 'when 128 bit trace id logging is not enabled' do
before do
allow(Datadog.configuration.tracing).to receive(:trace_id_128_bit_logging_enabled).and_return(false)
end

context 'when given 64 bit trace id' do
it 'returns to lower 64 bits of trace id' do
trace_id = 0xaaaaaaaaaaaaaaaa

result = described_class.format_trace_id(trace_id)

# `0xaaaaaaaaaaaaaaaa.to_s` => '12297829382473034410'
expect(result).to eq('12297829382473034410')
end
end

context 'when given 128 bit trace id' do
it 'returns to lower 64 bits of trace id' do
trace_id = 0xaaaaaaaaaaaaaaaaffffffffffffffff

result = described_class.format_trace_id(trace_id)

# `0xffffffffffffffff.to_s` => '18446744073709551615'
expect(result).to eq('18446744073709551615')
end
end
end

context 'when 128 bit trace id logging is enabled' do
before do
allow(Datadog.configuration.tracing).to receive(:trace_id_128_bit_logging_enabled).and_return(true)
end

context 'when given 64 bit trace id' do
it 'returns lower 64 bits of trace id' do
trace_id = 0xaaaaaaaaaaaaaaaa

result = described_class.format_trace_id(trace_id)

# `0xaaaaaaaaaaaaaaaa.to_s` => '12297829382473034410'
expect(result).to eq('12297829382473034410')
end
end

context 'when given > 64 bit trace id' do
it 'returns the entire trace id in hex encoded and zero padded format' do
trace_id = 0x00ffffffffffffffaaaaaaaaaaaaaaaa

result = described_class.format_trace_id(trace_id)

expect(result).to eq('00ffffffffffffffaaaaaaaaaaaaaaaa')
end
end

context 'when given > 64 bit trace id but high order is 0' do
it 'returns to lower 64 bits of trace id' do
trace_id = 0x00000000000000000aaaaaaaaaaaaaaaa

result = described_class.format_trace_id(trace_id)

# `0xaaaaaaaaaaaaaaaa.to_s` => '12297829382473034410'
expect(result).to eq('12297829382473034410')
end
end
end
end

describe described_class::Identifier do
describe '#new' do
context 'given no arguments' do
Expand Down

0 comments on commit cf95fce

Please sign in to comment.