diff --git a/airflow/metrics/datadog_logger.py b/airflow/metrics/datadog_logger.py index c2afe2bb80eb4..156407977305e 100644 --- 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) diff --git a/tests/core/test_stats.py b/tests/core/test_stats.py index 2c167d5d1eec0..4c8f6fb1d7c76 100644 --- 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", } )