Skip to content

Commit

Permalink
Fix resource processor porter install logs truncation (#4048)
Browse files Browse the repository at this point in the history
  • Loading branch information
marrobi authored Nov 6, 2024
1 parent 216003e commit 9140431
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
FEATURES:

ENHANCEMENTS:
* Split log entries with [Log chunk X of Y] for better readability. ([[#3992](https://github.com/microsoft/AzureTRE/issues/3992)
* Expose APP_SERVICE_SKU build variable to allow enablement of App Gateway WAF ([#4111](https://github.com/microsoft/AzureTRE/pull/4111))
* Update Terraform to use Azure AD authentication rather than storage account keys ([#4103](https://github.com/microsoft/AzureTRE/issues/4103))

Expand Down
2 changes: 1 addition & 1 deletion resource_processor/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.9.2"
__version__ = "0.9.3"
14 changes: 13 additions & 1 deletion resource_processor/shared/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ def initialize_logging() -> logging.Logger:
return logger


def chunk_log_output(output: str, chunk_size: int = 30000):
"""
Split the log output into smaller chunks and prefix each chunk with [Log chunk X of Y].
"""
total_chunks = (len(output) + chunk_size - 1) // chunk_size # Calculate total number of chunks
for i in range(0, len(output), chunk_size):
current_chunk = i // chunk_size + 1
prefix = f"[Log chunk {current_chunk} of {total_chunks}] "
yield prefix + output[i:i + chunk_size]


def shell_output_logger(console_output: str, prefix_item: str, logging_level: int):
if not console_output:
logger.debug("shell console output is empty.")
Expand All @@ -118,4 +129,5 @@ def shell_output_logger(console_output: str, prefix_item: str, logging_level: in
console_output = console_output.strip()

if console_output:
logger.log(logging_level, f"{prefix_item} {console_output}")
for chunk in chunk_log_output(console_output):
logger.log(logging_level, f"{prefix_item} {chunk}")
19 changes: 18 additions & 1 deletion resource_processor/tests_rp/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from mock import patch
import logging
from shared.logging import shell_output_logger
from shared.logging import shell_output_logger, chunk_log_output


@patch("shared.logging.logger")
Expand Down Expand Up @@ -29,3 +29,20 @@ def test_shell_output_logger_normal_case(mock_logger):
console_output = "Some logs"
shell_output_logger(console_output, "prefix", logging.DEBUG)
mock_logger.log.assert_called_with(logging.DEBUG, "prefix Some logs")


@patch("shared.logging.logger")
def test_shell_output_logger_chunked_logging(mock_logger):
console_output = "A" * 60000 # 60,000 characters long
shell_output_logger(console_output, "prefix", logging.DEBUG)
assert mock_logger.log.call_count == 2
mock_logger.log.assert_any_call(logging.DEBUG, f"prefix [Log chunk 1 of 2] {'A' * 30000}")
mock_logger.log.assert_any_call(logging.DEBUG, f"prefix [Log chunk 2 of 2] {'A' * 30000}")


def test_chunk_log_output():
output = "A" * 60000 # 60,000 characters long
chunks = list(chunk_log_output(output, chunk_size=30000))
assert len(chunks) == 2
assert chunks[0] == "[Log chunk 1 of 2] " + "A" * 30000
assert chunks[1] == "[Log chunk 2 of 2] " + "A" * 30000

0 comments on commit 9140431

Please sign in to comment.