Skip to content

Commit

Permalink
Telemetry: Add environment variable to disable logs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed Nov 22, 2024
1 parent 791f9db commit ad44840
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 14 deletions.
10 changes: 10 additions & 0 deletions lib/datadog/core/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,16 @@ def initialize(*_)
o.type :float
o.default 1.0
end

# Enable log collection for telemetry. Log collection only works when telemetry is enabled and
# logs are enabled.
# @default `DD_TELEMETRY_LOG_COLLECTION_ENABLED` environment variable, otherwise `true`.
# @return [Boolean]
option :log_collection_enabled do |o|
o.type :bool
o.env Core::Telemetry::Ext::ENV_LOG_COLLECTION
o.default true
end
end

# Remote configuration
Expand Down
12 changes: 9 additions & 3 deletions lib/datadog/core/telemetry/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def self.build(settings, agent_settings, logger)
metrics_aggregation_interval_seconds: settings.telemetry.metrics_aggregation_interval_seconds,
dependency_collection: settings.telemetry.dependency_collection,
shutdown_timeout_seconds: settings.telemetry.shutdown_timeout_seconds,
log_collection_enabled: settings.telemetry.log_collection_enabled
)
end

Expand All @@ -67,10 +68,11 @@ def initialize(
http_transport:,
shutdown_timeout_seconds:,
enabled: true,
metrics_enabled: true
metrics_enabled: true,
log_collection_enabled: true
)
@enabled = enabled
@stopped = false
@log_collection_enabled = log_collection_enabled

@metrics_manager = MetricsManager.new(
enabled: enabled && metrics_enabled,
Expand All @@ -86,6 +88,9 @@ def initialize(
dependency_collection: dependency_collection,
shutdown_timeout: shutdown_timeout_seconds
)

@stopped = false

@worker.start
end

Expand Down Expand Up @@ -114,7 +119,8 @@ def integrations_change!
end

def log!(event)
return unless @enabled || forked?
puts "forked?: #{forked?}"
return if !@enabled || forked? || !@log_collection_enabled

@worker.enqueue(event)
end
Expand Down
1 change: 1 addition & 0 deletions lib/datadog/core/telemetry/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Ext
ENV_INSTALL_TYPE = 'DD_INSTRUMENTATION_INSTALL_TYPE'
ENV_INSTALL_TIME = 'DD_INSTRUMENTATION_INSTALL_TIME'
ENV_AGENTLESS_URL_OVERRIDE = 'DD_TELEMETRY_AGENTLESS_URL'
ENV_LOG_COLLECTION = 'DD_TELEMETRY_LOG_COLLECTION_ENABLED'
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion sig/datadog/core/telemetry/component.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Datadog
module Telemetry
class Component
@enabled: bool
@log_collection_enabled: bool
@stopped: bool
@metrics_manager: Datadog::Core::Telemetry::MetricsManager
@worker: Datadog::Core::Telemetry::Worker
Expand All @@ -15,7 +16,7 @@ module Datadog

def self.build: (untyped settings, Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings agent_settings, Datadog::Core::Logger logger) -> Component

def initialize: (http_transport: Datadog::Core::Telemetry::Http::Transport, heartbeat_interval_seconds: Float, metrics_aggregation_interval_seconds: Float, dependency_collection: bool, ?enabled: bool, ?metrics_enabled: bool, shutdown_timeout_seconds: Float | Integer) -> void
def initialize: (http_transport: Datadog::Core::Telemetry::Http::Transport, heartbeat_interval_seconds: Float, metrics_aggregation_interval_seconds: Float, dependency_collection: bool, ?enabled: bool, ?metrics_enabled: bool, shutdown_timeout_seconds: Float | Integer, ?log_collection_enabled: bool) -> void

def disable!: () -> void

Expand Down
1 change: 1 addition & 0 deletions sig/datadog/core/telemetry/ext.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Datadog
module Ext
ENV_DEPENDENCY_COLLECTION: ::String
ENV_ENABLED: ::String
ENV_LOG_COLLECTION: ::String
ENV_METRICS_ENABLED: ::String
ENV_HEARTBEAT_INTERVAL: ::String
ENV_METRICS_AGGREGATION_INTERVAL: ::String
Expand Down
33 changes: 33 additions & 0 deletions spec/datadog/core/configuration/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,39 @@
end
end

describe '#log_collection_enabled' do
subject(:log_collection_enabled) { settings.telemetry.log_collection_enabled }
let(:env_var_name) { 'DD_TELEMETRY_LOG_COLLECTION_ENABLED' }

context 'when DD_TELEMETRY_LOG_COLLECTION_ENABLED' do
context 'is not defined' do
let(:env_var_value) { nil }

it { is_expected.to be true }
end

[true, false].each do |value|
context "is defined as #{value}" do
let(:env_var_value) { value.to_s }

it { is_expected.to be value }
end
end
end
end

describe '#log_collection_enabled=' do
let(:env_var_name) { 'DD_TELEMETRY_LOG_COLLECTION_ENABLED' }
let(:env_var_value) { 'true' }

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

describe '#heartbeat_interval' do
subject(:heartbeat_interval_seconds) { settings.telemetry.heartbeat_interval_seconds }
let(:env_var_name) { 'DD_TELEMETRY_HEARTBEAT_INTERVAL' }
Expand Down
25 changes: 15 additions & 10 deletions spec/datadog/core/telemetry/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
enabled: enabled,
http_transport: http_transport,
metrics_enabled: metrics_enabled,
log_collection_enabled: log_collection_enabled,
heartbeat_interval_seconds: heartbeat_interval_seconds,
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
dependency_collection: dependency_collection,
Expand All @@ -17,6 +18,7 @@

let(:enabled) { true }
let(:metrics_enabled) { true }
let(:log_collection_enabled) { true }
let(:heartbeat_interval_seconds) { 0 }
let(:metrics_aggregation_interval_seconds) { 1 }
let(:shutdown_timeout_seconds) { 1 }
Expand Down Expand Up @@ -232,8 +234,10 @@
telemetry.stop!
end

describe 'when enabled' do
describe 'when enabled and log_collection_enabled is enabled' do
let(:enabled) { true }
let(:log_collection_enabled) { true }

it do
event = instance_double(Datadog::Core::Telemetry::Event::Log)
telemetry.log!(event)
Expand All @@ -243,11 +247,12 @@

context 'when in fork', skip: !Process.respond_to?(:fork) do
it do
telemetry
expect_in_fork do
event = instance_double(Datadog::Core::Telemetry::Event::Log)
telemetry.log!(event)

expect(worker).to have_received(:enqueue).with(event)
expect(worker).not_to have_received(:enqueue)
end
end
end
Expand All @@ -262,16 +267,16 @@

expect(worker).not_to have_received(:enqueue)
end
end

context 'when in fork', skip: !Process.respond_to?(:fork) do
it do
expect_in_fork do
event = instance_double(Datadog::Core::Telemetry::Event::Log)
telemetry.log!(event)
describe 'when log_collection_enabled is disabled' do
let(:log_collection_enabled) { false }

expect(worker).not_to have_received(:enqueue)
end
end
it do
event = instance_double(Datadog::Core::Telemetry::Event::Log)
telemetry.log!(event)

expect(worker).not_to have_received(:enqueue)
end
end
end
Expand Down

0 comments on commit ad44840

Please sign in to comment.