Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PROF-7786] Include _dd.profiling.enabled tag #2913

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/datadog/core/workers/async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def fork_policy
:result

def mutex
@mutex || MUTEX_INIT.synchronize { @mutex ||= Mutex.new }
(defined?(@mutex) && @mutex) || MUTEX_INIT.synchronize { @mutex ||= Mutex.new }
end

def after_fork
Expand All @@ -103,7 +103,7 @@ def after_fork
:pid

def mutex_after_fork
@mutex_after_fork || MUTEX_INIT.synchronize { @mutex_after_fork ||= Mutex.new }
(defined?(@mutex_after_fork) && @mutex_after_fork) || MUTEX_INIT.synchronize { @mutex_after_fork ||= Mutex.new }
end

def worker
Expand Down
5 changes: 5 additions & 0 deletions lib/datadog/profiling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def self.allocation_count
nil
end

def self.enabled?
profiler = Datadog.send(:components).profiler
!!(profiler.scheduler.running? if profiler)
end

private_class_method def self.replace_noop_allocation_count
def self.allocation_count # rubocop:disable Lint/DuplicateMethods, Lint/NestedMethodDefinition (On purpose!)
Datadog::Profiling::Collectors::CpuAndWallTimeWorker._native_allocation_count
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/tracing/metadata/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ module Ext
# Set this tag to `1.0` if the span is a Service Entry span.
TAG_TOP_LEVEL = '_dd.top_level'

# Set to `1.0` if profiling is enabled together with tracing, and `0.0` otherwise
# See Datadog-internal "RFC: Identifying which spans have profiling enabled " for details
TAG_PROFILING_ENABLED = '_dd.profiling.enabled'

# Defines constants for trace analytics
# @public_api
module Analytics
Expand Down
5 changes: 4 additions & 1 deletion 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 @@ -450,7 +452,8 @@ def build_trace(spans, partial = false)
service: service,
tags: meta,
metrics: metrics,
root_span_id: !partial ? root_span && root_span.id : nil
root_span_id: !partial ? root_span && root_span.id : nil,
profiling_enabled: @profiling_enabled,
)
end

Expand Down
7 changes: 5 additions & 2 deletions lib/datadog/tracing/trace_segment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class TraceSegment
:sample_rate,
:sampling_decision_maker,
:sampling_priority,
:service
:service,
:profiling_enabled

# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
Expand All @@ -54,7 +55,8 @@ def initialize(
sampling_priority: nil,
service: nil,
tags: nil,
metrics: nil
metrics: nil,
profiling_enabled: nil
)
@id = id
@root_span_id = root_span_id
Expand All @@ -80,6 +82,7 @@ def initialize(
@sampling_decision_maker = sampling_decision_maker_tag
@sampling_priority = sampling_priority || sampling_priority_tag
@service = Core::Utils::SafeDup.frozen_or_dup(service || service_tag)
@profiling_enabled = profiling_enabled
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity
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
9 changes: 9 additions & 0 deletions lib/ddtrace/transport/trace_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def format!
tag_sampling_decision_maker!
tag_high_order_trace_id!
tag_sampling_priority!
tag_profiling_enabled!

trace
end
Expand Down Expand Up @@ -175,6 +176,14 @@ def tag_high_order_trace_id!
root_span.set_tag(Tracing::Metadata::Ext::Distributed::TAG_TID, high_order_tid)
end

def tag_profiling_enabled!
return if trace.profiling_enabled.nil?

root_span.set_tag(
Tracing::Metadata::Ext::TAG_PROFILING_ENABLED, trace.profiling_enabled ? 1 : 0
)
end

private

def partial?
Expand Down
29 changes: 29 additions & 0 deletions spec/datadog/profiling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,35 @@
end
end

describe '.enabled?' do
subject(:enabled?) { described_class.enabled? }

before do
allow(Datadog.send(:components)).to receive(:profiler).and_return(profiler)
end

context 'when no profiler is available' do
let(:profiler) { nil }

it { is_expected.to be(false) }
end

context 'when a profiler is available' do
let(:profiler) { instance_double('Datadog::Profiling::Profiler', scheduler: scheduler) }
let(:scheduler) { instance_double('Datadog::Profiling::Scheduler', running?: running) }

context 'when the profiler is running' do
let(:running) { true }
it { is_expected.to be(true) }
end

context 'when the profiler is not running' do
let(:running) { false }
it { is_expected.to be(false) }
end
end
end

describe '::supported?' do
subject(:supported?) { described_class.supported? }

Expand Down
8 changes: 5 additions & 3 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,9 +1638,9 @@ 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
end
Expand Down
7 changes: 7 additions & 0 deletions spec/datadog/tracing/trace_segment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
sampling_priority: nil,
service: nil,
spans: spans,
profiling_enabled: nil,
)
end

Expand Down Expand Up @@ -162,6 +163,12 @@

it { expect(trace_segment.send(:metrics)).to eq({ 'foo' => 42.0 }) }
end

context ':profiling_enabled' do
let(:options) { { profiling_enabled: true } }

it { is_expected.to have_attributes(profiling_enabled: true) }
end
end

context 'given tags' 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
8 changes: 6 additions & 2 deletions spec/ddtrace/transport/trace_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
runtime_id: runtime_id,
sample_rate: sample_rate,
sampling_priority: sampling_priority,
tags: trace_tags
tags: trace_tags,
profiling_enabled: profiling_enabled,
}
end

Expand All @@ -49,6 +50,7 @@
let(:runtime_id) { 'trace.runtime_id' }
let(:sample_rate) { rand }
let(:sampling_priority) { Datadog::Tracing::Sampling::Ext::Priority::USER_KEEP }
let(:profiling_enabled) { true }
end

shared_context 'trace metadata with tags' do
Expand Down Expand Up @@ -133,7 +135,8 @@
Datadog::Tracing::Metadata::Ext::Sampling::TAG_RULE_SAMPLE_RATE => nil,
Datadog::Core::Runtime::Ext::TAG_ID => nil,
Datadog::Tracing::Metadata::Ext::Sampling::TAG_SAMPLE_RATE => nil,
Datadog::Tracing::Metadata::Ext::Distributed::TAG_SAMPLING_PRIORITY => nil
Datadog::Tracing::Metadata::Ext::Distributed::TAG_SAMPLING_PRIORITY => nil,
Datadog::Tracing::Metadata::Ext::TAG_PROFILING_ENABLED => nil,
)
end
end
Expand All @@ -152,6 +155,7 @@
Datadog::Core::Runtime::Ext::TAG_ID => runtime_id,
Datadog::Tracing::Metadata::Ext::Sampling::TAG_SAMPLE_RATE => sample_rate,
Datadog::Tracing::Metadata::Ext::Distributed::TAG_SAMPLING_PRIORITY => sampling_priority,
Datadog::Tracing::Metadata::Ext::TAG_PROFILING_ENABLED => 1.0,
)
end
end
Expand Down