|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +require 'datadog/core/telemetry/logging' |
| 4 | +require 'datadog/core/telemetry/component' |
| 5 | + |
| 6 | +RSpec.describe Datadog::Core::Telemetry::Logging do |
| 7 | + describe '.report' do |
| 8 | + context 'with named exception' do |
| 9 | + it 'sends a log event to via telemetry' do |
| 10 | + telemetry = instance_double(Datadog::Core::Telemetry::Component) |
| 11 | + allow(Datadog.send(:components)).to receive(:telemetry).and_return(telemetry) |
| 12 | + expect(telemetry).to receive(:log!).with(instance_of(Datadog::Core::Telemetry::Event::Log)) do |event| |
| 13 | + expect(event.payload).to include(logs: [{ message: 'RuntimeError', level: 'ERROR' }]) |
| 14 | + end |
| 15 | + |
| 16 | + begin |
| 17 | + raise 'Invalid token: p@ssw0rd' |
| 18 | + rescue StandardError => e |
| 19 | + described_class.report(e, level: :error) |
| 20 | + end |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + context 'with anonymous exception' do |
| 25 | + it 'sends a log event to via telemetry' do |
| 26 | + telemetry = instance_double(Datadog::Core::Telemetry::Component) |
| 27 | + allow(Datadog.send(:components)).to receive(:telemetry).and_return(telemetry) |
| 28 | + expect(telemetry).to receive(:log!).with(instance_of(Datadog::Core::Telemetry::Event::Log)) do |event| |
| 29 | + expect(event.payload).to include(logs: [{ message: /#<Class:/, level: 'ERROR' }]) |
| 30 | + end |
| 31 | + |
| 32 | + customer_exception = Class.new(StandardError) |
| 33 | + |
| 34 | + begin |
| 35 | + raise customer_exception, 'Invalid token: p@ssw0rd' |
| 36 | + rescue StandardError => e |
| 37 | + described_class.report(e, level: :error) |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context 'when telemetry component is not available' do |
| 43 | + it 'does not sends a log event to via telemetry' do |
| 44 | + logger = Logger.new($stdout) |
| 45 | + expect(Datadog.send(:components)).to receive(:telemetry).and_return(nil) |
| 46 | + expect(Datadog).to receive(:logger).and_return(logger) |
| 47 | + expect(logger).to receive(:debug).with(no_args) do |&block| |
| 48 | + expect(block.call).to match(/Attempting to send telemetry log when telemetry component is not ready/) |
| 49 | + end |
| 50 | + |
| 51 | + begin |
| 52 | + raise 'Invalid token: p@ssw0rd' |
| 53 | + rescue StandardError => e |
| 54 | + described_class.report(e, level: :error) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | +end |
0 commit comments