|
| 1 | +import json |
| 2 | +import re |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | +from typing import TypedDict |
| 6 | +from zipfile import ZipFile |
| 7 | + |
| 8 | +from workflow_rerun.constants import LOGGER |
| 9 | + |
| 10 | + |
| 11 | +class LogFile(TypedDict): |
| 12 | + file_name: str |
| 13 | + path: Path |
| 14 | + |
| 15 | + |
| 16 | +class ErrorData(TypedDict): |
| 17 | + error_text: str |
| 18 | + ticket: int |
| 19 | + |
| 20 | + |
| 21 | +class LogAnalyzer: |
| 22 | + def __init__(self, |
| 23 | + path_to_log_archive: Path, |
| 24 | + path_to_errors_file: Path) -> None: |
| 25 | + self._path_to_log_archive = path_to_log_archive |
| 26 | + self._path_to_errors_file = path_to_errors_file |
| 27 | + |
| 28 | + self._errors_to_look_for: list[ErrorData] = [] |
| 29 | + self._collect_errors_to_look_for() |
| 30 | + |
| 31 | + self._log_dir = tempfile.TemporaryDirectory().name |
| 32 | + |
| 33 | + self._log_files: list[LogFile] = [] |
| 34 | + self._collect_log_files() |
| 35 | + |
| 36 | + all_txt_log_files_pretty = '\n'.join(map(lambda item: str(item['path']), self._log_files)) |
| 37 | + LOGGER.info(f'ALL .txt LOG FILES: \n{all_txt_log_files_pretty}') |
| 38 | + |
| 39 | + self.found_matching_error = False |
| 40 | + |
| 41 | + def _collect_errors_to_look_for(self) -> None: |
| 42 | + with open(file=self._path_to_errors_file, |
| 43 | + mode='r', |
| 44 | + encoding='utf-8') as errors_file: |
| 45 | + errors_data = json.load(errors_file) |
| 46 | + for error_data in errors_data: |
| 47 | + self._errors_to_look_for.append( |
| 48 | + ErrorData(error_text=error_data['error_text'], |
| 49 | + ticket=error_data['ticket']) |
| 50 | + ) |
| 51 | + |
| 52 | + def _collect_log_files(self) -> None: |
| 53 | + """ |
| 54 | + Collects the .txt log files from the log archive |
| 55 | +
|
| 56 | + The GitHub Actions pipeline logs archive should have the following structure: |
| 57 | + > Job_name_0 |
| 58 | + > step_name_0.txt |
| 59 | + > step_name_1.txt |
| 60 | + ... |
| 61 | + > Job_name_1 |
| 62 | + > step_name_0.txt |
| 63 | + > step_name_1.txt |
| 64 | + ... |
| 65 | + > Job_name_2 |
| 66 | + ... |
| 67 | + ... |
| 68 | + |
| 69 | + We need to only analyze the `*.txt` files |
| 70 | + """ |
| 71 | + |
| 72 | + with ZipFile(file=self._path_to_log_archive, |
| 73 | + mode='r') as zip_file: |
| 74 | + zip_file.extractall(self._log_dir) |
| 75 | + |
| 76 | + for _file in Path(self._log_dir).iterdir(): |
| 77 | + if _file.is_dir(): |
| 78 | + for log_file in _file.iterdir(): |
| 79 | + self._log_files.append(LogFile(file_name=log_file.name, |
| 80 | + path=log_file.resolve())) |
| 81 | + |
| 82 | + def _is_error_in_log(self, |
| 83 | + error_to_look_for: str, |
| 84 | + log_file_path: Path) -> bool: |
| 85 | + """ |
| 86 | + Searches for the error in the provided log |
| 87 | + """ |
| 88 | + |
| 89 | + error_to_look_for = self._clean_up_string(error_to_look_for) |
| 90 | + |
| 91 | + with open(file=log_file_path, |
| 92 | + mode='r', |
| 93 | + encoding='utf-8') as log_file: |
| 94 | + for line in log_file: |
| 95 | + if error_to_look_for in self._clean_up_string(line): |
| 96 | + return True |
| 97 | + return False |
| 98 | + |
| 99 | + @staticmethod |
| 100 | + def _clean_up_string(string: str) -> str: |
| 101 | + """ |
| 102 | + Replaces special characters with spaces in the string, strips it from leading and following spaces, |
| 103 | + and lowers it |
| 104 | + |
| 105 | + for "Could not resolve host: github.com" returns "could not resolve host github com" |
| 106 | + |
| 107 | + This cleanup is applied to both errors to look for and logs themselves for matching |
| 108 | + """ |
| 109 | + return re.sub(r'[^A-Za-z0-9]+', ' ', string).lower().strip() |
| 110 | + |
| 111 | + def analyze(self) -> None: |
| 112 | + """ |
| 113 | + Iterates over the known errors and tries to find them in the collected log files |
| 114 | + """ |
| 115 | + for error in self._errors_to_look_for: |
| 116 | + |
| 117 | + LOGGER.info(f'LOOKING FOR "{error["error_text"]}" ERROR...') |
| 118 | + |
| 119 | + for log_file in self._log_files: |
| 120 | + if self._is_error_in_log(error_to_look_for=error['error_text'], |
| 121 | + log_file_path=log_file['path']): |
| 122 | + LOGGER.info(f'FOUND "{error["error_text"]}" ERROR IN {log_file["path"]}. TICKET: {error["ticket"]}') |
| 123 | + self.found_matching_error = True |
| 124 | + return |
| 125 | + |
| 126 | +if __name__ == '__main__': |
| 127 | + # Usage example |
| 128 | + log_analyzer = LogAnalyzer(path_to_log_archive=Path('/tmp/logs/log.zip'), |
| 129 | + path_to_errors_file=Path('/tmp/errors_to_look_for.json')) |
| 130 | + log_analyzer.analyze() |
| 131 | + if log_analyzer.found_matching_error: |
| 132 | + print('found matching error, see logs above') |
0 commit comments