Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LogCollector should skip and log warning for files that don't exist #3098

Merged
merged 6 commits into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions azurelinuxagent/ga/logcollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,23 +304,27 @@ def _get_final_list_for_archive(self, priority_file_queue):
final_files_to_collect = []

while priority_file_queue:
file_path = heappop(priority_file_queue)[1] # (priority, file_path)
file_size = min(os.path.getsize(file_path), _FILE_SIZE_LIMIT)

if total_uncompressed_size + file_size > _UNCOMPRESSED_ARCHIVE_SIZE_LIMIT:
_LOGGER.warning("Archive too big, done with adding files.")
break

if os.path.getsize(file_path) <= _FILE_SIZE_LIMIT:
final_files_to_collect.append(file_path)
_LOGGER.info("Adding file %s, size %s b", file_path, file_size)
else:
truncated_file_path = self._truncate_large_file(file_path)
if truncated_file_path:
_LOGGER.info("Adding truncated file %s, size %s b", truncated_file_path, file_size)
final_files_to_collect.append(truncated_file_path)

total_uncompressed_size += file_size
try:
file_path = heappop(priority_file_queue)[1] # (priority, file_path)
file_size = min(os.path.getsize(file_path), _FILE_SIZE_LIMIT)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.path.getsize can rise ioerror if file not found


if total_uncompressed_size + file_size > _UNCOMPRESSED_ARCHIVE_SIZE_LIMIT:
_LOGGER.warning("Archive too big, done with adding files.")
break

if os.path.getsize(file_path) <= _FILE_SIZE_LIMIT:
final_files_to_collect.append(file_path)
_LOGGER.info("Adding file %s, size %s b", file_path, file_size)
else:
truncated_file_path = self._truncate_large_file(file_path)
if truncated_file_path:
_LOGGER.info("Adding truncated file %s, size %s b", truncated_file_path, file_size)
final_files_to_collect.append(truncated_file_path)

total_uncompressed_size += file_size
except IOError as e:
if e.errno == 2: # [Errno 2] No such file or directory
_LOGGER.warning("File %s does not exist, skipping collection for this file", file_path)

_LOGGER.info("Uncompressed archive size is %s b", total_uncompressed_size)

Expand Down Expand Up @@ -357,21 +361,32 @@ def collect_logs_and_get_archive(self):

compressed_archive = None

def handle_add_file_to_archive_error(error_count, max_errors, file_to_collect, exception):
error_count += 1
if error_count >= max_errors:
raise Exception("Too many errors, giving up. Last error: {0}".format(ustr(exception)))
else:
_LOGGER.warning("Failed to add file %s to the archive: %s", file_to_collect, ustr(exception))
maddieford marked this conversation as resolved.
Show resolved Hide resolved
return error_count

try:
compressed_archive = zipfile.ZipFile(COMPRESSED_ARCHIVE_PATH, "w", compression=zipfile.ZIP_DEFLATED)

max_errors = 8
error_count = 0

for file_to_collect in files_to_collect:
try:
archive_file_name = LogCollector._convert_file_name_to_archive_name(file_to_collect)
compressed_archive.write(file_to_collect.encode("utf-8"), arcname=archive_file_name)
except Exception as e:
error_count += 1
if error_count >= max_errors:
raise Exception("Too many errors, giving up. Last error: {0}".format(ustr(e)))
except IOError as e:
if e.errno == 2: # [Errno 2] No such file or directory
_LOGGER.warning("File %s does not exist, skipping collection for this file",
file_to_collect)
else:
_LOGGER.warning("Failed to add file %s to the archive: %s", file_to_collect, ustr(e))
error_count = handle_add_file_to_archive_error(error_count, max_errors, file_to_collect, e)
except Exception as e:
error_count = handle_add_file_to_archive_error(error_count, max_errors, file_to_collect, e)

compressed_archive_size = os.path.getsize(COMPRESSED_ARCHIVE_PATH)
_LOGGER.info("Successfully compressed files. Compressed archive size is %s b", compressed_archive_size)
Expand Down
Loading