|
23 | 23 | from airflow._shared.observability.metrics import statsd_logger |
24 | 24 | from airflow.configuration import conf |
25 | 25 |
|
| 26 | +log = logging.getLogger(__name__) |
| 27 | + |
26 | 28 | if TYPE_CHECKING: |
27 | 29 | from airflow._shared.observability.metrics.statsd_logger import SafeStatsdLogger |
28 | 30 |
|
29 | | -log = logging.getLogger(__name__) |
30 | | - |
31 | 31 |
|
32 | 32 | def get_statsd_logger() -> SafeStatsdLogger: |
33 | | - stats_class = conf.getimport("metrics", "statsd_custom_client_path", fallback=None) |
| 33 | + # Local import to avoid requiring statsd when other backends are used (e.g. Datadog) |
| 34 | + from statsd import StatsClient, UnixSocketStatsClient |
34 | 35 |
|
35 | | - # no need to check for the scheduler/statsd_on -> this method is only called when it is set |
36 | | - # and previously it would crash with None is callable if it was called without it. |
37 | | - from statsd import StatsClient |
| 36 | + socket_path = conf.get("metrics", "statsd_socket_path", fallback=None) or None |
| 37 | + custom_class = conf.getimport("metrics", "statsd_custom_client_path", fallback=None) |
38 | 38 |
|
39 | | - if stats_class: |
40 | | - if not issubclass(stats_class, StatsClient): |
| 39 | + if custom_class is not None: |
| 40 | + if socket_path is not None: |
| 41 | + if not issubclass(custom_class, UnixSocketStatsClient): |
| 42 | + raise AirflowConfigException( |
| 43 | + "Your custom StatsD client must extend the statsd.UnixSocketStatsClient " |
| 44 | + "when using a socket path in order to ensure backwards compatibility." |
| 45 | + ) |
| 46 | + elif not issubclass(custom_class, StatsClient): |
41 | 47 | raise AirflowConfigException( |
42 | | - "Your custom StatsD client must extend the statsd.StatsClient in order to ensure " |
43 | | - "backwards compatibility." |
| 48 | + "Your custom StatsD client must extend the statsd.StatsClient in order " |
| 49 | + "to ensure backwards compatibility." |
44 | 50 | ) |
45 | 51 | log.info("Successfully loaded custom StatsD client") |
46 | 52 |
|
47 | | - else: |
48 | | - stats_class = StatsClient |
49 | | - |
50 | | - return statsd_logger.get_statsd_logger( |
51 | | - stats_class=stats_class, |
| 53 | + common_kwargs = { |
| 54 | + "stats_class": custom_class, |
| 55 | + "prefix": conf.get("metrics", "statsd_prefix"), |
| 56 | + "influxdb_tags_enabled": conf.getboolean("metrics", "statsd_influxdb_enabled", fallback=False), |
| 57 | + "statsd_disabled_tags": conf.get("metrics", "statsd_disabled_tags", fallback=None), |
| 58 | + "metrics_allow_list": conf.get("metrics", "metrics_allow_list", fallback=None), |
| 59 | + "metrics_block_list": conf.get("metrics", "metrics_block_list", fallback=None), |
| 60 | + "stat_name_handler": conf.getimport("metrics", "stat_name_handler"), |
| 61 | + "statsd_influxdb_enabled": conf.getboolean("metrics", "statsd_influxdb_enabled", fallback=False), |
| 62 | + } |
| 63 | + if socket_path is not None: |
| 64 | + return statsd_logger.get_socket_statsd_logger(socket_path=socket_path, **common_kwargs) |
| 65 | + return statsd_logger.get_udp_statsd_logger( |
52 | 66 | host=conf.get("metrics", "statsd_host"), |
53 | 67 | port=conf.getint("metrics", "statsd_port"), |
54 | | - prefix=conf.get("metrics", "statsd_prefix"), |
55 | 68 | ipv6=conf.getboolean("metrics", "statsd_ipv6", fallback=False), |
56 | | - influxdb_tags_enabled=conf.getboolean("metrics", "statsd_influxdb_enabled", fallback=False), |
57 | | - statsd_disabled_tags=conf.get("metrics", "statsd_disabled_tags", fallback=None), |
58 | | - metrics_allow_list=conf.get("metrics", "metrics_allow_list", fallback=None), |
59 | | - metrics_block_list=conf.get("metrics", "metrics_block_list", fallback=None), |
60 | | - stat_name_handler=conf.getimport("metrics", "stat_name_handler"), |
61 | | - statsd_influxdb_enabled=conf.getboolean("metrics", "statsd_influxdb_enabled", fallback=False), |
| 69 | + **common_kwargs, |
62 | 70 | ) |
0 commit comments