-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
add log file tailing and logrotate support #1264
Conversation
Can one of the admins verify that this patch is reasonable to test? If so, please reply "ok to test". This message may repeat a few times in short succession due to jenkinsci/ghprb-plugin#292. Sorry. Otherwise, if this message is too spammy, please complain to ixdy. |
}, nil | ||
} | ||
|
||
// initializes an OomParser object. Returns and OomParser object and an error. |
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.
s/and/an/
Can we just use inotify? |
glog.Errorf("Open failed on %s", t.filename) | ||
return nil, err | ||
} | ||
t.file.Seek(0, os.SEEK_END) |
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.
This is a change in behavior, as we won't get old events.
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 is true. i figured it was bug before, sending oom events for things that may have happened days ago in the logs.
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.
Things from days ago are probably not relevant, but events from seconds ago might be. Either way, I think it's out of scope for this PR (feel free to leave a TODO)
@timstclair i'm looking into using inotify. might be less of a hassle than fsnotify was. |
448946c
to
c0c74f0
Compare
@timstclair @ncdc updated with inotify instead of polling |
return | ||
} | ||
if reopen { | ||
break |
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.
I'm pretty sure this will break you out of the outer loop. I'd recommend adding some unit tests :)
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.
it breaks me out of the for loop starting line 90 which is what i want. i've done tests so i know it works. i'll have to think about how to unit test it.
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.
My bad, had it backwards
c0c74f0
to
e8eb9ca
Compare
@timstclair new update that i think captures all the previous comments. PTAL when you can. |
t.watcher, err = inotify.NewWatcher() | ||
if err != nil { | ||
glog.Errorf("Inotify init failed on %s: %v", t.filename, err) | ||
return |
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.
nit: avoid naked returns
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.
how do i avoid these?
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.
Just change this line to return t, err
or return nil, err
. Also change the return type to (*Tail, error)
since the variable names don't add anything in that case.
IMHO there are only 2 reasons to give return types names:
- If the return type is something like
(int, int, int, int)
, names distinguish the different ints (though I would usually prefer a struct for this) - If you need to modify a return value in a
defer
statement (again, this should be avoided if possible)
Which parts are you having trouble with? The race condition can be avoided by protecting the reader with a mutex, and you can punt on the unit test for now. |
FYI, we're hoping to cut a v0.23.2 release tomorrow (2016-05-18) for kubernetes 1.3, and I'd like to get this change in. Please ping me if there's anything I can help with to move this along. |
I'll fix it up asap. Update within the hour.
|
Great, thanks! |
@timstclair I'm close to having this done. Another half hour. |
e8eb9ca
to
a058132
Compare
@timstclair i've pushed an update. i reworked watchFile, but now the mutex handling is nasty in there. recommendations welcome. |
a058132
to
559dce2
Compare
559dce2
to
8d80aac
Compare
|
||
type Tail struct { | ||
reader *bufio.Reader | ||
sync.Mutex // protects reader |
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.
Call this readerLock
(if it's embedded it's less clear what it's protecting)
8d80aac
to
4790ea2
Compare
@timstclair ok, i incorporated your comments and introduced a readerState so that the locking can be cleaner. the real purpose is to avoid the race between a reader and the log file opening. |
glog.V(4).Infof("Log file %s moved/deleted", t.filename) | ||
t.readerLock.Lock() | ||
defer t.readerLock.Unlock() | ||
t.readerState = readerStateOpening |
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.
Did you mean to set reader to nil here as well? I don't think setting the state does anything otherwise.
Thanks, this looks much better. Just a couple small things, then LGTM. |
Taking this as-is, will follow up with a couple fixes. Thanks! |
Cleanup tail util from #1264
Fixes #1248
Add some "tail -F" support (via polling and reopening) so that cadvisor can continue to track kernel log messages across log rotations.
Looked at doing this with fsnotify but 1) fsnotify depends on golang.org/x/sys/unix and that is about 10k LOC and 2) I couldn't find a tail implementation that actually worked with fsnotify.