-
Notifications
You must be signed in to change notification settings - Fork 74
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
Support ltsv #363
Support ltsv #363
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ package tailer | |
import ( | ||
"context" | ||
"io" | ||
"strings" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"github.com/nxadm/tail" | ||
|
@@ -66,6 +67,11 @@ type PatternTailer struct { | |
gc *grok.CompiledGrok | ||
} | ||
|
||
type LTSVTailer struct { | ||
handle *tail.Tail | ||
ltsvParseString func(line string) map[string]string | ||
} | ||
|
||
func NewTailer(file string) (*Tailer, error) { | ||
t, err := tail.TailFile(file, tailConfig) | ||
if err != nil { | ||
|
@@ -95,6 +101,28 @@ func NewPatternTailer(file string, patterns map[string]string) (*PatternTailer, | |
return &PatternTailer{t, gc}, nil | ||
} | ||
|
||
func NewLTSVTailer(file string) (*LTSVTailer, error) { | ||
ltsvParseString := func(line string) map[string]string { | ||
columns := strings.Split(line, "\t") | ||
lvs := make(map[string]string) | ||
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. Not sure what lvs and lv mean. Could you give the variables more meaningful names? 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. @dhurley Also Fix those name. |
||
for _, column := range columns { | ||
lv := strings.SplitN(column, ":", 2) | ||
if len(lv) < 2 { | ||
continue | ||
} | ||
label, value := strings.TrimSpace(lv[0]), strings.TrimSpace(lv[1]) | ||
lvs[label] = value | ||
} | ||
return lvs | ||
} | ||
t, err := tail.TailFile(file, tailConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return <SVTailer{t, ltsvParseString}, nil | ||
} | ||
|
||
func (t *Tailer) Tail(ctx context.Context, data chan<- string) { | ||
for { | ||
select { | ||
|
@@ -150,3 +178,32 @@ func (t *PatternTailer) Tail(ctx context.Context, data chan<- map[string]string) | |
} | ||
} | ||
} | ||
|
||
func (t *LTSVTailer) Tail(ctx context.Context, data chan<- map[string]string) { | ||
for { | ||
select { | ||
case line := <-t.handle.Lines: | ||
if line == nil { | ||
return | ||
} | ||
if line.Err != nil { | ||
continue | ||
} | ||
|
||
l := t.ltsvParseString(line.Text) | ||
if l != nil { | ||
data <- l | ||
} | ||
case <-ctx.Done(): | ||
ctxErr := ctx.Err() | ||
switch ctxErr { | ||
case context.DeadlineExceeded: | ||
log.Tracef("Tailer cancelled because deadline was exceeded, %v", ctxErr) | ||
case context.Canceled: | ||
log.Tracef("Tailer forcibly cancelled, %v", ctxErr) | ||
} | ||
log.Tracef("Tailer is done") | ||
return | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Can you move this out into its own function?
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.
@dhurley I could move out the parse function outside.