Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed Jun 5, 2024
1 parent 2675f58 commit 4727e12
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 28 deletions.
3 changes: 1 addition & 2 deletions lib/datadog/tracing/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def build_tracer(settings, agent_settings, logger:)
span_sampler: build_span_sampler(settings),
writer: writer,
tags: build_tracer_tags(settings),
_inherit_parent_service: false
#_inherit_parent_service: settings.tracing.inherit_parent_service
inherit_parent_service: settings.tracing.inherit_parent_service
)
end

Expand Down
10 changes: 10 additions & 0 deletions lib/datadog/tracing/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ def self.extended(base)
o.setter { |header_tags, _| Configuration::HTTP::HeaderTags.new(header_tags) }
end

# Whether spans inherit their parent's service name, when span `service` is not explicitly set.
# If `true`, the spans will use the service name from its parent span.
# If `false`, the spans will use the global service name (`c.service`).
# @default `false`
# @return [Boolean]
option :inherit_parent_service do |o|
o.default false
o.type :bool
end

# Enable 128 bit trace id generation.
#
# @default `DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED` environment variable, otherwise `true`
Expand Down
6 changes: 3 additions & 3 deletions lib/datadog/tracing/span_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def initialize(
trace_id: nil,
type: nil,
links: nil,
_inherit_parent_service: false
inherit_parent_service: false
)
# Ensure dynamically created strings are UTF-8 encoded.
#
Expand Down Expand Up @@ -91,7 +91,7 @@ def initialize(
# Subscribe :on_error event
@events.on_error.wrap_default(&on_error) if on_error.is_a?(Proc)

@_inherit_parent_service = _inherit_parent_service
@inherit_parent_service = inherit_parent_service

# Start the span with start time, if given.
start(start_time) if start_time
Expand Down Expand Up @@ -442,7 +442,7 @@ def message
# mutation by reference; when this span is returned,
# we don't want this SpanOperation to modify it further.
def build_span
service = self.service || (@parent&.service if @_inherit_parent_service)
service = self.service || (@parent&.service if @inherit_parent_service)

Span.new(
@name,
Expand Down
6 changes: 3 additions & 3 deletions lib/datadog/tracing/trace_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ def initialize(
trace_state: nil,
trace_state_unknown_fields: nil,
remote_parent: false,
_inherit_parent_service: false
inherit_parent_service: false
)
# Attributes
@id = id || Tracing::Utils::TraceId.next_id
@max_length = max_length || DEFAULT_MAX_LENGTH
@parent_span_id = parent_span_id
@sampled = sampled.nil? ? true : sampled
@remote_parent = remote_parent
@_inherit_parent_service = _inherit_parent_service
@inherit_parent_service = inherit_parent_service

# Tags
@agent_sample_rate = agent_sample_rate
Expand Down Expand Up @@ -254,7 +254,7 @@ def build_span(
tags: tags,
trace_id: trace_id,
type: type,
_inherit_parent_service: @_inherit_parent_service
inherit_parent_service: @inherit_parent_service
)
rescue StandardError => e
Datadog.logger.debug { "Failed to build new span: #{e}" }
Expand Down
8 changes: 4 additions & 4 deletions lib/datadog/tracing/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def initialize(
span_sampler: Sampling::Span::Sampler.new,
tags: {},
writer: Writer.new,
_inherit_parent_service: false
inherit_parent_service: false
)
@trace_flush = trace_flush
@default_service = default_service
Expand All @@ -69,7 +69,7 @@ def initialize(
@span_sampler = span_sampler
@tags = tags
@writer = writer
@_inherit_parent_service = _inherit_parent_service
@inherit_parent_service = inherit_parent_service
end

# Return a {Datadog::Tracing::SpanOperation span_op} and {Datadog::Tracing::TraceOperation trace_op}
Expand Down Expand Up @@ -330,15 +330,15 @@ def build_trace(digest = nil)
trace_state_unknown_fields: digest.trace_state_unknown_fields,
remote_parent: digest.span_remote,
default_service: @default_service,
_inherit_parent_service: @_inherit_parent_service,
inherit_parent_service: @inherit_parent_service,
)
else
TraceOperation.new(
hostname: hostname,
profiling_enabled: profiling_enabled,
remote_parent: false,
default_service: @default_service,
_inherit_parent_service: @_inherit_parent_service,
inherit_parent_service: @inherit_parent_service,
)
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/datadog/core/configuration/components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@
end
end
let(:span_sampler) { be_a(Datadog::Tracing::Sampling::Span::Sampler) }
let(:inherit_parent_service) { false }
let(:default_options) do
{
default_service: settings.service,
Expand All @@ -434,6 +435,7 @@
sampler: sampler,
span_sampler: span_sampler,
writer: writer,
inherit_parent_service: inherit_parent_service,
}
end

Expand Down
31 changes: 31 additions & 0 deletions spec/datadog/tracing/configuration/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,37 @@ def propagation_style_inject
end
end

describe '#inherit_parent_service' do
subject(:inherit_parent_service) { settings.tracing.inherit_parent_service }

it { is_expected.to be false }

context "when inherit_parent_service" do
before { settings.tracing.inherit_parent_service = value }

context 'is set to true' do
let(:value) { true }

it { is_expected.to be true }
end

context 'is set to false' do
let(:value) { false }

it { is_expected.to be false }
end
end
end

describe '#inherit_parent_service=' do
it 'updates the #inherit_parent_service setting' do
expect { settings.tracing.inherit_parent_service = true }
.to change { settings.tracing.inherit_parent_service }
.from(false)
.to(true)
end
end

describe '#header_tags' do
subject(:header_tags) { settings.tracing.header_tags }

Expand Down
58 changes: 42 additions & 16 deletions spec/datadog/tracing/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,29 +259,55 @@
end

context 'when nesting spans' do
it 'propagates parent span and uses default service name' do
subject!(:nested_spans) do
tracer.trace('parent', service: 'service-parent') do
tracer.trace('child1') { |s| s.set_tag('tag', 'tag_1') }
tracer.trace('child2', service: 'service-child2') { |s| s.set_tag('tag', 'tag_2') }
end
end

expect(spans).to have(3).items
context "when inherit_parent_service is false" do
it 'propagates parent span and uses default service name' do
expect(spans).to have(3).items

child1, child2, parent = spans # Spans are sorted alphabetically by operation name
child1, child2, parent = spans # Spans are sorted alphabetically by operation name

expect(parent).to be_root_span
expect(parent.name).to eq('parent')
expect(parent.service).to eq('service-parent')

expect(child1.parent_id).to be(parent.id)
expect(child1.name).to eq('child1')
expect(child1.service).to eq('service-parent')
expect(child1.get_tag('tag')).to eq('tag_1')

expect(child2.parent_id).to be(parent.id)
expect(child2.name).to eq('child2')
expect(child2.service).to eq('service-child2')
expect(child2.get_tag('tag')).to eq('tag_2')
expect(parent).to be_root_span
expect(parent.name).to eq('parent')
expect(parent.service).to eq('service-parent')

expect(child1.parent_id).to be(parent.id)
expect(child1.name).to eq('child1')
expect(child1.service).to eq(tracer.default_service)
expect(child1.get_tag('tag')).to eq('tag_1')

expect(child2.parent_id).to be(parent.id)
expect(child2.name).to eq('child2')
expect(child2.service).to eq('service-child2')
expect(child2.get_tag('tag')).to eq('tag_2')
end
end

context "when inherit_parent_service is true" do
let(:tracer_options) { super().merge(inherit_parent_service: true) }

it "propagates parent span and uses parent's service name" do
expect(spans).to have(3).items

child1, child2, parent = spans # Spans are sorted alphabetically by operation name

expect(parent).to be_root_span
expect(parent.name).to eq('parent')
expect(parent.service).to eq('service-parent')

expect(child1.parent_id).to be(parent.id)
expect(child1.name).to eq('child1')
expect(child1.service).to eq('service-parent')

expect(child2.parent_id).to be(parent.id)
expect(child2.name).to eq('child2')
expect(child2.service).to eq('service-child2')
end
end

it 'trace has a runtime ID and PID tags' do
Expand Down

0 comments on commit 4727e12

Please sign in to comment.