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 e1346ba
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 63 deletions.
18 changes: 7 additions & 11 deletions lib/datadog/tracing/span_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,15 @@ 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
Expand All @@ -62,7 +58,7 @@ def to_hash
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
93 changes: 48 additions & 45 deletions spec/datadog/tracing/span_link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,110 @@
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) }
end

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

it { is_expected.to have_attributes(trace_flags: 0x01) }
it { is_expected.to have_attributes(attributes: attributes) }
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' } }
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 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
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
10 changes: 6 additions & 4 deletions spec/datadog/tracing/transport/serializable_trace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@
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,
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' }
)]
)
Expand Down

0 comments on commit e1346ba

Please sign in to comment.