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

Wait for file log to drain before k6 exits #2414

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (c *rootCommand) setupLoggers() (<-chan struct{}, error) {
c.logger.SetOutput(ioutil.Discard)

case strings.HasPrefix(line, "loki"):
ch = make(chan struct{})
ch = make(chan struct{}) // TODO: refactor, get it from the constructor
hook, err := log.LokiFromConfigLine(c.ctx, c.fallbackLogger, line, ch)
if err != nil {
return nil, err
Expand All @@ -300,7 +300,8 @@ func (c *rootCommand) setupLoggers() (<-chan struct{}, error) {
c.logFmt = "raw"

case strings.HasPrefix(line, "file"):
hook, err := log.FileHookFromConfigLine(c.ctx, c.fallbackLogger, line)
ch = make(chan struct{}) // TODO: refactor, get it from the constructor
hook, err := log.FileHookFromConfigLine(c.ctx, c.fallbackLogger, line, ch)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion log/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ type fileHook struct {
w io.WriteCloser
bw *bufio.Writer
levels []logrus.Level
done chan struct{}
}

// FileHookFromConfigLine returns new fileHook hook.
func FileHookFromConfigLine(
ctx context.Context, fallbackLogger logrus.FieldLogger, line string,
ctx context.Context, fallbackLogger logrus.FieldLogger, line string, done chan struct{},
Copy link
Contributor

@olegbespalov olegbespalov Mar 7, 2022

Choose a reason for hiding this comment

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

This done channel looks more like the internal state. Do you think we should accept it as input?

Can a signature be like?

FileHookFromConfigLine(ctx context.Context, fallbackLogger logrus.FieldLogger, line string) (logrus.Hook, chan struct{}, error)

Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally, even with the doneCh <-chan sruct{}, but I guess it is outside of the scope of this PR

Copy link
Member Author

Choose a reason for hiding this comment

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

hmm yeah, that is slightly better 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

actually, I think I'll refactor both this and LokiFromConfigLine in a separate PR, so I'll leave it as it is for now

) (logrus.Hook, error) {
// TODO: fix this so it works correctly with relative paths from the CWD
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you mean by this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Noticed that we don't explicitly check if we got a relative path here and instead just open exactly what we were given:

k6/log/file.go

Line 108 in 1e28a3e

file, err := os.OpenFile(h.path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o600)

This works, for now, since we're directly using the os package and it will properly handle relative paths based on the current working directory. However, after we switch to afero.Fs here, we will need to explicitly account for it.

Copy link
Contributor

Choose a reason for hiding this comment

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

can you expand this comment there with that info :)

Copy link
Member Author

Choose a reason for hiding this comment

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

No need, I plan to fix it with a small commit in #2412 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

FWIW, this is the fix and the test to verify it: 7574bd2


hook := &fileHook{
fallbackLogger: fallbackLogger,
levels: logrus.AllLevels,
done: done,
}

parts := strings.SplitN(line, "=", 2)
Expand Down Expand Up @@ -120,6 +124,7 @@ func (h *fileHook) loop(ctx context.Context) chan []byte {
loglines := make(chan []byte, fileHookBufferSize)

go func() {
defer close(h.done)
for {
select {
case entry := <-loglines:
Expand Down
5 changes: 4 additions & 1 deletion log/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ func TestFileHookFromConfigLine(t *testing.T) {
t.Run(test.line, func(t *testing.T) {
t.Parallel()

res, err := FileHookFromConfigLine(context.Background(), logrus.New(), test.line)
res, err := FileHookFromConfigLine(
context.Background(), logrus.New(), test.line, make(chan struct{}),
)

if test.err {
require.Error(t, err)
Expand Down Expand Up @@ -147,6 +149,7 @@ func TestFileHookFire(t *testing.T) {
w: nc,
bw: bufio.NewWriter(nc),
levels: logrus.AllLevels,
done: make(chan struct{}),
}

ctx, cancel := context.WithCancel(context.Background())
Expand Down