Repository: apache/airflow

Issue:
Add a pattern-matching support for allow and block list in Datadog metric 
### Description

Currently, when exporting metrics to Datadog, we only do plain prefix matches for the allow list and block list.  I am requesting to include regex pattern-matching

### Use case/motivation

We are already supporting this for OTEL #36250. Adding this to Datadog will help to maintain a reduced number of patterns required in the allow/block list

### Related issues

_No response_

### Are you willing to submit a PR?

- [X] Yes I am willing to submit a PR!

### Code of Conduct

- [X] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)



Discussion:

Resolving patch:
diff --git a/airflow/metrics/datadog_logger.py b/airflow/metrics/datadog_logger.py
--- a/airflow/metrics/datadog_logger.py
+++ b/airflow/metrics/datadog_logger.py
@@ -26,6 +26,7 @@
 from airflow.metrics.validators import (
     AllowListValidator,
     BlockListValidator,
+    get_validator,
     validate_stat,
 )
 
@@ -160,25 +161,12 @@ def get_dogstatsd_logger(cls) -> SafeDogStatsdLogger:
     """Get DataDog StatsD logger."""
     from datadog import DogStatsd
 
-    metrics_validator: ListValidator
-
     dogstatsd = DogStatsd(
         host=conf.get("metrics", "statsd_host"),
         port=conf.getint("metrics", "statsd_port"),
         namespace=conf.get("metrics", "statsd_prefix"),
         constant_tags=cls.get_constant_tags(),
     )
-    if conf.get("metrics", "metrics_allow_list", fallback=None):
-        metrics_validator = AllowListValidator(conf.get("metrics", "metrics_allow_list"))
-        if conf.get("metrics", "metrics_block_list", fallback=None):
-            log.warning(
-                "Ignoring metrics_block_list as both metrics_allow_list "
-                "and metrics_block_list have been set"
-            )
-    elif conf.get("metrics", "metrics_block_list", fallback=None):
-        metrics_validator = BlockListValidator(conf.get("metrics", "metrics_block_list"))
-    else:
-        metrics_validator = AllowListValidator()
     datadog_metrics_tags = conf.getboolean("metrics", "statsd_datadog_metrics_tags", fallback=True)
     metric_tags_validator = BlockListValidator(conf.get("metrics", "statsd_disabled_tags", fallback=None))
-    return SafeDogStatsdLogger(dogstatsd, metrics_validator, datadog_metrics_tags, metric_tags_validator)
+    return SafeDogStatsdLogger(dogstatsd, get_validator(), datadog_metrics_tags, metric_tags_validator)


Test patch:
diff --git a/tests/core/test_stats.py b/tests/core/test_stats.py
--- a/tests/core/test_stats.py
+++ b/tests/core/test_stats.py
@@ -253,7 +253,9 @@ def test_enabled_by_config(self):
         """Test that enabling this sets the right instance properties"""
         from datadog import DogStatsd
 
-        with conf_vars({("metrics", "statsd_datadog_enabled"): "True"}):
+        with conf_vars(
+            {("metrics", "statsd_datadog_enabled"): "True", ("metrics", "metrics_use_pattern_match"): "True"}
+        ):
             importlib.reload(airflow.stats)
             assert isinstance(airflow.stats.Stats.dogstatsd, DogStatsd)
             assert not hasattr(airflow.stats.Stats, "statsd")
@@ -263,7 +265,13 @@ def test_enabled_by_config(self):
     def test_does_not_send_stats_using_statsd_when_statsd_and_dogstatsd_both_on(self):
         from datadog import DogStatsd
 
-        with conf_vars({("metrics", "statsd_on"): "True", ("metrics", "statsd_datadog_enabled"): "True"}):
+        with conf_vars(
+            {
+                ("metrics", "statsd_on"): "True",
+                ("metrics", "statsd_datadog_enabled"): "True",
+                ("metrics", "metrics_use_pattern_match"): "True",
+            }
+        ):
             importlib.reload(airflow.stats)
             assert isinstance(airflow.stats.Stats.dogstatsd, DogStatsd)
             assert not hasattr(airflow.stats.Stats, "statsd")
@@ -515,6 +523,7 @@ def test_does_not_send_stats_using_statsd_when_the_name_is_not_valid(self, mock_
     @conf_vars(
         {
             ("metrics", "statsd_datadog_enabled"): "True",
+            ("metrics", "metrics_use_pattern_match"): "True",
             ("metrics", "stat_name_handler"): "tests.core.test_stats.always_invalid",
         }
     )
@@ -539,6 +548,7 @@ def test_does_send_stats_using_statsd_when_the_name_is_valid(self, mock_statsd):
     @conf_vars(
         {
             ("metrics", "statsd_datadog_enabled"): "True",
+            ("metrics", "metrics_use_pattern_match"): "True",
             ("metrics", "stat_name_handler"): "tests.core.test_stats.always_valid",
         }
     )