Skip to content

Commit

Permalink
add internal error telemetry log test
Browse files Browse the repository at this point in the history
  • Loading branch information
christophe-papazian committed Oct 28, 2024
1 parent f35624d commit 7ef8470
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/appsec/appsec/test_telemetry.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7ef8470

Please sign in to comment.