|
| 1 | +import os |
| 2 | + |
| 3 | +from ddtrace.appsec._constants import IAST |
| 4 | +from ddtrace.internal.logger import get_logger |
| 5 | +from ddtrace.internal.telemetry import telemetry_metrics_writer |
| 6 | +from ddtrace.internal.telemetry.constants import TELEMETRY_NAMESPACE_TAG_IAST |
| 7 | + |
| 8 | + |
| 9 | +log = get_logger(__name__) |
| 10 | + |
| 11 | +TELEMETRY_OFF_NAME = "OFF" |
| 12 | +TELEMETRY_DEBUG_NAME = "DEBUG" |
| 13 | +TELEMETRY_MANDATORY_NAME = "MANDATORY" |
| 14 | +TELEMETRY_INFORMATION_NAME = "INFORMATION" |
| 15 | + |
| 16 | +TELEMETRY_DEBUG_VERBOSITY = 10 |
| 17 | +TELEMETRY_INFORMATION_VERBOSITY = 20 |
| 18 | +TELEMETRY_MANDATORY_VERBOSITY = 30 |
| 19 | +TELEMETRY_OFF_VERBOSITY = 40 |
| 20 | + |
| 21 | +METRICS_REPORT_LVLS = ( |
| 22 | + (TELEMETRY_DEBUG_VERBOSITY, TELEMETRY_DEBUG_NAME), |
| 23 | + (TELEMETRY_INFORMATION_VERBOSITY, TELEMETRY_INFORMATION_NAME), |
| 24 | + (TELEMETRY_MANDATORY_VERBOSITY, TELEMETRY_MANDATORY_NAME), |
| 25 | + (TELEMETRY_OFF_VERBOSITY, TELEMETRY_OFF_NAME), |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def get_iast_metrics_report_lvl(*args, **kwargs): |
| 30 | + report_lvl_name = os.environ.get(IAST.TELEMETRY_REPORT_LVL, TELEMETRY_INFORMATION_NAME).upper() |
| 31 | + report_lvl = 3 |
| 32 | + for lvl, lvl_name in METRICS_REPORT_LVLS: |
| 33 | + if report_lvl_name == lvl_name: |
| 34 | + return lvl |
| 35 | + return report_lvl |
| 36 | + |
| 37 | + |
| 38 | +def metric_verbosity(lvl): |
| 39 | + def wrapper(f): |
| 40 | + if lvl >= get_iast_metrics_report_lvl(): |
| 41 | + try: |
| 42 | + return f |
| 43 | + except Exception: |
| 44 | + log.warning("Error reporting IAST metrics", exc_info=True) |
| 45 | + return lambda: None # noqa: E731 |
| 46 | + |
| 47 | + return wrapper |
| 48 | + |
| 49 | + |
| 50 | +@metric_verbosity(TELEMETRY_MANDATORY_VERBOSITY) |
| 51 | +def _set_metric_iast_instrumented_source(source_type): |
| 52 | + telemetry_metrics_writer.add_count_metric( |
| 53 | + TELEMETRY_NAMESPACE_TAG_IAST, "instrumented.source", 1, (("source_type", source_type),) |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +@metric_verbosity(TELEMETRY_MANDATORY_VERBOSITY) |
| 58 | +def _set_metric_iast_instrumented_propagation(): |
| 59 | + telemetry_metrics_writer.add_count_metric(TELEMETRY_NAMESPACE_TAG_IAST, "instrumented.propagation", 1) |
| 60 | + |
| 61 | + |
| 62 | +@metric_verbosity(TELEMETRY_MANDATORY_VERBOSITY) |
| 63 | +def _set_metric_iast_instrumented_sink(vulnerability_type): |
| 64 | + telemetry_metrics_writer.add_count_metric( |
| 65 | + TELEMETRY_NAMESPACE_TAG_IAST, "instrumented.sink", 1, (("vulnerability_type", vulnerability_type),) |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +@metric_verbosity(TELEMETRY_INFORMATION_VERBOSITY) |
| 70 | +def _set_metric_iast_executed_source(source_type): |
| 71 | + telemetry_metrics_writer.add_count_metric( |
| 72 | + TELEMETRY_NAMESPACE_TAG_IAST, "executed.source", 1, (("source_type", source_type),) |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +@metric_verbosity(TELEMETRY_INFORMATION_VERBOSITY) |
| 77 | +def _set_metric_iast_executed_sink(vulnerability_type): |
| 78 | + telemetry_metrics_writer.add_count_metric( |
| 79 | + TELEMETRY_NAMESPACE_TAG_IAST, "executed.sink", 1, (("vulnerability_type", vulnerability_type),) |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +@metric_verbosity(TELEMETRY_INFORMATION_VERBOSITY) |
| 84 | +def _set_metric_iast_request_tainted(): |
| 85 | + telemetry_metrics_writer.add_count_metric(TELEMETRY_NAMESPACE_TAG_IAST, "request.tainted", 1) |
0 commit comments