-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'event' | ||
|
||
module Datadog | ||
module Core | ||
module Telemetry | ||
# Logging interface for sending telemetry logs. | ||
# | ||
# Reporting internal error so that we can fix them. | ||
# IMPORTANT: Make sure to not log any sensitive information. | ||
module Logging | ||
module_function | ||
|
||
def report(exception, level:) | ||
# Annoymous exceptions to be logged as <Class:0x00007f8b1c0b3b40> | ||
message = exception.class.name || exception.class.inspect | ||
|
||
event = Event::Log.new( | ||
message: message, | ||
level: level | ||
) | ||
|
||
if (telemetry = Datadog.send(:components).telemetry) | ||
telemetry.log!(event) | ||
else | ||
Datadog.logger.debug do | ||
"Attempting to send telemetry log when telemetry component is not ready: #{message}" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Datadog | ||
module Core | ||
module Telemetry | ||
module Logging | ||
def report: (Exception exception, level: Symbol) -> void | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'spec_helper' | ||
|
||
require 'datadog/core/telemetry/logging' | ||
|
||
RSpec.describe Datadog::Core::Telemetry::Logging do | ||
describe '.report' do | ||
context 'with named exception' do | ||
it 'sends a log event to via telemetry' do | ||
telemetry = double('telemetry') | ||
allow(Datadog.send(:components)).to receive(:telemetry).and_return(telemetry) | ||
expect(telemetry).to receive(:log!).with(instance_of(Datadog::Core::Telemetry::Event::Log)) do |event| | ||
expect(event.payload).to include(message: 'RuntimeError', level: 'ERROR') | ||
end | ||
|
||
begin | ||
raise 'Invalid token: p@ssw0rd' | ||
rescue StandardError => e | ||
described_class.report(e, level: :error) | ||
end | ||
end | ||
end | ||
|
||
context 'with anonymous exception' do | ||
it 'sends a log event to via telemetry' do | ||
telemetry = double('telemetry') | ||
allow(Datadog.send(:components)).to receive(:telemetry).and_return(telemetry) | ||
expect(telemetry).to receive(:log!).with(instance_of(Datadog::Core::Telemetry::Event::Log)) do |event| | ||
expect(event.payload).to include(message: /#<Class:/, level: 'ERROR') | ||
end | ||
|
||
customer_exception = Class.new(StandardError) | ||
|
||
begin | ||
raise customer_exception, 'Invalid token: p@ssw0rd' | ||
rescue StandardError => e | ||
described_class.report(e, level: :error) | ||
end | ||
end | ||
end | ||
|
||
context 'when telemetry component is not available' do | ||
it 'does not sends a log event to via telemetry' do | ||
logger = Logger.new($stdout) | ||
expect(Datadog.send(:components)).to receive(:telemetry).and_return(nil) | ||
expect(Datadog).to receive(:logger).and_return(logger) | ||
expect(logger).to receive(:debug).with(no_args) do |&block| | ||
expect(block.call).to match /Attempting to send telemetry log when telemetry component is not ready/ | ||
end | ||
|
||
begin | ||
raise 'Invalid token: p@ssw0rd' | ||
rescue StandardError => e | ||
described_class.report(e, level: :error) | ||
end | ||
end | ||
end | ||
end | ||
end |