Log inspection and troubleshooting in Apache Airflow - Time & Space Complexity
When we inspect logs in Airflow, we want to know how the time to find issues grows as logs get bigger.
We ask: How does the effort to search logs change when there are more log entries?
Analyze the time complexity of the following Airflow log inspection code.
from airflow.models import TaskInstance
def inspect_logs(task_instances):
errors = []
for ti in task_instances:
log = ti.log.read()
if 'ERROR' in log:
errors.append(ti.task_id)
return errors
This code checks each task instance's log for the word "ERROR" and collects task IDs with errors.
Look at what repeats in the code.
- Primary operation: Looping over all task instances and reading their logs.
- How many times: Once for each task instance in the input list.
As the number of task instances grows, the time to check logs grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 log reads and checks |
| 100 | 100 log reads and checks |
| 1000 | 1000 log reads and checks |
Pattern observation: The work grows directly with the number of task instances.
Time Complexity: O(n * m)
This means the time to inspect logs grows linearly with the number of task instances and the size of each log.
[X] Wrong: "Checking logs for errors is instant no matter how many tasks there are."
[OK] Correct: Each task's log must be read and checked, so more tasks mean more work and more time.
Understanding how log inspection time grows helps you design better monitoring and troubleshooting tools in real projects.
"What if we cached logs after the first read? How would that change the time complexity?"
Practice
Solution
Step 1: Understand what logs represent in Airflow
Logs record the detailed events and outputs of each task run, showing what happened inside the task.Step 2: Identify the purpose of inspecting logs
Inspecting logs helps to see the step-by-step execution and find errors or confirm success.Final Answer:
To understand what happened during task execution step-by-step -> Option CQuick Check:
Logs show task execution details = A [OK]
- Thinking logs change task code
- Confusing logs with scheduling
- Believing logs delete DAGs
process_data in DAG daily_report for date 2024-06-01?Solution
Step 1: Recall the correct airflow CLI syntax for logs
The correct command isairflow tasks logs <dag_id> <task_id> <execution_date>.Step 2: Match the command with given task and date
airflow tasks logs daily_report process_data 2024-06-01 matches the syntax exactly with correct order and spelling.Final Answer:
airflow tasks logs daily_report process_data 2024-06-01 -> Option AQuick Check:
Correct airflow logs command = A [OK]
- Swapping 'tasks' and 'logs' words
- Using 'show' or 'task' incorrectly
- Wrong command order
airflow tasks logs sales_report generate_summary 2024-06-02 and see an error about missing file. What does this log output indicate?Solution
Step 1: Analyze the log error message about missing file
An error about a missing file means the task tried to access a file that does not exist or is not reachable.Step 2: Understand the implication for task status
This error causes the task to fail because it cannot complete its work without the file.Final Answer:
The task failed because it could not find a required file -> Option DQuick Check:
Missing file error means task failure = C [OK]
- Assuming task succeeded despite error
- Confusing DAG run status with task logs
- Thinking scheduler status is shown in task logs
airflow tasks logs my_dag my_task 2024-06-05 but get a 'No logs found' message. What is the most likely cause?Solution
Step 1: Understand what 'No logs found' means in Airflow
This message usually means no logs exist because the task has not executed for the given date.Step 2: Check other options for plausibility
The command syntax is correct, Airflow installed is unrelated, and paused DAGs still keep logs from past runs.Final Answer:
The task has not run yet for that date -> Option BQuick Check:
No logs means no task run yet = D [OK]
- Assuming command syntax error
- Thinking Airflow is missing
- Confusing paused DAG with no logs
data_cleanup in DAG weekly_maintenance fails intermittently. Which approach best helps you find the root cause using logs?Solution
Step 1: Understand the goal of troubleshooting intermittent failures
Intermittent failures require comparing multiple runs to see when and why failures happen.Step 2: Identify the best log inspection method
Checking logs from several recent runs helps spot patterns or recurring errors causing failure.Final Answer:
Check logs for multiple recent runs to find patterns of failure -> Option AQuick Check:
Compare multiple logs to find failure patterns = B [OK]
- Deleting logs loses important info
- Restarting scheduler ignores root cause
- Changing schedule doesn't fix errors
Ready to prove it?
Pass the test and this pattern moves unknown
