Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Sep 13, 2024
1 parent c9994df commit 4d5b459
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/datadog/core/telemetry/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Telemetry
# Report telemetry logs via delegating to the telemetry component instance via mutex.
#
# IMPORTANT: Invoking this method during the lifecycle of component initialization will
# cause a non-recoverable deadlock
# be no-op instead.
#
# For developer using this module:
# read: lib/datadog/core/telemetry/logging.rb
Expand All @@ -25,7 +25,17 @@ def error(description)
private

def instance
Datadog.send(:components).telemetry
# `allow_initialization: false` to prevent deadlock from components lifecycle
components = Datadog.send(:components, allow_initialization: false)

if components && components.telemetry
components.telemetry
else
Datadog.logger.debug(
'Fail to send telemetry log before components initialization or within components lifecycle'
)
nil
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/datadog/core/telemetry/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
telemetry = instance_double(Datadog::Core::Telemetry::Component)
allow(Datadog.send(:components)).to receive(:telemetry).and_return(telemetry)
expect(telemetry).to receive(:report).with(exception, level: :error, description: 'Oops...')
expect(Datadog.logger).not_to receive(:debug)

expect do
described_class.report(exception, level: :error, description: 'Oops...')
Expand All @@ -22,6 +23,7 @@
telemetry = instance_double(Datadog::Core::Telemetry::Component)
allow(Datadog.send(:components)).to receive(:telemetry).and_return(telemetry)
expect(telemetry).to receive(:report).with(exception, level: :error, description: nil)
expect(Datadog.logger).not_to receive(:debug)

expect do
described_class.report(exception)
Expand All @@ -34,6 +36,7 @@
it do
exception = StandardError.new
allow(Datadog.send(:components)).to receive(:telemetry).and_return(nil)
expect(Datadog.logger).to receive(:debug).with(/Fail to send telemetry log/)

expect do
described_class.report(exception, level: :error, description: 'Oops...')
Expand All @@ -48,6 +51,7 @@
telemetry = instance_double(Datadog::Core::Telemetry::Component)
allow(Datadog.send(:components)).to receive(:telemetry).and_return(telemetry)
expect(telemetry).to receive(:error).with('description')
expect(Datadog.logger).not_to receive(:debug)

expect { described_class.error('description') }.not_to raise_error
end
Expand All @@ -56,6 +60,7 @@
context 'when there is no telemetry component configured' do
it do
allow(Datadog.send(:components)).to receive(:telemetry).and_return(nil)
expect(Datadog.logger).to receive(:debug).with(/Fail to send telemetry log/)

expect { described_class.error('description') }.not_to raise_error
end
Expand Down

0 comments on commit 4d5b459

Please sign in to comment.