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

Telemetry: Add environment variable to disable logs #4153

Merged
merged 5 commits into from
Dec 6, 2024
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
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 @@ -14,6 +14,7 @@ module Datadog
module Core
module Telemetry
# Telemetry entrypoint, coordinates sending telemetry events at various points in app lifecycle.
# Note: Telemetry does not spawn its worker thread in fork processes, thus no telemetry is sent in forked processes.
class Component
attr_reader :enabled

Expand Down Expand Up @@ -52,6 +53,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 +69,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 +89,9 @@ def initialize(
dependency_collection: dependency_collection,
shutdown_timeout: shutdown_timeout_seconds
)

@stopped = false

@worker.start
end

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

def log!(event)
return unless @enabled || forked?
return if !@enabled || forked? || !@log_collection_enabled
marcotc marked this conversation as resolved.
Show resolved Hide resolved

@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
10 changes: 6 additions & 4 deletions sig/datadog/core/utils/forking.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ module Datadog
module Core
module Utils
module Forking
@fork_pid: Integer

def self.included: (untyped base) -> untyped

def self.extended: (untyped base) -> untyped

def after_fork!: () { () -> untyped } -> untyped
def after_fork!: () { () -> untyped } -> bool

def forked?: () -> untyped
def forked?: () -> bool

def update_fork_pid!: () -> untyped
def update_fork_pid!: () -> void

def fork_pid: () -> untyped
def fork_pid: () -> Integer

module ClassExtensions
def initialize: (*untyped args) { () -> untyped } -> untyped
Expand Down
13 changes: 9 additions & 4 deletions spec/datadog/core/configuration/components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,13 @@
{ enabled: enabled, http_transport: an_instance_of(Datadog::Core::Telemetry::Http::Transport),
metrics_enabled: metrics_enabled, heartbeat_interval_seconds: heartbeat_interval_seconds,
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
dependency_collection: dependency_collection, shutdown_timeout_seconds: shutdown_timeout_seconds }
dependency_collection: dependency_collection, shutdown_timeout_seconds: shutdown_timeout_seconds,
log_collection_enabled: log_collection_enabled, }
end
let(:enabled) { true }
let(:agentless_enabled) { false }
let(:metrics_enabled) { true }
let(:log_collection_enabled) { true }
let(:heartbeat_interval_seconds) { 60 }
let(:metrics_aggregation_interval_seconds) { 10 }
let(:shutdown_timeout_seconds) { 1.0 }
Expand All @@ -262,7 +264,8 @@
{ enabled: false, http_transport: an_instance_of(Datadog::Core::Telemetry::Http::Transport),
metrics_enabled: false, heartbeat_interval_seconds: heartbeat_interval_seconds,
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
dependency_collection: dependency_collection, shutdown_timeout_seconds: shutdown_timeout_seconds }
dependency_collection: dependency_collection, shutdown_timeout_seconds: shutdown_timeout_seconds,
log_collection_enabled: true, }
end
let(:agent_settings) do
instance_double(
Expand All @@ -287,7 +290,8 @@
{ enabled: enabled, http_transport: transport,
metrics_enabled: metrics_enabled, heartbeat_interval_seconds: heartbeat_interval_seconds,
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
dependency_collection: dependency_collection, shutdown_timeout_seconds: shutdown_timeout_seconds }
dependency_collection: dependency_collection, shutdown_timeout_seconds: shutdown_timeout_seconds,
log_collection_enabled: log_collection_enabled, }
end

before do
Expand All @@ -306,7 +310,8 @@
{ enabled: false, http_transport: transport,
metrics_enabled: false, heartbeat_interval_seconds: heartbeat_interval_seconds,
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
dependency_collection: dependency_collection, shutdown_timeout_seconds: shutdown_timeout_seconds }
dependency_collection: dependency_collection, shutdown_timeout_seconds: shutdown_timeout_seconds,
log_collection_enabled: true, }
end

it 'does not enable telemetry when agentless mode requested but api key is not present' do
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
Loading