Skip to content

oomparser - new constructor to support streaming of new/current OOM events only. #3716

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion utils/oomparser/oomexample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ import (
// demonstrates how to run oomparser.OomParser to get OomInstance information
func main() {
klog.InitFlags(nil)
ignoreOldOOMs := flag.Bool("ignore_old", false, "Read only new OOM events, ignoring old. ")
flag.Parse()
// out is a user-provided channel from which the user can read incoming
// OomInstance objects
outStream := make(chan *oomparser.OomInstance)
oomLog, err := oomparser.New()

var oomLog *oomparser.OomParser
var err error
if *ignoreOldOOMs {
oomLog, err = oomparser.NewFromNow()
} else {
oomLog, err = oomparser.New()
}
if err != nil {
klog.Infof("Couldn't make a new oomparser. %v", err)
} else {
Expand Down
15 changes: 15 additions & 0 deletions utils/oomparser/oomparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ func (p *OomParser) StreamOoms(outStream chan<- *OomInstance) {
klog.Errorf("exiting analyzeLines. OOM events will not be reported.")
}

// NewFromNow initializes an OomParser object that returns current OOM events only. In this mode the OomParser object
// will not send OOM events that occurred before the OomParser object was constructed.
func NewFromNow() (*OomParser, error) {
parser, err := New()
if err != nil {
return nil, err
}

// seek to and set the offset at the end of /dev/kmsg to avoid reporting old OOM events
if err := parser.parser.SeekEnd(); err != nil {
return nil, err
}
return parser, nil
}

// initializes an OomParser object. Returns an OomParser object and an error.
func New() (*OomParser, error) {
parser, err := kmsgparser.NewParser()
Expand Down
Loading