Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Apache Airflow Stats Initialize

From Leeroopedia


Knowledge Sources
Domains Observability, Python_API
Last Updated 2026-02-08 00:00 GMT

Overview

Concrete tool for initializing and emitting Airflow metrics provided by the Stats metaclass and backend loggers.

Description

The Stats class uses a metaclass (_Stats) to lazily initialize the appropriate metrics backend. Stats.initialize() selects between StatsD, OpenTelemetry, or Datadog loggers based on configuration flags. All Airflow components emit metrics through the Stats facade, which delegates to the configured backend.

Usage

Stats is initialized automatically by the Airflow runtime. Metrics configuration is set in airflow.cfg under the [metrics] section.

Code Reference

Source Location

  • Repository: Apache Airflow
  • File: shared/observability/src/airflow_shared/observability/metrics/stats.py

Signature

class _Stats(type):
    factory: Callable[[], StatsLogger | NoStatsLogger] | None = None
    instance: StatsLogger | NoStatsLogger | None = None

    def initialize(
        cls,
        *,
        is_statsd_datadog_enabled: bool,
        is_statsd_on: bool,
        is_otel_on: bool,
    ) -> None:
        """Initialize the metrics backend based on configuration."""
        ...

    @classmethod
    def get_constant_tags(cls, *, tags_in_string: str | None) -> list[str]:
        ...

class Stats(metaclass=_Stats):
    """Empty class for Stats — metaclass injects the right backend."""

Import

from airflow_shared.observability.metrics.stats import Stats

I/O Contract

Inputs

Name Type Required Description
is_statsd_on bool Yes Enable StatsD metrics
is_otel_on bool Yes Enable OpenTelemetry metrics
is_statsd_datadog_enabled bool Yes Enable Datadog StatsD

Outputs

Name Type Description
Metrics StatsD/OTel/Datadog Counters, gauges, timers emitted to configured backend
Traces OTel spans Distributed traces (if OTel enabled)

Usage Examples

Helm Chart Metrics Configuration

# values.yaml — Enable StatsD with Prometheus
statsd:
  enabled: true
  port: 9125
  prefix: airflow

# Or via airflow.cfg
# [metrics]
# statsd_on = True
# statsd_host = localhost
# statsd_port = 9125
# statsd_prefix = airflow

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment