Skip to content

Commit

Permalink
Merge pull request #3729 from DataDog/fix-tid
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc authored Jun 26, 2024
2 parents 5c779a0 + d03e37d commit 7cd7c4c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/tracing/distributed/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def build_tags(digest)

return tags if high_order == 0

tags.merge(Tracing::Metadata::Ext::Distributed::TAG_TID => high_order.to_s(16))
tags.merge(Tracing::Metadata::Ext::Distributed::TAG_TID => format('%016x', high_order))
end

# Side effect: Remove high order 64 bit hex-encoded `tid` tag from distributed tags
Expand Down
5 changes: 4 additions & 1 deletion lib/datadog/tracing/trace_segment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,13 @@ def sampled?
|| sampling_priority == Sampling::Ext::Priority::USER_KEEP
end

# Returns the high order part of the trace id as a hexadecimal string; the most significant 64 bits.
# The String returned is padded with zeros, having a fixed length of 16 characters.
# If the high order part is zero, it returns nil.
def high_order_tid
high_order = Tracing::Utils::TraceId.to_high_order(@id)

high_order.to_s(16) if high_order != 0
format('%016x', high_order) if high_order != 0
end

protected
Expand Down
8 changes: 4 additions & 4 deletions spec/datadog/tracing/distributed/datadog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
context 'when given a trace digest with 128 bit trace id' do
let(:digest) do
Datadog::Tracing::TraceDigest.new(
trace_id: 0xaaaaaaaaaaaaaaaaffffffffffffffff,
trace_id: 0x0aaaaaaaaaaaaaaaffffffffffffffff,
span_id: 0xbbbbbbbbbbbbbbbb
)
end
Expand All @@ -220,7 +220,7 @@
expect(data).to eq(
'x-datadog-trace-id' => 0xffffffffffffffff.to_s,
'x-datadog-parent-id' => 0xbbbbbbbbbbbbbbbb.to_s,
'x-datadog-tags' => '_dd.p.tid=aaaaaaaaaaaaaaaa'
'x-datadog-tags' => '_dd.p.tid=0aaaaaaaaaaaaaaa'
)
end
end
Expand Down Expand Up @@ -495,11 +495,11 @@
{
prepare_key['x-datadog-trace-id'] => 0xffffffffffffffff.to_s,
prepare_key['x-datadog-parent-id'] => 0xbbbbbbbbbbbbbbbb.to_s,
prepare_key['x-datadog-tags'] => '_dd.p.tid=aaaaaaaaaaaaaaaa'
prepare_key['x-datadog-tags'] => '_dd.p.tid=0aaaaaaaaaaaaaaa'
}
end

it { expect(digest.trace_id).to eq(0xaaaaaaaaaaaaaaaaffffffffffffffff) }
it { expect(digest.trace_id).to eq(0x0aaaaaaaaaaaaaaaffffffffffffffff) }
it { expect(digest.span_id).to eq(0xbbbbbbbbbbbbbbbb) }
it { expect(digest.trace_distributed_tags).not_to include('_dd.p.tid') }
end
Expand Down
6 changes: 3 additions & 3 deletions spec/datadog/tracing/trace_segment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@
end

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

it do
expect(trace_segment.high_order_tid).to eq('aaaaaaaaaaaaaaaa')
it 'returns a fixed-size, 16-char hex string' do
expect(trace_segment.high_order_tid).to eq('0aaaaaaaaaaaaaaa')
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/datadog/tracing/transport/trace_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
RSpec.describe Datadog::Tracing::Transport::TraceFormatter do
subject(:trace_formatter) { described_class.new(trace) }
let(:trace_options) { { id: trace_id } }
let(:trace_id) { Datadog::Tracing::Utils::TraceId.next_id }
let(:trace_id) { 0xa3efc9f3333333334d39dacf84ab3fe }

shared_context 'trace metadata' do
let(:trace_tags) do
Expand Down Expand Up @@ -199,7 +199,7 @@
{
'foo' => 'bar',
'_dd.p.dm' => '-1',
'_dd.p.tid' => high_order_hex_trace_id(trace_id),
'_dd.p.tid' => '0a3efc9f33333333',
}
)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/tracer_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ def low_order_trace_id(trace_id)
# Wraps call to Tracing::Utils::TraceId.to_high_order and converts to hex
# for better test readability
def high_order_hex_trace_id(trace_id)
Datadog::Tracing::Utils::TraceId.to_high_order(trace_id).to_s(16)
format('%016x', Datadog::Tracing::Utils::TraceId.to_high_order(trace_id))
end
end

0 comments on commit 7cd7c4c

Please sign in to comment.