Skip to content

Commit

Permalink
Remove lenient conversion #to_i for Utils::TraceId
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Mar 3, 2023
1 parent 15d1cc6 commit 0aab0da
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
6 changes: 3 additions & 3 deletions lib/datadog/tracing/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def next_id
end

def to_high_order(trace_id)
trace_id.to_i >> 64
trace_id >> 64
end

def to_low_order(trace_id)
trace_id.to_i & 0xFFFFFFFFFFFFFFFF
trace_id & 0xFFFFFFFFFFFFFFFF
end

def concatenate(high_order, low_order)
high_order.to_i << 64 | low_order.to_i
high_order << 64 | low_order
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion spec/datadog/tracing/distributed/datadog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@
end

context 'with trace_distributed_tags' do
let(:digest) { Datadog::Tracing::TraceDigest.new(trace_distributed_tags: tags) }
let(:digest) do
Datadog::Tracing::TraceDigest.new(
trace_id: require 'datadog/tracing/trace_segment',
trace_distributed_tags: tags
)
end

context 'nil' do
let(:tags) { nil }
Expand Down
15 changes: 7 additions & 8 deletions spec/datadog/tracing/trace_segment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
require 'datadog/tracing/sampling/ext'
require 'datadog/tracing/span'
require 'datadog/tracing/trace_segment'
require 'datadog/tracing/utils'

RSpec.describe Datadog::Tracing::TraceSegment do
subject(:trace_segment) { described_class.new(spans, **options) }
let(:trace_id) { Datadog::Tracing::Utils::TraceId.next_id }
subject(:trace_segment) { described_class.new(spans, **options.merge(id: trace_id)) }
let(:options) { {} }

let(:spans) do
Array.new(3) do |i|
span = Datadog::Tracing::Span.new(
'job.work',
trace_id: trace_id,
resource: 'generate_report',
service: 'jobs-worker',
type: 'worker'
Expand All @@ -30,7 +33,7 @@
is_expected.to have_attributes(
agent_sample_rate: nil,
hostname: nil,
id: nil,
id: trace_id,
lang: nil,
name: nil,
origin: nil,
Expand Down Expand Up @@ -343,20 +346,16 @@
end

describe '#high_order_tid' do
it do
expect(trace_segment.high_order_tid).to eq(nil)
end

context 'when given 64 bits id' do
let(:options) { { id: 0xffffffffffffffff } }
let(:trace_id) { 0xffffffffffffffff }

it do
expect(trace_segment.high_order_tid).to eq(nil)
end
end

context 'when given 128 bits id' do
let(:options) { { id: 0xaaaaaaaaaaaaaaaaffffffffffffffff } }
let(:trace_id) { 0xaaaaaaaaaaaaaaaaffffffffffffffff }

it do
expect(trace_segment.high_order_tid).to eq('aaaaaaaaaaaaaaaa')
Expand Down
8 changes: 4 additions & 4 deletions spec/datadog/tracing/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
context 'when given <= 64 bit' do
[
0xaaaaaaaaaaaaaaaa,
0xffffffffffffffff
0xffffffffffffffff,
0,
].each do |input|
it 'returns itself' do
expect(described_class.to_low_order(input)).to eq(input)
Expand All @@ -64,7 +65,7 @@
[
0xaaaaaaaaaaaaaaaa,
0xffffffffffffffff,
nil
0,
].each do |input|
it 'returns 0' do
expect(described_class.to_high_order(input)).to eq(0)
Expand Down Expand Up @@ -92,9 +93,8 @@
[0xffffffffffffffff, 0xaaaaaaaaaaaaaaaa] => 0xffffffffffffffffaaaaaaaaaaaaaaaa,
[0x00000000aaaaaaaa, 0xffffffffffffffff] => 0x00000000aaaaaaaaffffffffffffffff,
[0xaaaaaaaaaaaaaaaa, 0xffffffff] => 0xaaaaaaaaaaaaaaaa00000000ffffffff,
[nil, 0xffffffffffffffff] => 0xffffffffffffffff,
[0, 0xffffffffffffffff] => 0xffffffffffffffff,
['0', 0xffffffffffffffff] => 0xffffffffffffffff,
[0xaaaaaaaaaaaaaaaa, 0] => 0xaaaaaaaaaaaaaaaa0000000000000000,
}.each do |(high_order, low_order), result|
context "when given `#{high_order}` and `#{low_order}`" do
it "returns `0x#{result.to_s(16)}`" do
Expand Down
4 changes: 3 additions & 1 deletion spec/ddtrace/transport/trace_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

RSpec.describe Datadog::Transport::TraceFormatter do
subject(:trace_formatter) { described_class.new(trace) }
let(:trace_options) { {} }
let(:trace_options) { { id: trace_id } }
let(:trace_id) { Datadog::Tracing::Utils::TraceId.next_id }

shared_context 'trace metadata' do
let(:trace_tags) do
Expand All @@ -21,6 +22,7 @@

let(:trace_options) do
{
id: trace_id,
resource: resource,
agent_sample_rate: agent_sample_rate,
hostname: hostname,
Expand Down

0 comments on commit 0aab0da

Please sign in to comment.