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

[AIP-49] Decouple metrics clients and validators into their own modules #30802

Merged
merged 3 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions airflow/metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
107 changes: 107 additions & 0 deletions airflow/metrics/base_stats_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from __future__ import annotations

from airflow.metrics.protocols import DeltaType, Timer, TimerProtocol
from airflow.typing_compat import Protocol


class StatsLogger(Protocol):
"""This class is only used for TypeChecking (for IDEs, mypy, etc)."""

@classmethod
def incr(
cls,
stat: str,
count: int = 1,
rate: int | float = 1,
*,
tags: dict[str, str] | None = None,
) -> None:
"""Increment stat."""

@classmethod
def decr(
cls,
stat: str,
count: int = 1,
rate: int | float = 1,
*,
tags: dict[str, str] | None = None,
) -> None:
"""Decrement stat."""

@classmethod
def gauge(
cls,
stat: str,
value: float,
rate: int | float = 1,
delta: bool = False,
*,
tags: dict[str, str] | None = None,
) -> None:
"""Gauge stat."""

@classmethod
def timing(
cls,
stat: str,
dt: DeltaType | None,
*,
tags: dict[str, str] | None = None,
) -> None:
"""Stats timing."""

@classmethod
def timer(cls, *args, **kwargs) -> TimerProtocol:
"""Timer metric that can be cancelled."""
raise NotImplementedError()


class NoStatsLogger:
"""If no StatsLogger is configured, NoStatsLogger is used as a fallback."""

@classmethod
def incr(cls, stat: str, count: int = 1, rate: int = 1, *, tags: dict[str, str] | None = None) -> None:
"""Increment stat."""

@classmethod
def decr(cls, stat: str, count: int = 1, rate: int = 1, *, tags: dict[str, str] | None = None) -> None:
"""Decrement stat."""

@classmethod
def gauge(
cls,
stat: str,
value: int,
rate: int = 1,
delta: bool = False,
*,
tags: dict[str, str] | None = None,
) -> None:
"""Gauge stat."""

@classmethod
def timing(cls, stat: str, dt: DeltaType, *, tags: dict[str, str] | None = None) -> None:
"""Stats timing."""

@classmethod
def timer(cls, *args, **kwargs) -> TimerProtocol:
"""Timer metric that can be cancelled."""
return Timer()
180 changes: 180 additions & 0 deletions airflow/metrics/datadog_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from __future__ import annotations

import datetime
import logging
from typing import TYPE_CHECKING

from airflow.configuration import conf
from airflow.metrics.protocols import DeltaType, Timer, TimerProtocol
from airflow.metrics.validators import (
AllowListValidator,
BlockListValidator,
ListValidator,
validate_stat,
)

if TYPE_CHECKING:
from datadog import DogStatsd

log = logging.getLogger(__name__)


class SafeDogStatsdLogger:
"""DogStatsd Logger."""

def __init__(
self,
dogstatsd_client: DogStatsd,
metrics_validator: ListValidator = AllowListValidator(),
metrics_tags: bool = False,
metric_tags_validator: ListValidator = AllowListValidator(),
) -> None:
self.dogstatsd = dogstatsd_client
self.metrics_validator = metrics_validator
self.metrics_tags = metrics_tags
self.metric_tags_validator = metric_tags_validator

@validate_stat
def incr(
self,
stat: str,
count: int = 1,
rate: float = 1,
*,
tags: dict[str, str] | None = None,
) -> None:
"""Increment stat."""
if self.metrics_tags and isinstance(tags, dict):
tags_list = [
f"{key}:{value}" for key, value in tags.items() if self.metric_tags_validator.test(key)
]
else:
tags_list = []
if self.metrics_validator.test(stat):
return self.dogstatsd.increment(metric=stat, value=count, tags=tags_list, sample_rate=rate)
return None

@validate_stat
def decr(
self,
stat: str,
count: int = 1,
rate: float = 1,
*,
tags: dict[str, str] | None = None,
) -> None:
"""Decrement stat."""
if self.metrics_tags and isinstance(tags, dict):
tags_list = [
f"{key}:{value}" for key, value in tags.items() if self.metric_tags_validator.test(key)
]
else:
tags_list = []
if self.metrics_validator.test(stat):
return self.dogstatsd.decrement(metric=stat, value=count, tags=tags_list, sample_rate=rate)
return None

@validate_stat
def gauge(
self,
stat: str,
value: int | float,
rate: float = 1,
delta: bool = False,
*,
tags: dict[str, str] | None = None,
) -> None:
"""Gauge stat."""
if self.metrics_tags and isinstance(tags, dict):
tags_list = [
f"{key}:{value}" for key, value in tags.items() if self.metric_tags_validator.test(key)
]
else:
tags_list = []
if self.metrics_validator.test(stat):
return self.dogstatsd.gauge(metric=stat, value=value, tags=tags_list, sample_rate=rate)
return None

@validate_stat
def timing(
self,
stat: str,
dt: DeltaType,
*,
tags: dict[str, str] | None = None,
) -> None:
"""Stats timing."""
if self.metrics_tags and isinstance(tags, dict):
tags_list = [
f"{key}:{value}" for key, value in tags.items() if self.metric_tags_validator.test(key)
]
else:
tags_list = []
if self.metrics_validator.test(stat):
if isinstance(dt, datetime.timedelta):
dt = dt.total_seconds()
return self.dogstatsd.timing(metric=stat, value=dt, tags=tags_list)
return None

@validate_stat
def timer(
self,
stat: str | None = None,
tags: dict[str, str] | None = None,
**kwargs,
) -> TimerProtocol:
"""Timer metric that can be cancelled."""
if self.metrics_tags and isinstance(tags, dict):
tags_list = [
f"{key}:{value}" for key, value in tags.items() if self.metric_tags_validator.test(key)
]
else:
tags_list = []
if stat and self.metrics_validator.test(stat):
return Timer(self.dogstatsd.timed(stat, tags=tags_list, **kwargs))
return Timer()


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)
Loading