Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics basic deprecated validators removed in favor of pattern matching #41975

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions airflow/config_templates/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1073,34 +1073,21 @@ metrics:
description: |
`StatsD <https://github.com/statsd/statsd>`__ integration settings.
options:
metrics_use_pattern_match:
description: |
If true, ``[metrics] metrics_allow_list`` and ``[metrics] metrics_block_list`` will use
regex pattern matching anywhere within the metric name instead of only prefix matching
at the start of the name.
version_added: 2.9.0
type: boolean
example: ~
default: "False"
metrics_allow_list:
description: |
Configure an allow list (comma separated string) to send only certain metrics.
If ``[metrics] metrics_use_pattern_match`` is ``false``, match only the exact metric name prefix.
If ``[metrics] metrics_use_pattern_match`` is ``true``, provide regex patterns to match.
Configure an allow list (comma separated regex patterns to match) to send only certain metrics.
version_added: 2.6.0
type: string
example: "\"scheduler,executor,dagrun,pool,triggerer,celery\"
or \"^scheduler,^executor,heartbeat|timeout\""
default: ""
metrics_block_list:
description: |
Configure a block list (comma separated string) to block certain metrics from being emitted.
Configure a block list (comma separated regex patterns to match) to block certain metrics
from being emitted.
If ``[metrics] metrics_allow_list`` and ``[metrics] metrics_block_list`` are both configured,
``[metrics] metrics_block_list`` is ignored.

If ``[metrics] metrics_use_pattern_match`` is ``false``, match only the exact metric name prefix.

If ``[metrics] metrics_use_pattern_match`` is ``true``, provide regex patterns to match.
version_added: 2.6.0
type: string
example: "\"scheduler,executor,dagrun,pool,triggerer,celery\"
Expand Down
12 changes: 7 additions & 5 deletions airflow/metrics/datadog_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
from airflow.configuration import conf
from airflow.metrics.protocols import Timer
from airflow.metrics.validators import (
AllowListValidator,
BlockListValidator,
PatternAllowListValidator,
PatternBlockListValidator,
get_validator,
validate_stat,
)
Expand All @@ -47,9 +47,9 @@ class SafeDogStatsdLogger:
def __init__(
self,
dogstatsd_client: DogStatsd,
metrics_validator: ListValidator = AllowListValidator(),
metrics_validator: ListValidator = PatternAllowListValidator(),
metrics_tags: bool = False,
metric_tags_validator: ListValidator = AllowListValidator(),
metric_tags_validator: ListValidator = PatternAllowListValidator(),
) -> None:
self.dogstatsd = dogstatsd_client
self.metrics_validator = metrics_validator
Expand Down Expand Up @@ -168,5 +168,7 @@ def get_dogstatsd_logger(cls) -> SafeDogStatsdLogger:
constant_tags=cls.get_constant_tags(),
)
datadog_metrics_tags = conf.getboolean("metrics", "statsd_datadog_metrics_tags", fallback=True)
metric_tags_validator = BlockListValidator(conf.get("metrics", "statsd_disabled_tags", fallback=None))
metric_tags_validator = PatternBlockListValidator(
conf.get("metrics", "statsd_disabled_tags", fallback=None)
)
return SafeDogStatsdLogger(dogstatsd, get_validator(), datadog_metrics_tags, metric_tags_validator)
4 changes: 2 additions & 2 deletions airflow/metrics/otel_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
from airflow.metrics.protocols import Timer
from airflow.metrics.validators import (
OTEL_NAME_MAX_LENGTH,
AllowListValidator,
ListValidator,
PatternAllowListValidator,
get_validator,
stat_name_otel_handler,
)
Expand Down Expand Up @@ -170,7 +170,7 @@ def __init__(
self,
otel_provider,
prefix: str = DEFAULT_METRIC_NAME_PREFIX,
metrics_validator: ListValidator = AllowListValidator(),
metrics_validator: ListValidator = PatternAllowListValidator(),
):
self.otel: Callable = otel_provider
self.prefix: str = prefix
Expand Down
12 changes: 7 additions & 5 deletions airflow/metrics/statsd_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
from airflow.exceptions import AirflowConfigException
from airflow.metrics.protocols import Timer
from airflow.metrics.validators import (
AllowListValidator,
BlockListValidator,
PatternAllowListValidator,
PatternBlockListValidator,
get_validator,
validate_stat,
)
Expand Down Expand Up @@ -70,9 +70,9 @@ class SafeStatsdLogger:
def __init__(
self,
statsd_client: StatsClient,
metrics_validator: ListValidator = AllowListValidator(),
metrics_validator: ListValidator = PatternAllowListValidator(),
influxdb_tags_enabled: bool = False,
metric_tags_validator: ListValidator = AllowListValidator(),
metric_tags_validator: ListValidator = PatternAllowListValidator(),
) -> None:
self.statsd = statsd_client
self.metrics_validator = metrics_validator
Expand Down Expand Up @@ -180,5 +180,7 @@ def get_statsd_logger(cls) -> SafeStatsdLogger:
)

influxdb_tags_enabled = conf.getboolean("metrics", "statsd_influxdb_enabled", fallback=False)
metric_tags_validator = BlockListValidator(conf.get("metrics", "statsd_disabled_tags", fallback=None))
metric_tags_validator = PatternBlockListValidator(
conf.get("metrics", "statsd_disabled_tags", fallback=None)
)
return SafeStatsdLogger(statsd, get_validator(), influxdb_tags_enabled, metric_tags_validator)
39 changes: 4 additions & 35 deletions airflow/metrics/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import re2

from airflow.configuration import conf
from airflow.exceptions import InvalidStatsNameException, RemovedInAirflow3Warning
from airflow.exceptions import InvalidStatsNameException

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -88,25 +88,14 @@ class MetricNameLengthExemptionWarning(Warning):

def get_validator() -> ListValidator:
validators = {
"basic": {"allow": AllowListValidator, "block": BlockListValidator},
"pattern": {"allow": PatternAllowListValidator, "block": PatternBlockListValidator},
"allow": PatternAllowListValidator,
"block": PatternBlockListValidator,
}
metric_lists = {
"allow": (metric_allow_list := conf.get("metrics", "metrics_allow_list", fallback=None)),
"block": (metric_block_list := conf.get("metrics", "metrics_block_list", fallback=None)),
}

use_pattern = conf.getboolean("metrics", "metrics_use_pattern_match", fallback=False)
validator_type = "pattern" if use_pattern else "basic"

if not use_pattern:
warnings.warn(
"The basic metric validator will be deprecated in the future in favor of pattern-matching. "
"You can try this now by setting config option metrics_use_pattern_match to True.",
RemovedInAirflow3Warning,
stacklevel=2,
)

if metric_allow_list:
list_type = "allow"
if metric_block_list:
Expand All @@ -118,7 +107,7 @@ def get_validator() -> ListValidator:
else:
list_type = DEFAULT_VALIDATOR_TYPE

return validators[validator_type][list_type](metric_lists[list_type])
return validators[list_type](metric_lists[list_type])
potiuk marked this conversation as resolved.
Show resolved Hide resolved


def validate_stat(fn: Callable) -> Callable:
Expand Down Expand Up @@ -263,16 +252,6 @@ def _has_pattern_match(self, name: str) -> bool:
return False


class AllowListValidator(ListValidator):
"""AllowListValidator only allows names that match the allowed prefixes."""

def test(self, name: str) -> bool:
if self.validate_list is not None:
return name.strip().lower().startswith(self.validate_list)
else:
return True # default is all metrics are allowed


class PatternAllowListValidator(ListValidator):
"""Match the provided strings anywhere in the metric name."""

Expand All @@ -283,16 +262,6 @@ def test(self, name: str) -> bool:
return True # default is all metrics are allowed


class BlockListValidator(ListValidator):
"""BlockListValidator only allows names that do not match the blocked prefixes."""

def test(self, name: str) -> bool:
if self.validate_list is not None:
return not name.strip().lower().startswith(self.validate_list)
else:
return True # default is all metrics are allowed


class PatternBlockListValidator(ListValidator):
"""Only allow names that do not match the blocked strings."""

Expand Down
1 change: 1 addition & 0 deletions newsfragments/41975.significant.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Metrics basic deprecated validators (``AllowListValidator`` and ``BlockListValidator``) were removed in favor of pattern matching. Pattern matching validators (``PatternAllowListValidator`` and ``PatternBlockListValidator``) are enabled by default. Configuration parameter ``metrics_use_pattern_match``was removed from the ``metrics`` section.
Loading
Loading