Skip to content

Commit

Permalink
reportportal: add logfile size upload limit
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaarreell committed Sep 12, 2024
1 parent 7b16db6 commit ec94f05
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions tmt/steps/report/reportportal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from tmt._compat.typing import TypeAlias

JSON: 'TypeAlias' = Any
DEFAULT_LOG_SIZE_LIMIT: int = 1024 * 1024


def _flag_env_to_default(option: str, default: bool) -> bool:
Expand All @@ -41,13 +42,38 @@ def _str_env_to_default(option: str, default: Optional[str]) -> Optional[str]:
return str(os.getenv(env_var))


def _filter_invalid_chars(data: str) -> str:
def _filter_invalid_chars(data: str, step_data: 'ReportReportPortalData') -> str:
return re.sub(
'[^\u0020-\uD7FF\u0009\u000A\u000D\uE000-\uFFFD\U00010000-\U0010FFFF]+',
'',
data)


def _filter_log_per_size(data: str, step_data: 'ReportReportPortalData') -> str:
size = len(data)
header = (f"WARNING: Uploaded log has been truncated because its size {size} bytes "
f"exceeds tmt reportportal plugin limit of {step_data.log_size_limit} bytes."
"The limit is controlled with --log-size-limit plugin option or"
"TMT_PLUGIN_REPORT_REPORTPORTAL_LOG_SIZE_LIMIT environment variable.\n\n")
if size > step_data.log_size_limit:
return (f"WARNING: Log file was truncated because its size {size} bytes "
f"exceeds {step_data.log_size_limit} bytes.\n\n"
f"{data[:step_data.log_size_limit]}")
return data


_LOG_FILTERS = [
_filter_log_per_size,
_filter_invalid_chars,
]


def _filter_log(log: str, step_data: 'ReportReportPortalData') -> str:
for log_filter in _LOG_FILTERS:
log = log_filter(log, step_data)
return log


@dataclasses.dataclass
class ReportReportPortalData(tmt.steps.report.ReportStepData):

Expand Down Expand Up @@ -142,6 +168,17 @@ class ReportReportPortalData(tmt.steps.report.ReportStepData):
(e.g. 'Idle'). 'To Investigate' is used by default.
""")

log_size_limit: int = field(
option="--log-size-limit",
metavar="LOG_SIZE_LIMIT",
default=int(
_str_env_to_default('log_size_limit', str(DEFAULT_LOG_SIZE_LIMIT))),
help=f"""
Size limit in bytes for log upload to ReportPortal.
The default limit is {DEFAULT_LOG_SIZE_LIMIT} bytes
({DEFAULT_LOG_SIZE_LIMIT / 1024 / 1024} MB).
""")

exclude_variables: str = field(
option="--exclude-variables",
metavar="PATTERN",
Expand Down Expand Up @@ -590,15 +627,15 @@ def go(self, *, logger: Optional[tmt.log.Logger] = None) -> None:
response = self.rp_api_post(
session=session,
path="log/entry",
json={"message": _filter_invalid_chars(log),
json={"message": _filter_log(log, self.data),
"itemUuid": item_uuid,
"launchUuid": launch_uuid,
"level": level,
"time": result.end_time})

# Write out failures
if index == 0 and status == "FAILED":
message = _filter_invalid_chars(result.failures(log))
message = _filter_log(result.failures(log), self.data)
response = self.rp_api_post(
session=session,
path="log/entry",
Expand Down

0 comments on commit ec94f05

Please sign in to comment.