0
0
Apache Airflowdevops~7 mins

Airflow metrics with Prometheus - Commands & Configuration

Choose your learning style9 modes available
Introduction
When running many workflows, it is hard to know how well Airflow is performing or if tasks are failing often. Prometheus helps by collecting detailed metrics from Airflow so you can watch its health and performance in real time.
When you want to monitor how many tasks are running or failing in Airflow over time
When you need to alert your team if Airflow scheduler or workers stop working
When you want to see how long tasks take to run and identify slow workflows
When you want to collect metrics from Airflow and visualize them in Grafana dashboards
When you want to track Airflow resource usage and task success rates automatically
Config File - airflow.cfg
airflow.cfg
[metrics]
# Enable StatsD metrics exporter
statsd_on = True
statsd_host = localhost
statsd_port = 8125
statsd_prefix = airflow

[metrics.prometheus]
enabled = True
port = 9090

[webserver]
# Expose Prometheus metrics endpoint
expose_prometheus = True

This configuration enables Airflow to send metrics to Prometheus.

statsd_on turns on metrics collection.

statsd_host and statsd_port tell Airflow where the StatsD server listens.

expose_prometheus in the webserver section allows Prometheus to scrape metrics directly from Airflow's webserver.

Commands
Starts the Airflow webserver which now exposes Prometheus metrics on port 8080.
Terminal
airflow webserver --port 8080
Expected OutputExpected
[2024-06-01 12:00:00,000] {webserver.py:123} INFO - Starting Airflow webserver on port 8080 [2024-06-01 12:00:01,000] {prometheus.py:45} INFO - Prometheus metrics endpoint enabled at /metrics
--port - Specifies the port where the Airflow webserver listens
Starts the Airflow scheduler which runs tasks and updates metrics that Prometheus will collect.
Terminal
airflow scheduler
Expected OutputExpected
[2024-06-01 12:00:05,000] {scheduler.py:78} INFO - Starting the scheduler [2024-06-01 12:00:06,000] {scheduler.py:90} INFO - Scheduler heartbeat
Fetches the raw Prometheus metrics from Airflow's metrics endpoint to verify metrics are exposed.
Terminal
curl http://localhost:8080/metrics
Expected OutputExpected
# HELP airflow_task_duration_seconds Duration of Airflow tasks in seconds # TYPE airflow_task_duration_seconds histogram airflow_task_duration_seconds_bucket{task_id="example_task",le="5"} 3 airflow_task_duration_seconds_bucket{task_id="example_task",le="10"} 5 # HELP airflow_scheduler_heartbeat_seconds Time between scheduler heartbeats # TYPE airflow_scheduler_heartbeat_seconds gauge airflow_scheduler_heartbeat_seconds 1.0
Starts Prometheus server with configuration to scrape Airflow metrics from the webserver endpoint.
Terminal
prometheus --config.file=prometheus.yml
Expected OutputExpected
level=info ts=2024-06-01T12:00:10.000Z caller=main.go:123 msg="Starting Prometheus" version="2.45.0" level=info ts=2024-06-01T12:00:10.001Z caller=main.go:456 msg="Loading configuration file" filename=prometheus.yml level=info ts=2024-06-01T12:00:10.002Z caller=main.go:789 msg="Server is ready to receive web requests."
--config.file - Specifies the Prometheus configuration file to use
Key Concept

If you remember nothing else from this pattern, remember: enabling Prometheus metrics in Airflow lets you monitor task and scheduler health in real time.

Common Mistakes
Not enabling the Prometheus metrics endpoint in airflow.cfg
Prometheus cannot scrape metrics if Airflow does not expose them, so no data is collected.
Set 'expose_prometheus = True' under the [webserver] section in airflow.cfg.
Starting Prometheus without configuring it to scrape Airflow metrics
Prometheus will run but will not collect any Airflow metrics, making monitoring useless.
Add Airflow's metrics endpoint to prometheus.yml scrape_configs.
Running Airflow webserver on a different port than Prometheus expects
Prometheus will fail to connect and collect metrics, causing missing data.
Ensure the webserver port matches the one configured in Prometheus scrape target.
Summary
Configure airflow.cfg to enable Prometheus metrics and expose the metrics endpoint.
Start Airflow webserver and scheduler to generate and expose metrics.
Verify metrics are available by fetching the /metrics endpoint.
Run Prometheus with a config that scrapes Airflow metrics for monitoring.