Skip to content

Commit

Permalink
use digest as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdinur committed Mar 21, 2024
1 parent 78c3924 commit 78e19ba
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 74 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/opentelemetry/sdk/trace/span.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require '../../../tracing/utils'
require 'datadog/tracing/utils'

module Datadog
module OpenTelemetry
Expand Down
23 changes: 9 additions & 14 deletions lib/datadog/tracing/span_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,28 @@ class SpanLink
attr_reader :trace_state

def initialize(
span_id: nil,
trace_id: nil,
attributes: nil,
trace_flags: nil,
trace_state: nil
digest: nil
)
@span_id = span_id
@trace_id = trace_id
@attributes = attributes && attributes.dup.freeze
@trace_flags = trace_flags
@trace_state = trace_state && trace_state.dup.freeze
@span_id = digest&.span_id
@trace_id = digest&.trace_id
@trace_flags = digest&.trace_flags
@trace_state = digest&.trace_state && digest&.trace_state.dup
@dropped_attributes = 0
freeze
@attributes = (attributes && attributes.dup) || {}
end

def to_hash
h = {
span_id: @span_id,
trace_id: Tracing::Utils::TraceId.to_low_order(@trace_id),

span_id: @span_id || 0,
trace_id: Tracing::Utils::TraceId.to_low_order(@trace_id) || 0,
}
# Optimization: Hash non empty attributes
if @trace_id.to_i > Tracing::Utils::EXTERNAL_MAX_ID
h[:trace_id_high] =
Tracing::Utils::TraceId.to_high_order(@trace_id)
end
if @attributes
unless @attributes&.empty?
h[:attributes] = {}
@attributes.each do |k1, v1|
Tracing::Utils.serialize_attribute(k1, v1).each do |new_k1, value|
Expand Down
6 changes: 3 additions & 3 deletions sig/datadog/tracing/span_link.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ module Datadog

@trace_id: untyped

@attributes: untyped

@trace_flags: untyped

@trace_state: untyped

@dropped_attributes: untyped

@attributes: untyped

# @!attribute [r] span_id
# Datadog id for the currently active span.
# @return [Integer]
Expand Down Expand Up @@ -45,7 +45,7 @@ module Datadog
# @see https://www.w3.org/TR/trace-context/#tracestate-header
attr_reader trace_state: untyped

def initialize: (?span_id: untyped?, ?trace_id: untyped?, ?attributes: untyped?, ?trace_flags: untyped?, ?trace_state: untyped?) -> void
def initialize: (?attributes: untyped?, ?digest: untyped?) -> void

def to_hash: () -> untyped
end
Expand Down
105 changes: 57 additions & 48 deletions spec/datadog/tracing/span_link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,116 @@
require 'support/object_helpers'

require 'datadog/tracing/span_link'
require 'datadog/tracing/trace_digest'

RSpec.describe Datadog::Tracing::SpanLink do
subject(:span_link) { described_class.new(**options) }
let(:options) { {} }
subject(:span_link) { described_class.new(attributes: attributes, digest: digest) }

let(:attributes) { nil }
let(:digest) do
Datadog::Tracing::TraceDigest.new(
span_id: span_id,
trace_id: trace_id,
trace_flags: trace_flags,
trace_state: trace_state,
)
end
let(:span_id) { nil }
let(:trace_id) { nil }
let(:trace_state) { nil }
let(:trace_flags) { nil }

describe '::new' do
context 'by default' do
let(:digest) { nil }
let(:attributes) { nil }

it do
is_expected.to have_attributes(
span_id: nil,
attributes: nil,
attributes: {},
trace_id: nil,
trace_flags: nil,
trace_state: nil,
)
end

it { is_expected.to be_frozen }
end

context 'given' do
context ':span_id' do
let(:options) { { span_id: span_id } }
let(:span_id) { Datadog::Tracing::Utils.next_id }

it { is_expected.to have_attributes(span_id: span_id) }
end

context ':attributes' do
let(:options) { { attributes: attributes } }
let(:attributes) { { tag: 'value' } }

it { is_expected.to have_attributes(attributes: be_a_frozen_copy_of(attributes)) }
end

context ':trace_id' do
let(:options) { { trace_id: trace_id } }
let(:trace_id) { Datadog::Tracing::Utils::TraceId.next_id }

it { is_expected.to have_attributes(trace_id: trace_id) }
it { is_expected.to have_attributes(attributes: attributes) }
end

context ':trace_flags' do
let(:options) { { trace_flags: trace_flags } }
let(:trace_flags) { 0x01 }

it { is_expected.to have_attributes(trace_flags: 0x01) }
end

context ':trace_state' do
let(:options) { { trace_state: trace_state } }
let(:trace_state) { 'vendor1=value,v2=v' }

it { is_expected.to have_attributes(trace_state: be_a_frozen_copy_of('vendor1=value,v2=v')) }
context ':digest with' do
context ':span_id' do
let(:span_id) { Datadog::Tracing::Utils.next_id }
it { is_expected.to have_attributes(span_id: span_id) }
end

context ':trace_id' do
let(:trace_id) { Datadog::Tracing::Utils::TraceId.next_id }
it { is_expected.to have_attributes(trace_id: trace_id) }
end

context ':trace_flags' do
let(:trace_flags) { 0x01 }
it { is_expected.to have_attributes(trace_flags: 0x01) }
end

context ':trace_state' do
let(:trace_state) { 'vendor1=value,v2=v' }
it { is_expected.to have_attributes(trace_state: 'vendor1=value,v2=v') }
end
end
end
end

describe '#to_hash' do
subject(:to_hash) { span_link.to_hash }
let(:span_id) { 34 }
let(:trace_id) { 12 }

context 'with required fields' do
let(:options) { { span_id: 34, trace_id: 12 } }

context 'when trace_id < 2^64' do
it { is_expected.to eq(trace_id: 12, span_id: 34, flags: 0) }
end

context 'when trace_id >= 2^64' do
let(:options) { { span_id: 34, trace_id: 2**64 + 12 } }
let(:trace_id) { 2**64 + 12 }
it { is_expected.to eq(trace_id: 12, trace_id_high: 1, span_id: 34, flags: 0) }
end
end

context 'with trace_state' do
let(:options) { { span_id: 34, trace_id: 12, trace_state: 'dd=s:1' } }
context 'required fields are set to nil' do
let(:span_id) { nil }
let(:trace_id) { nil }
it { is_expected.to eq(trace_id: 0, span_id: 0, flags: 0) }
end

context 'when trace_state is set' do
let(:trace_state) { 'dd=s:1' }
it { is_expected.to include(tracestate: 'dd=s:1') }
end

context 'with trace_flag' do
context 'when trace_flag is set' do
context 'when trace_flag is unset' do
let(:options) { { span_id: 34, trace_id: 12 } }
it { is_expected.to include(flags: 0) }
end

context 'when trace_flags is 0' do
let(:options) { { span_id: 34, trace_id: 12, trace_flags: 0 } }
let(:trace_flags) { 0 }
it { is_expected.to include(flags: 2147483648) }
end

context 'when trace_flag is 1' do
let(:options) { { span_id: 34, trace_id: 12, trace_flags: 1 } }
let(:trace_flags) { 1 }
it { is_expected.to include(flags: 2147483649) }
end
end

context 'with attributes' do
let(:options) do
{ span_id: 34, trace_id: 12,
attributes: { 'link.name' => :test_link, 'link.id' => 1, 'nested' => [true, [2, 3], 'val'] } }
end
context 'when attributes is set' do
let(:attributes) { { 'link.name' => :test_link, 'link.id' => 1, 'nested' => [true, [2, 3], 'val'] } }
it {
is_expected.to include(
attributes: { 'link.name' => 'test_link', 'link.id' => '1', 'nested.0' => 'true',
Expand Down
36 changes: 28 additions & 8 deletions spec/datadog/tracing/transport/serializable_trace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,26 @@
Array.new(3) do |_i|
Datadog::Tracing::Span.new(
'dummy',
links: [Datadog::Tracing::SpanLink.new(
trace_id: 0xaaaaaaaaaaaaaaaaffffffffffffffff,
span_id: 0x1,
trace_state: 'vendor1=value,v2=v,dd=s:1',
trace_flags: 0x1,
attributes: { 'link.name' => 'test_link' }
)]
links: [
Datadog::Tracing::SpanLink.new(
digest: Datadog::Tracing::TraceDigest.new(
trace_id: 0xaaaaaaaaaaaaaaaaffffffffffffffff,
span_id: 0x1,
trace_state: 'vendor1=value,v2=v,dd=s:1',
trace_flags: 0x1,
),
attributes: { 'link.name' => 'test_link' }
),
Datadog::Tracing::SpanLink.new(
digest: Datadog::Tracing::TraceDigest.new(
trace_id: 0xa0123456789abcdef,
span_id: 0x2,
),
),
Datadog::Tracing::SpanLink.new(
digest: Datadog::Tracing::TraceDigest.new,
)
],
)
end
end
Expand All @@ -103,7 +116,14 @@
'attributes' => { 'link.name' => 'test_link' },
'flags' => 2147483649,
'tracestate' => 'vendor1=value,v2=v,dd=s:1',
}]
},
{
'span_id' => 2,
'trace_id' => 0x0123456789abcdef,
'trace_id_high' => 10,
'flags' => 0
},
{ 'span_id' => 0, 'trace_id' => 0, 'flags' => 0 }]
)
)
end
Expand Down

0 comments on commit 78e19ba

Please sign in to comment.