-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Account for line endings in logging #430
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,6 +116,68 @@ int set_k8s_timestamp(char *buf, ssize_t buflen, const char *stream_type) | |
| return 0; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| * splits buf into lines and inserts timestamps at the beginning of | ||
| * the line written to logfd. | ||
| */ | ||
| int write_with_timestamps(int logfd, const char *buf, ssize_t buflen) | ||
| { | ||
| #define TSBUFLEN 34 | ||
| char tsbuf[TSBUFLEN]; | ||
| static bool last_buf_ended_with_newline = TRUE; | ||
|
|
||
| g_auto(GStrv) lines = g_strsplit(buf, "\n", -1); | ||
| ssize_t num_lines = g_strv_length(lines); | ||
| for (ssize_t i = 0; i < num_lines; i++) | ||
| { | ||
| const char *line = lines[i]; | ||
|
|
||
| ninfo("Processing line: %ld, %s", i, line); | ||
|
|
||
| /* Skip last line if it is empty */ | ||
| if (i == (num_lines - 1) && buf[buflen-1] == '\n' && !strcmp("", line)) { | ||
| ninfo("Skipping last line"); | ||
| break; | ||
| } | ||
|
|
||
| /* | ||
| * Only add timestamps for first line if last buffer's last | ||
| * line ended with newline. Add it for all other lines. | ||
| */ | ||
| if (i != 0 || (i == 0 && last_buf_ended_with_newline)) { | ||
| ninfo("Adding timestamp"); | ||
| int rc = set_k8s_timestamp(tsbuf, TSBUFLEN, "stdout"); | ||
| if (rc < 0) { | ||
| nwarn("failed to set timestamp"); | ||
| } else { | ||
| /* Exclude the \0 while writing */ | ||
| if (write(logfd, tsbuf, TSBUFLEN - 1) != (TSBUFLEN - 1)) { | ||
| nwarn("partial/failed write ts (logFd)"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /* Log output to logfd. */ | ||
| ssize_t len = strlen(line); | ||
| if (write(logfd, line, len) != len) { | ||
| nwarn("partial/failed write (logFd)"); | ||
| return -1; | ||
| } | ||
| /* Write the line ending */ | ||
| if ((i < num_lines - 1) || (i == (num_lines - 1) && buf[buflen - 1] == '\n')) { | ||
| if (write(logfd, "\n", 1) != 1) { | ||
| nwarn("failed to write line ending"); | ||
| return -1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| last_buf_ended_with_newline = buf[buflen-1] == '\n'; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int main(int argc, char *argv[]) | ||
| { | ||
| int ret, runtime_status; | ||
|
|
@@ -395,9 +457,6 @@ int main(int argc, char *argv[]) | |
| pexit("Failed to add console master fd to epoll"); | ||
| } | ||
|
|
||
| #define TSBUFLEN 34 | ||
| char tsbuf[TSBUFLEN]; | ||
|
|
||
| /* | ||
| * Log all of the container's output and pipe STDIN into it. Currently | ||
| * nothing using the STDIN setup (which makes its inclusion here a bit | ||
|
|
@@ -427,28 +486,12 @@ int main(int argc, char *argv[]) | |
| if (num_read <= 0) | ||
| goto out; | ||
|
|
||
| buf[num_read] = '\0'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is splitting every line into and it's breaking tests (removing this reads the whole line) |
||
| ninfo("read a chunk: (fd=%d) '%s'", mfd, buf); | ||
|
|
||
|
|
||
| /* Prepend the CRI-mandated timestamp and other metadata. */ | ||
| /* | ||
| * FIXME: Currently this code makes the assumption that | ||
| * @buf doesn't contain any newlines (since the CRI | ||
| * requires each line to contain a timestamp). This | ||
| * is an /okay/ assumption in most cases because | ||
| * ptys generally have newline-buffered output. | ||
| */ | ||
| int rc = set_k8s_timestamp(tsbuf, TSBUFLEN, "stdout"); | ||
| /* Insert CRI mandated timestamps in the buffer for each line */ | ||
| int rc = write_with_timestamps(logfd, buf, num_read); | ||
| if (rc < 0) { | ||
| nwarn("failed to set timestamp"); | ||
| } else { | ||
| if (write(logfd, tsbuf, TSBUFLEN) != TSBUFLEN) { | ||
| nwarn("partial/failed write ts (logFd)"); | ||
| } | ||
| } | ||
| /* Log all output to logfd. */ | ||
| if (write(logfd, buf, num_read) != num_read) { | ||
| nwarn("partial/failed write (logFd)"); | ||
| goto out; | ||
| } | ||
| } | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Removing the
\0added at line 486 from my comment above, I went testing this again:snprintfinset_k8s_timestampis adding a\0char at the end of the buf. When the log is read by k8s the hex dump is as follow:notice the
00at the beginning. Which makes this test fail with:even if strings appear to be the same.
The thing should be that, when we write the timestamp line with the log, we should
snprintfwith the whole log line and remove the NULL byte at the end beforewriteing, this patch does that but it's ugly I know :):This patch makes the test above pass. The patch is ugly but the thing is we should not play with null-terminated strings when printing to
logfd, or when read back we have issues.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.
Again, the patch is ugly and I'm by any mean a good C code writer so if you guys can come up with a better solution please discard my whole comment :)
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.
@runcom
ninfo("fuck %ld", TSBUFLEN+len);sort of captures my opinion about having to do this in C 😉. I think we could make this code "nicer" if we wrote partial lines to the log (so we just append thetimestampafter the\nand don't do any of this code that stores the "remaining" part of the line). The downside is that a user might be able to see the partially-written lines. I don't think it should happen that often in practice, but you never know.If you want, I'll open up a PR with my implementation of this so you can compare it with this.
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.
We are taking care of partial lines in the PR. The logic will always have edge cases. For e.g. if we append after \n instead of inserting at beginning we still need to add one at the beginning of the log buffer. In my approach I am tracking if previous buffer ended with \n or not and adding a timestamp accordingly. One thing that is missing and needs to be handled is when there are no \n in a buf.
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.
Ah right, I misread the code. In any case, #436 is my implementation.