Skip to content

Instantly share code, notes, and snippets.

@karampok
Created January 21, 2020 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karampok/c6e31279ac0cd378ba3660d5ba4f0d62 to your computer and use it in GitHub Desktop.
Save karampok/c6e31279ac0cd378ba3660d5ba4f0d62 to your computer and use it in GitHub Desktop.
Tailer
func (w *Watcher) Initialize() error {
lines := make(chan (string))
//go get lines from file
go func(file string) {
defer close(lines)
//NOTE. To implement tail properly is challenging
// here is a link with nice explanation
// https://github.com/fstab/grok_exporter/wiki/tailer-(tail-%E2%80%90f)
// In all cases, if custom-made tail implementation is needed
// it should be implemented in another package and this code
// should remain simple to read
tl, err := tail.TailFile(file, tail.Config{Follow: true})
if err != nil {
panic(err)
}
for line := range tl.Lines {
lines <- line.Text
}
}(w.logfile)
//go read lines and build state
go func() {
for l := range lines {
parts := strings.Split(l, " ")
if len(parts) < 4 { //TODO. improve validation
log.Printf("error parsing line [%v]", l)
continue
}
ts, err := time.Parse("2006/01/02 15:04:05", parts[0]+" "+parts[1])
if err != nil {
log.Printf("error parsing date %v:", err)
}
id := strings.Trim(parts[2], "[]")
if id == "" { //TODO. validPID(id)
log.Printf("error parsing line")
continue
}
restMsg := strings.Join(parts[3:], " ")
v, ok := w.Connections[id]
if !ok {
v = &connection{}
w.Connections[id] = v
}
v.Timestamps = append(v.Timestamps, ts)
v.Msgs = append(v.Msgs, restMsg)
v.Update(restMsg)
}
}()
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment