-
Notifications
You must be signed in to change notification settings - Fork 14
scm: truncate long command output in logs (Bug 2036740) #1123
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
Open
cgsheeh
wants to merge
4
commits into
mozilla-conduit:main
Choose a base branch
from
cgsheeh:hg-logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| LOG_OUTPUT_HEAD_LIMIT = 500 | ||
| LOG_OUTPUT_TAIL_LIMIT = 200 | ||
|
|
||
|
|
||
| def truncate_output(output: str) -> str: | ||
| """Trim long command output to its head and tail. | ||
|
|
||
| Keeps log volume bounded for commands like `hg export` or `git format-patch` | ||
| that emit an entire patch, while preserving the most diagnostically useful | ||
| portions (commit metadata at the start, summary or error trailer at the end). | ||
| """ | ||
| total = len(output) | ||
| if total <= LOG_OUTPUT_HEAD_LIMIT + LOG_OUTPUT_TAIL_LIMIT: | ||
| return output | ||
|
|
||
| head = output[:LOG_OUTPUT_HEAD_LIMIT] | ||
| tail = output[-LOG_OUTPUT_TAIL_LIMIT:] | ||
| omitted = total - LOG_OUTPUT_HEAD_LIMIT - LOG_OUTPUT_TAIL_LIMIT | ||
| return f"{head}\n...[{omitted} bytes omitted]...\n{tail}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import pytest | ||
|
|
||
| from lando.utils.strings import ( | ||
| LOG_OUTPUT_HEAD_LIMIT, | ||
| LOG_OUTPUT_TAIL_LIMIT, | ||
| truncate_output, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "case_name,input_string", | ||
| [ | ||
| ("empty", ""), | ||
| ("short", "hello world"), | ||
| ( | ||
| "just_under_boundary", | ||
| "x" * (LOG_OUTPUT_HEAD_LIMIT + LOG_OUTPUT_TAIL_LIMIT - 1), | ||
| ), | ||
| ("at_boundary", "x" * (LOG_OUTPUT_HEAD_LIMIT + LOG_OUTPUT_TAIL_LIMIT)), | ||
| ], | ||
| ) | ||
| def test_truncate_output_passes_short_input_through(case_name, input_string): | ||
| assert ( | ||
| truncate_output(input_string) == input_string | ||
| ), f"`truncate_output` should leave `{case_name}` input unchanged." | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("middle_size", [1, 1000, 100_000]) | ||
| def test_truncate_output_long_keeps_head_and_tail(middle_size): | ||
| head = "H" * LOG_OUTPUT_HEAD_LIMIT | ||
| middle = "M" * middle_size | ||
| tail = "T" * LOG_OUTPUT_TAIL_LIMIT | ||
| long_output = head + middle + tail | ||
|
|
||
| result = truncate_output(long_output) | ||
|
|
||
| assert result.startswith( | ||
| head | ||
| ), "Truncated output should preserve the first `LOG_OUTPUT_HEAD_LIMIT` characters." | ||
| assert result.endswith( | ||
| tail | ||
| ), "Truncated output should preserve the last `LOG_OUTPUT_TAIL_LIMIT` characters." | ||
| assert ( | ||
| f"[{middle_size} bytes omitted]" in result | ||
| ), "Truncated output should include a marker reporting the omitted byte count." | ||
| assert ( | ||
| "M" not in result | ||
| ), "Truncated output should not contain any of the omitted middle section." |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In your original description you mentioned this issue is with the
hg exportcommand. Do we need this to apply to git commands as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
git diffcontent doesn't need to be emitted in full to the logs, hence why I addedtruncate_log_outputbelow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, for both the git cases, I think we could simply remove
--stdoutor use--outputto prevent the unnecessary output in the first place (which may be a legacy implementation anyway). I wonder if this is something we can also do inhgcommands? I think if we can avoid truncating output it would be best.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a way, if the output somehow ends up being incorrect, it's probably best to have some of it in the logs, for quicker identification, than none at all. Truncating would allow to retain that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said, yes, not using
--stdoutmay be a good idea (likely with--output-directoryand--numbered-filesto get predictable names).