Skip to content

Commit

Permalink
Refactor with default level
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Sep 5, 2024
1 parent b39b450 commit 80356eb
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 21 deletions.
6 changes: 3 additions & 3 deletions lib/datadog/appsec/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def require_libddwaf
'libddwaf failed to load,' \
"installed platform: #{libddwaf_platform} ruby platforms: #{ruby_platforms} error: #{e.inspect}"
end
@telemetry.report(e, level: :error, description: 'libddwaf failed to load')
@telemetry.report(e, description: 'libddwaf failed to load')

false
end
Expand Down Expand Up @@ -140,7 +140,7 @@ def create_waf_handle(settings, ruleset)
Datadog.logger.error do
"libddwaf failed to initialize, error: #{e.inspect}"
end
@telemetry.report(e, level: :error, description: 'libddwaf failed to initialize')
@telemetry.report(e, description: 'libddwaf failed to initialize')

@diagnostics = e.diagnostics if e.diagnostics

Expand All @@ -149,7 +149,7 @@ def create_waf_handle(settings, ruleset)
Datadog.logger.error do
"libddwaf failed to initialize, error: #{e.inspect}"
end
@telemetry.report(e, level: :error, description: 'libddwaf failed to initialize')
@telemetry.report(e, description: 'libddwaf failed to initialize')

false
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/appsec/processor/rule_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def load_rules(ruleset:, telemetry:)
"libddwaf ruleset failed to load, ruleset: #{ruleset.inspect} error: #{e.inspect}"
end

telemetry.report(e, level: :error, description: 'libddwaf ruleset failed to load')
telemetry.report(e, description: 'libddwaf ruleset failed to load')

nil
end
Expand Down
2 changes: 0 additions & 2 deletions lib/datadog/appsec/processor/rule_merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def merge(
Datadog.logger.error("libddwaf rulemerger failed to parse default waf processors. Error: #{e.inspect}")
telemetry.report(
e,
level: :error,
description: 'libddwaf rulemerger failed to parse default waf processors'
)
[]
Expand All @@ -43,7 +42,6 @@ def merge(
Datadog.logger.error("libddwaf rulemerger failed to parse default waf scanners. Error: #{e.inspect}")
telemetry.report(
e,
level: :error,
description: 'libddwaf rulemerger failed to parse default waf scanners'
)
[]
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/core/telemetry/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.from(exception)
end
end

def report(exception, level:, description: nil)
def report(exception, level: :error, description: nil)
# Annoymous exceptions to be logged as <Class:0x00007f8b1c0b3b40>
message = +''
message << (exception.class.name || exception.class.inspect)
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/profiling/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def flush_events
Datadog.logger.error(
"Unable to report profile. Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
)
Datadog::Core::Telemetry::Logger.report(e, level: :error, description: "Unable to report profile")
Datadog::Core::Telemetry::Logger.report(e, description: "Unable to report profile")
end

true
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/core/telemetry/logger.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Datadog
module Core
module Telemetry
module Logger
def self.report: (Exception exception, level: Symbol, ?description: String?) -> void
def self.report: (Exception exception, ?level: Symbol, ?description: String?) -> void

def self.error: (String description) -> void

Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/core/telemetry/logging.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Datadog
def self.from: (Exception exception) -> String?
end

def report: (Exception exception, level: Symbol, ?description: String?) -> void
def report: (Exception exception, ?level: Symbol, ?description: String?) -> void

def error: (String description) -> void
end
Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/appsec/processor/rule_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
it 'returns `nil`' do
expect(telemetry).to receive(:report).with(
an_instance_of(Errno::ENOENT),
level: :error,
description: 'libddwaf ruleset failed to load'
)

Expand Down Expand Up @@ -95,7 +94,6 @@
it 'returns `nil`' do
expect(telemetry).to receive(:report).with(
an_instance_of(JSON::ParserError),
level: :error,
description: 'libddwaf ruleset failed to load'
)

Expand Down
2 changes: 0 additions & 2 deletions spec/datadog/appsec/processor/rule_merger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,6 @@
expect(Datadog.logger).to receive(:error).with(/libddwaf rulemerger failed to parse default waf processors/)
expect(telemetry).to receive(:report).with(
a_kind_of(StandardError),
level: :error,
description: 'libddwaf rulemerger failed to parse default waf processors'
)

Expand Down Expand Up @@ -749,7 +748,6 @@
expect(Datadog.logger).to receive(:error).with(/libddwaf rulemerger failed to parse default waf scanners/)
expect(telemetry).to receive(:report).with(
a_kind_of(StandardError),
level: :error,
description: 'libddwaf rulemerger failed to parse default waf scanners'
)

Expand Down
6 changes: 0 additions & 6 deletions spec/datadog/appsec/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
allow_any_instance_of(described_class).to receive(:require).with('libddwaf').and_raise(LoadError)
expect(telemetry).to receive(:report).with(
an_instance_of(LoadError),
level: :error,
description: 'libddwaf failed to load'
).at_least(:once)

Expand Down Expand Up @@ -152,7 +151,6 @@ def required_addresses
)
expect(telemetry).to receive(:report).with(
a_kind_of(Datadog::AppSec::WAF::LibDDWAF::Error),
level: :error,
description: 'libddwaf failed to initialize'
)

Expand Down Expand Up @@ -190,7 +188,6 @@ def required_addresses
)
expect(telemetry).to receive(:report).with(
a_kind_of(StandardError),
level: :error,
description: 'libddwaf failed to initialize'
)

Expand Down Expand Up @@ -239,7 +236,6 @@ def required_addresses
expect(Datadog.logger).to receive(:warn)
expect(telemetry).to receive(:report).with(
a_kind_of(StandardError),
level: :error,
description: 'libddwaf failed to initialize'
)

Expand All @@ -258,7 +254,6 @@ def required_addresses
expect(Datadog::AppSec::WAF::Handle).to receive(:new).and_raise(StandardError)
expect(telemetry).to receive(:report).with(
an_instance_of(StandardError),
level: :error,
description: 'libddwaf failed to initialize'
)

Expand All @@ -282,7 +277,6 @@ def diagnostics
expect(Datadog::AppSec::WAF::Handle).to receive(:new).and_raise(Datadog::AppSec::WAF::LibDDWAF::Error)
expect(telemetry).to receive(:report).with(
an_instance_of(Datadog::AppSec::WAF::LibDDWAF::Error),
level: :error,
description: 'libddwaf failed to initialize'
)

Expand Down
8 changes: 8 additions & 0 deletions spec/datadog/core/telemetry/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@
end
end

describe 'includes Datadog::Core::Telemetry::Logging' do
after do
telemetry.stop!
end

it { is_expected.to a_kind_of(Datadog::Core::Telemetry::Logging) }
end

describe '#log!' do
after do
telemetry.stop!
Expand Down
2 changes: 1 addition & 1 deletion spec/datadog/profiling/scheduler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
it "gracefully handles the exception, logging it" do
expect(Datadog.logger).to receive(:error).with(/Kaboom/)
expect(Datadog::Core::Telemetry::Logger).to receive(:report)
.with(an_instance_of(RuntimeError), level: :error, description: "Unable to report profile")
.with(an_instance_of(RuntimeError), description: "Unable to report profile")

flush_events
end
Expand Down

0 comments on commit 80356eb

Please sign in to comment.