From 7ef8470e4cb24ab68009657a8aff1554cd5a3105 Mon Sep 17 00:00:00 2001 From: Christophe Papazian Date: Mon, 28 Oct 2024 10:39:20 +0100 Subject: [PATCH] add internal error telemetry log test --- tests/appsec/appsec/test_telemetry.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/appsec/appsec/test_telemetry.py b/tests/appsec/appsec/test_telemetry.py index 0ea1ca5a6f..8678820e8d 100644 --- a/tests/appsec/appsec/test_telemetry.py +++ b/tests/appsec/appsec/test_telemetry.py @@ -1,9 +1,13 @@ import os from time import sleep +import mock import pytest +from ddtrace import tracer +import ddtrace.appsec._asm_request_context as asm_request_context from ddtrace.appsec._ddwaf import version +import ddtrace.appsec._ddwaf.ddwaf_types from ddtrace.appsec._deduplications import deduplication from ddtrace.appsec._processor import AppSecSpanProcessor from ddtrace.contrib.trace_utils import set_http_meta @@ -154,6 +158,28 @@ def test_log_metric_error_ddwaf_update(telemetry_writer): assert "waf_version:{}".format(version()) in list_metrics_logs[0]["tags"] +unpatched_run = ddtrace.appsec._ddwaf.ddwaf_types.ddwaf_run + + +def _wrapped_run(*args, **kwargs): + unpatched_run(*args, **kwargs) + return -3 + + +@mock.patch.object(ddtrace.appsec._ddwaf, "ddwaf_run", new=_wrapped_run) +def test_log_metric_error_ddwaf_internal_error(telemetry_writer): + """Test that an internal error is logged when the WAF returns an internal error.""" + with override_global_config(dict(_asm_enabled=True, _deduplication_enabled=False)): + with tracer.trace("test", span_type=SpanTypes.WEB, service="test") as span: + span_processor = AppSecSpanProcessor() + span_processor.on_span_start(span) + asm_request_context._call_waf(span, {}) + list_metrics_logs = list(telemetry_writer._logs) + assert len(list_metrics_logs) == 1 + assert list_metrics_logs[0]["message"] == "appsec.waf.request::error::-3" + assert "waf_version:{}".format(version()) in list_metrics_logs[0]["tags"] + + def test_log_metric_error_ddwaf_update_deduplication(telemetry_writer): with override_global_config(dict(_asm_enabled=True)): span_processor = AppSecSpanProcessor()