Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/build/lib/airflow/stats.py: 67%
42 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
1#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18from __future__ import annotations
20import logging
21import socket
22from typing import TYPE_CHECKING, Callable
24from airflow.configuration import conf
25from airflow.metrics import datadog_logger, otel_logger, statsd_logger
26from airflow.metrics.base_stats_logger import NoStatsLogger, StatsLogger
28log = logging.getLogger(__name__)
31class _Stats(type):
32 factory: Callable
33 instance: StatsLogger | NoStatsLogger | None = None
35 def __getattr__(cls, name: str) -> str:
36 if not cls.instance:
37 try:
38 cls.instance = cls.factory()
39 except (socket.gaierror, ImportError) as e:
40 log.error("Could not configure StatsClient: %s, using NoStatsLogger instead.", e)
41 cls.instance = NoStatsLogger()
42 return getattr(cls.instance, name)
44 def __init__(cls, *args, **kwargs) -> None:
45 super().__init__(cls)
46 if not hasattr(cls.__class__, "factory"):
47 is_datadog_enabled_defined = conf.has_option("metrics", "statsd_datadog_enabled")
48 if is_datadog_enabled_defined and conf.getboolean("metrics", "statsd_datadog_enabled"):
49 cls.__class__.factory = datadog_logger.get_dogstatsd_logger
50 elif conf.getboolean("metrics", "statsd_on"):
51 cls.__class__.factory = statsd_logger.get_statsd_logger
52 elif conf.getboolean("metrics", "otel_on"):
53 cls.__class__.factory = otel_logger.get_otel_logger
54 else:
55 cls.__class__.factory = NoStatsLogger
57 @classmethod
58 def get_constant_tags(cls) -> list[str]:
59 """Get constant DataDog tags to add to all stats."""
60 tags: list[str] = []
61 tags_in_string = conf.get("metrics", "statsd_datadog_tags", fallback=None)
62 if tags_in_string is None or tags_in_string == "":
63 return tags
64 else:
65 for key_value in tags_in_string.split(","):
66 tags.append(key_value)
67 return tags
70if TYPE_CHECKING:
71 Stats: StatsLogger
72else:
74 class Stats(metaclass=_Stats):
75 """Empty class for Stats - we use metaclass to inject the right one."""