Skip to content

Commit bd7f202

Browse files
chore(aap): report config_errors for waf init failure (#13690)
- Ensure the `waf.config_errors` metrics is generated when there is a waf failed initialisation or update. - Improve typing in telemetry module for appsec - This will also be tested in system tests DataDog/system-tests#4788 APPSEC-58013 ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent f1cfa5e commit bd7f202

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

ddtrace/appsec/_metrics.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ddtrace.appsec import _asm_request_context
44
from ddtrace.appsec import _constants
55
from ddtrace.appsec._deduplications import deduplication
6+
from ddtrace.appsec._utils import DDWaf_info
67
from ddtrace.internal import telemetry
78
import ddtrace.internal.logger as ddlogger
89
from ddtrace.internal.telemetry.constants import TELEMETRY_LOG_LEVEL
@@ -53,35 +54,35 @@ def _set_waf_error_log(msg: str, version: str, error_level: bool = True) -> None
5354
logger.warning(WARNING_TAGS.TELEMETRY_METRICS, extra=extra, exc_info=True)
5455

5556

56-
def _set_waf_updates_metric(info, success: bool):
57+
def _set_waf_updates_metric(info: DDWaf_info, success: bool):
5758
try:
58-
if info:
59-
tags: typing.Tuple[typing.Tuple[str, str], ...] = (
60-
("event_rules_version", info.version or UNKNOWN_VERSION),
61-
("waf_version", ddwaf_version),
62-
("success", bool_str[success]),
63-
)
64-
else:
65-
tags = (("waf_version", ddwaf_version), ("success", bool_str[success]))
59+
tags: typing.Tuple[typing.Tuple[str, str], ...] = (
60+
("event_rules_version", info.version or UNKNOWN_VERSION),
61+
("waf_version", ddwaf_version),
62+
)
6663

67-
telemetry.telemetry_writer.add_count_metric(TELEMETRY_NAMESPACE.APPSEC, "waf.updates", 1, tags=tags)
64+
telemetry.telemetry_writer.add_count_metric(
65+
TELEMETRY_NAMESPACE.APPSEC, "waf.updates", 1, tags=tags + (("success", bool_str[success]),)
66+
)
67+
if not success:
68+
telemetry.telemetry_writer.add_count_metric(TELEMETRY_NAMESPACE.APPSEC, "waf.config_errors", 1, tags=tags)
6869
except Exception:
6970
extra = {"product": "appsec", "exec_limit": 6, "more_info": ":waf:updates"}
7071
logger.warning(WARNING_TAGS.TELEMETRY_METRICS, extra=extra, exc_info=True)
7172

7273

73-
def _set_waf_init_metric(info, success: bool):
74+
def _set_waf_init_metric(info: DDWaf_info, success: bool):
7475
try:
75-
if info:
76-
tags: typing.Tuple[typing.Tuple[str, str], ...] = (
77-
("event_rules_version", info.version or UNKNOWN_VERSION),
78-
("waf_version", ddwaf_version),
79-
("success", bool_str[success]),
80-
)
81-
else:
82-
tags = (("waf_version", ddwaf_version), ("success", bool_str[success]))
76+
tags: typing.Tuple[typing.Tuple[str, str], ...] = (
77+
("event_rules_version", info.version or UNKNOWN_VERSION),
78+
("waf_version", ddwaf_version),
79+
)
8380

84-
telemetry.telemetry_writer.add_count_metric(TELEMETRY_NAMESPACE.APPSEC, "waf.init", 1, tags=tags)
81+
telemetry.telemetry_writer.add_count_metric(
82+
TELEMETRY_NAMESPACE.APPSEC, "waf.init", 1, tags=tags + (("success", bool_str[success]),)
83+
)
84+
if not success:
85+
telemetry.telemetry_writer.add_count_metric(TELEMETRY_NAMESPACE.APPSEC, "waf.config_errors", 1, tags=tags)
8586
except Exception:
8687
extra = {"product": "appsec", "exec_limit": 6, "more_info": ":waf:init"}
8788
logger.warning(WARNING_TAGS.TELEMETRY_METRICS, extra=extra, exc_info=True)

0 commit comments

Comments
 (0)