Bird
Raised Fist0
Apache Airflowdevops~30 mins

Airflow metrics with Prometheus - Mini Project: Build & Apply

Choose your learning style10 modes available
Airflow Metrics with Prometheus
📖 Scenario: You are a DevOps engineer setting up monitoring for Apache Airflow using Prometheus. Monitoring helps you see how your Airflow workflows perform and catch problems early.In this project, you will create a simple Airflow DAG, configure Prometheus metrics collection, and verify the metrics output.
🎯 Goal: Build a basic Airflow DAG that exposes Prometheus metrics. Configure Airflow to enable Prometheus metrics export. Finally, check the metrics output to confirm it works.
📋 What You'll Learn
Create a simple Airflow DAG with one task
Enable Prometheus metrics in Airflow configuration
Add a Prometheus metrics exporter to Airflow
Verify metrics output by printing metrics endpoint response
💡 Why This Matters
🌍 Real World
Monitoring Airflow workflows helps detect failures and performance issues early. Prometheus is a popular tool to collect and visualize such metrics.
💼 Career
DevOps engineers and data engineers often set up monitoring for workflow tools like Airflow to ensure reliability and optimize resource use.
Progress0 / 4 steps
1
Create a simple Airflow DAG
Create a Python file called example_dag.py with a DAG named example_prometheus_dag. Add one task called print_hello that uses PythonOperator to print "Hello Airflow".
Apache Airflow
Hint

Use DAG and PythonOperator from Airflow. Define a function that prints "Hello Airflow" and assign it to the operator.

2
Enable Prometheus metrics in Airflow config
Add the following line to your Airflow configuration file airflow.cfg under the [metrics] section: statsd_on = True. Also set statsd_host = localhost and statsd_port = 8125.
Apache Airflow
Hint

Edit airflow.cfg file manually or via your editor. Add the lines exactly under the [metrics] section.

3
Add Prometheus metrics exporter to Airflow
In your Airflow DAG file, import PrometheusMetrics from airflow_prometheus_exporter. Then add PrometheusMetrics().start_http_server(port=9090) at the top level to start the metrics server.
Apache Airflow
Hint

Import PrometheusMetrics and call start_http_server with port 9090 before defining the DAG.

4
Verify Prometheus metrics output
Run a command to fetch metrics from http://localhost:9090/metrics and print the response. Use Python's requests library to get the metrics and print the text.
Apache Airflow
Hint

Use requests.get to fetch metrics and print the first 200 characters to confirm output.

Practice

(1/5)
1. What is the main purpose of integrating Prometheus with Airflow metrics?
easy
A. To schedule Airflow DAGs automatically
B. To store Airflow logs permanently
C. To monitor Airflow task and system health in real-time
D. To send emails from Airflow tasks

Solution

  1. Step 1: Understand Airflow metrics role

    Airflow metrics provide data about task execution and system performance.
  2. Step 2: Identify Prometheus integration purpose

    Prometheus collects and visualizes these metrics for monitoring and alerting.
  3. Final Answer:

    To monitor Airflow task and system health in real-time -> Option C
  4. Quick Check:

    Monitoring = B [OK]
Hint: Prometheus is for monitoring, not scheduling or logging [OK]
Common Mistakes:
  • Confusing monitoring with scheduling
  • Thinking Prometheus stores logs
  • Assuming Prometheus sends emails
2. Which configuration option must be enabled in Airflow to expose metrics for Prometheus scraping?
easy
A. metrics_enabled = False
B. statsd_on = True
C. prometheus_exporter = False
D. logging_level = DEBUG

Solution

  1. Step 1: Identify Airflow metric exposure setting

    Airflow uses StatsD to collect metrics, so enabling StatsD is required.
  2. Step 2: Match correct config option

    The correct option to enable StatsD is statsd_on = True.
  3. Final Answer:

    statsd_on = True -> Option B
  4. Quick Check:

    Enable StatsD = A [OK]
Hint: Enable StatsD to expose metrics for Prometheus [OK]
Common Mistakes:
  • Disabling metrics with false flags
  • Confusing logging level with metrics
  • Assuming Prometheus exporter is off by default
3. Given this Airflow config snippet:
[metrics]
statsd_on = True
statsd_host = "localhost"
statsd_port = 8125

What will Prometheus scrape from Airflow?
medium
A. Airflow task execution metrics via StatsD exporter
B. Airflow DAG definitions
C. Airflow database contents
D. Airflow log files

Solution

  1. Step 1: Analyze config snippet

    The snippet enables StatsD metrics collection on localhost port 8125.
  2. Step 2: Understand Prometheus scraping

    Prometheus scrapes metrics exposed by StatsD exporter, which includes task execution data.
  3. Final Answer:

    Airflow task execution metrics via StatsD exporter -> Option A
  4. Quick Check:

    StatsD metrics = D [OK]
Hint: StatsD config means Prometheus scrapes metrics, not logs or DB [OK]
Common Mistakes:
  • Thinking Prometheus scrapes DAG code
  • Assuming Prometheus reads database directly
  • Confusing logs with metrics
4. You enabled StatsD in Airflow but Prometheus shows no metrics. What is a likely cause?
medium
A. Prometheus scrape config missing or incorrect
B. Airflow DAGs are paused
C. Airflow scheduler is disabled
D. Airflow logs are too large

Solution

  1. Step 1: Check Prometheus scraping setup

    If Prometheus shows no metrics, it may not be scraping the StatsD exporter endpoint correctly.
  2. Step 2: Rule out unrelated causes

    Paused DAGs or scheduler off do not stop metrics exposure; large logs do not affect metrics scraping.
  3. Final Answer:

    Prometheus scrape config missing or incorrect -> Option A
  4. Quick Check:

    Scrape config issue = C [OK]
Hint: Check Prometheus scrape config if no metrics appear [OK]
Common Mistakes:
  • Blaming paused DAGs for missing metrics
  • Assuming scheduler off stops metrics
  • Thinking logs size affects metrics
5. You want to create an alert in Prometheus for Airflow tasks that fail more than 5 times in 10 minutes. Which Prometheus query is correct?
hard
A. count(airflow_task_failures_total) == 5
B. sum(airflow_task_success_total) > 5
C. rate(airflow_task_failures_total[5m]) < 5
D. increase(airflow_task_failures_total[10m]) > 5

Solution

  1. Step 1: Understand alert condition

    The alert triggers if failures increase by more than 5 in 10 minutes.
  2. Step 2: Match Prometheus query

    increase(metric[10m]) > 5 checks if failures increased over 5 in last 10 minutes.
  3. Step 3: Eliminate wrong options

    sum(airflow_task_success_total) > 5 checks successes, not failures; C uses wrong time and comparison; D counts current samples, not increase.
  4. Final Answer:

    increase(airflow_task_failures_total[10m]) > 5 -> Option D
  5. Quick Check:

    Use increase() over 10m > 5 for failure alert [OK]
Hint: Use increase() over time window to detect failure spikes [OK]
Common Mistakes:
  • Using success metric instead of failure
  • Using rate() incorrectly for count threshold
  • Counting samples instead of increase
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard