From 413066ff2f649ea28fad8734a148d2d1f6ac2e8f Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Thu, 12 Dec 2024 13:35:27 +0100 Subject: [PATCH] [APPSEC-56128] Replace telemetry error with report --- lib/datadog/appsec/processor/context.rb | 9 +++++++-- spec/datadog/appsec/processor/context_spec.rb | 12 +++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/datadog/appsec/processor/context.rb b/lib/datadog/appsec/processor/context.rb index 7750d43dba0..c90552a2e2a 100644 --- a/lib/datadog/appsec/processor/context.rb +++ b/lib/datadog/appsec/processor/context.rb @@ -79,7 +79,7 @@ def try_run(persistent_data, ephemeral_data, timeout) @context.run(persistent_data, ephemeral_data, timeout) rescue WAF::LibDDWAF::Error => e Datadog.logger.debug { "#{@libddwaf_debug_tag} execution error: #{e} backtrace: #{e.backtrace&.first(3)}" } - @telemetry.report(e, description: 'libddwaf-rb internal low-level error') + @telemetry.report(e, description: "#{@libddwaf_debug_tag} internal low-level error") [:err_internal, WAF::Result.new(:err_internal, [], 0.0, false, [], [])] end @@ -92,8 +92,13 @@ def report_execution(result) else message = "#{@libddwaf_debug_tag} execution error: #{result.status.inspect}" + # NOTE: Ruby 3.4.0 allows to pass way faster `Thread::Backtrace::Location` + # via `caller_locations` method + error = WAF::LibDDWAF::Error.new(message) + error.set_backtrace(caller) + Datadog.logger.debug { message } - @telemetry.error(message) + @telemetry.report(error, description: message) end end diff --git a/spec/datadog/appsec/processor/context_spec.rb b/spec/datadog/appsec/processor/context_spec.rb index 7a435053528..83348c677c5 100644 --- a/spec/datadog/appsec/processor/context_spec.rb +++ b/spec/datadog/appsec/processor/context_spec.rb @@ -259,7 +259,9 @@ end it 'sends telemetry error' do - expect(telemetry).to receive(:error).with(/libddwaf:[\d.]+ method:ddwaf_run execution error: :err_invalid_object/) + expect(telemetry).to receive(:report) + .with(kind_of(Datadog::AppSec::WAF::LibDDWAF::Error), + description: /libddwaf:[\d.]+ method:ddwaf_run execution error: :err_invalid_object/) context.run(input, {}, timeout) end @@ -274,9 +276,13 @@ let(:result) { context.run(input, {}, timeout) } it 'sends telemetry report' do - expect(telemetry).to receive(:error).with(/libddwaf:[\d.]+ method:ddwaf_run execution error: :err_internal/) expect(telemetry).to receive(:report) - .with(kind_of(Datadog::AppSec::WAF::LibDDWAF::Error), description: 'libddwaf-rb internal low-level error') + .with(kind_of(Datadog::AppSec::WAF::LibDDWAF::Error), + description: /libddwaf:[\d.]+ method:ddwaf_run execution error: :err_internal/) + + expect(telemetry).to receive(:report) + .with(kind_of(Datadog::AppSec::WAF::LibDDWAF::Error), + description: /libddwaf:[\d.]+ method:ddwaf_run internal low-level error/) expect(result.status).to eq(:err_internal) end