|
| 1 | +def extract_all_exceptions(context): |
| 2 | + exception = context.get("exception") |
| 3 | + exception_type = type(exception).__name__ if exception else "Unknown" |
| 4 | + exception_message = ( |
| 5 | + str(exception) if exception else "No exception message available" |
| 6 | + ) |
| 7 | + |
| 8 | + # Extract all exceptions from logs if available |
| 9 | + all_exceptions = [] |
| 10 | + |
| 11 | + # DEBUG: Set this to a string containing log text to test exception parsing |
| 12 | + # When None, normal operation resumes |
| 13 | + DEBUG_LOG_TEXT = None |
| 14 | + |
| 15 | + # First, get exceptions from Docker container logs if available |
| 16 | + if DEBUG_LOG_TEXT is not None: |
| 17 | + # DEBUG MODE: Use the debug log text instead of actual logs |
| 18 | + print(f"DEBUG MODE: Using debug log text for parsing") |
| 19 | + parsed_exceptions = extract_all_exceptions_from_log(DEBUG_LOG_TEXT) |
| 20 | + |
| 21 | + # Add parsed exceptions (filter out None entries) |
| 22 | + for exc in parsed_exceptions: |
| 23 | + if exc[0] is not None: |
| 24 | + all_exceptions.append(exc) |
| 25 | + elif exception and hasattr(exception, "logs") and exception.logs: |
| 26 | + logs = exception.logs |
| 27 | + parsed_exceptions = extract_all_exceptions_from_log("\n".join(logs)) |
| 28 | + |
| 29 | + # Add parsed exceptions (filter out None entries) |
| 30 | + for exc in parsed_exceptions: |
| 31 | + if exc[0] is not None: |
| 32 | + all_exceptions.append(exc) |
| 33 | + |
| 34 | + # Always add the Airflow-level exception as well if not already found |
| 35 | + airflow_exception = (exception_type, exception_message, "Airflow") |
| 36 | + # Check if we already have this exception from parsing |
| 37 | + airflow_already_found = any( |
| 38 | + exc[0] == exception_type and exc[1] == exception_message |
| 39 | + for exc in all_exceptions |
| 40 | + ) |
| 41 | + if not airflow_already_found: |
| 42 | + all_exceptions.append(airflow_exception) |
| 43 | + |
| 44 | + return all_exceptions |
| 45 | + |
| 46 | + |
| 47 | +def extract_exception_from_log(log_text): |
| 48 | + import re |
| 49 | + |
| 50 | + # Find the last occurrence of 'Traceback (most recent call last):' |
| 51 | + traceback_start = log_text.rfind("Traceback (most recent call last):") |
| 52 | + if traceback_start == -1: |
| 53 | + return None, None # No traceback found |
| 54 | + |
| 55 | + # Extract the traceback portion |
| 56 | + traceback_text = log_text[traceback_start:] |
| 57 | + |
| 58 | + # Find the last line (which usually contains the exception type and message) |
| 59 | + last_line = traceback_text.strip().split("\n")[-1] |
| 60 | + |
| 61 | + # Extract exception type and message |
| 62 | + match = re.match(r"([\w.]+): (.*)", last_line) |
| 63 | + if match: |
| 64 | + return match.group(1), match.group(2) |
| 65 | + |
| 66 | + return None, None |
| 67 | + |
| 68 | + |
| 69 | +def extract_all_exceptions_from_log(log_text): |
| 70 | + """ |
| 71 | + Extract all Python exceptions from raw Airflow logs. |
| 72 | + Returns list of tuples (exception_type, exception_message, source). |
| 73 | + Source is "ETL" for exceptions before "Task failed with exception" line, |
| 74 | + "Airflow" for exceptions after that line. |
| 75 | + """ |
| 76 | + |
| 77 | + exceptions = [] |
| 78 | + lines = log_text.split("\n") |
| 79 | + |
| 80 | + # Find the "Task failed with exception" dividing line |
| 81 | + task_failed_line_idx = None |
| 82 | + for i, line in enumerate(lines): |
| 83 | + if "Task failed with exception" in line: |
| 84 | + task_failed_line_idx = i |
| 85 | + break |
| 86 | + |
| 87 | + # Look for all traceback sections |
| 88 | + traceback_starts = [] |
| 89 | + for i, line in enumerate(lines): |
| 90 | + if "Traceback (most recent call last):" in line: |
| 91 | + traceback_starts.append(i) |
| 92 | + |
| 93 | + # Process each traceback section |
| 94 | + for start_idx in traceback_starts: |
| 95 | + # Determine source based on position relative to "Task failed with exception" |
| 96 | + if task_failed_line_idx is None: |
| 97 | + source = "ETL" # Default to ETL if no dividing line found |
| 98 | + elif start_idx < task_failed_line_idx: |
| 99 | + source = "ETL" |
| 100 | + else: |
| 101 | + source = "Airflow" |
| 102 | + |
| 103 | + # Find the exception line for this traceback |
| 104 | + exception_line = _find_exception_line_in_traceback(lines, start_idx) |
| 105 | + |
| 106 | + if exception_line: |
| 107 | + exc_type, exc_msg = _parse_exception_line(exception_line) |
| 108 | + if exc_type: |
| 109 | + exceptions.append((exc_type, exc_msg, source)) |
| 110 | + |
| 111 | + return exceptions if exceptions else [(None, None, "Unknown")] |
| 112 | + |
| 113 | + |
| 114 | +def _find_exception_line_in_traceback(lines, traceback_start_idx): |
| 115 | + """ |
| 116 | + Find the actual exception line (final line) in a traceback section. |
| 117 | + """ |
| 118 | + # Start from the traceback line and look forward |
| 119 | + i = traceback_start_idx + 1 |
| 120 | + |
| 121 | + while i < len(lines): |
| 122 | + line = lines[i].strip() |
| 123 | + |
| 124 | + # Stop if we hit another traceback |
| 125 | + if "Traceback (most recent call last):" in line: |
| 126 | + break |
| 127 | + |
| 128 | + # Stop if we hit certain log markers that indicate end of traceback |
| 129 | + if line.startswith("During handling of the above exception") or line.startswith( |
| 130 | + "The above exception was the direct cause" |
| 131 | + ): |
| 132 | + i += 1 |
| 133 | + continue |
| 134 | + |
| 135 | + # Check if this is an exception line |
| 136 | + if _is_exception_line(line): |
| 137 | + return line |
| 138 | + |
| 139 | + i += 1 |
| 140 | + |
| 141 | + return None |
| 142 | + |
| 143 | + |
| 144 | +def _is_exception_line(line): |
| 145 | + """Check if a line contains a Python exception""" |
| 146 | + import re |
| 147 | + |
| 148 | + line = line.strip() |
| 149 | + |
| 150 | + # Skip obvious non-exception lines |
| 151 | + if ( |
| 152 | + not line |
| 153 | + or line.startswith("File ") |
| 154 | + or line.startswith(" ") |
| 155 | + or line.startswith("^") |
| 156 | + or "Traceback" in line |
| 157 | + or line.startswith("During handling") |
| 158 | + or line.startswith("The above exception") |
| 159 | + ): |
| 160 | + return False |
| 161 | + |
| 162 | + # Look for Python exception patterns |
| 163 | + # Must start with a capital letter followed by word characters, dots, underscores |
| 164 | + # Common exception endings but not required |
| 165 | + exception_pattern = ( |
| 166 | + r"^[A-Z][A-Za-z0-9_.]*(?:Error|Exception|Warning|Timeout)?(?::\s|$)" |
| 167 | + ) |
| 168 | + return re.match(exception_pattern, line) is not None |
| 169 | + |
| 170 | + |
| 171 | +def _parse_exception_line(line): |
| 172 | + """Parse an exception line into (type, message)""" |
| 173 | + import re |
| 174 | + |
| 175 | + line = line.strip() |
| 176 | + |
| 177 | + # Pattern for ExceptionType: message |
| 178 | + match = re.match(r"^([A-Z][A-Za-z0-9_.]*)\s*:\s*(.*)", line) |
| 179 | + if match: |
| 180 | + return match.group(1), match.group(2) |
| 181 | + |
| 182 | + # Pattern for just ExceptionType (no colon/message) |
| 183 | + match = re.match(r"^([A-Z][A-Za-z0-9_.]*)$", line) |
| 184 | + if match: |
| 185 | + return match.group(1), "" |
| 186 | + |
| 187 | + return None, None |
0 commit comments