Skip to content

Commit

Permalink
Cache profiling_enabled instead of re-reading it all the time
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoanjo committed Jun 20, 2023
1 parent d35fadd commit cbe0317
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
5 changes: 3 additions & 2 deletions lib/datadog/tracing/trace_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def initialize(
sampled: nil,
sampling_priority: nil,
service: nil,
profiling_enabled: nil,
tags: nil,
metrics: nil
)
Expand All @@ -84,6 +85,7 @@ def initialize(
@sample_rate = sample_rate
@sampling_priority = sampling_priority
@service = service
@profiling_enabled = profiling_enabled

# Generic tags
set_tags(tags) if tags
Expand Down Expand Up @@ -451,8 +453,7 @@ def build_trace(spans, partial = false)
tags: meta,
metrics: metrics,
root_span_id: !partial ? root_span && root_span.id : nil,
profiling_enabled:
!!(defined?(Datadog::Profiling) && Datadog::Profiling.respond_to?(:enabled?) && Datadog::Profiling.enabled?), # rubocop:disable Style/DoubleNegation
profiling_enabled: @profiling_enabled,
)
end

Expand Down
9 changes: 8 additions & 1 deletion lib/datadog/tracing/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ def build_trace(digest = nil)
if digest
TraceOperation.new(
hostname: hostname,
profiling_enabled: profiling_enabled,
id: digest.trace_id,
origin: digest.trace_origin,
parent_span_id: digest.span_id,
Expand All @@ -329,7 +330,8 @@ def build_trace(digest = nil)
)
else
TraceOperation.new(
hostname: hostname
hostname: hostname,
profiling_enabled: profiling_enabled,
)
end
end
Expand Down Expand Up @@ -525,6 +527,11 @@ def skip_trace(name)
span
end
end

def profiling_enabled
@profiling_enabled ||=
!!(defined?(Datadog::Profiling) && Datadog::Profiling.respond_to?(:enabled?) && Datadog::Profiling.enabled?)
end
end
end
end
18 changes: 5 additions & 13 deletions spec/datadog/tracing/trace_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
sampled: sampled,
sampling_priority: sampling_priority,
service: service,
profiling_enabled: profiling_enabled,
tags: tags,
metrics: metrics
metrics: metrics,
}
end

Expand All @@ -48,6 +49,7 @@
let(:sampled) { true }
let(:sampling_priority) { Datadog::Tracing::Sampling::Ext::Priority::USER_KEEP }
let(:service) { 'billing-api' }
let(:profiling_enabled) { 'profiling_enabled' }
let(:tags) { { 'foo' => 'bar' }.merge(distributed_tags) }
let(:metrics) { { 'baz' => 42.0 } }

Expand Down Expand Up @@ -1636,21 +1638,11 @@ def span
rule_sample_rate: rule_sample_rate,
runtime_id: Datadog::Core::Environment::Identity.id,
sample_rate: sample_rate,

sampling_priority: sampling_priority,
service: service
service: service,
profiling_enabled: profiling_enabled,
)
end

context 'and profiling is enabled' do
before { expect(Datadog::Profiling).to receive(:enabled?).and_return(true) }
it { is_expected.to have_attributes(profiling_enabled: true) }
end

context 'and profiling is disabled' do
before { expect(Datadog::Profiling).to receive(:enabled?).and_return(false) }
it { is_expected.to have_attributes(profiling_enabled: false) }
end
end

context 'is finished' do
Expand Down
20 changes: 20 additions & 0 deletions spec/datadog/tracing/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,26 @@
end
end
end

it 'adds profiling_enabled to the trace' do
expect(Datadog::Profiling).to receive(:enabled?).and_return(true)

tracer.trace(name) {}

expect(traces.first.profiling_enabled).to be true
end

context 'when profiler is not available' do
before do
expect(Datadog::Profiling).to receive(:respond_to?).with(:enabled?).and_return(false)
end

it 'adds profiling_enabled as false to the trace' do
tracer.trace(name) {}

expect(traces.first.profiling_enabled).to be false
end
end
end

context 'when nesting spans' do
Expand Down

0 comments on commit cbe0317

Please sign in to comment.