Skip to content

Commit

Permalink
Implement WriteEvent on file output so that it can be used by 'inputs…
Browse files Browse the repository at this point in the history
…' components
  • Loading branch information
netixx committed Nov 2, 2023
1 parent bd19bd9 commit f2481c5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
55 changes: 54 additions & 1 deletion outputs/file/file_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,60 @@ func (f *File) Write(ctx context.Context, rsp proto.Message, meta outputs.Meta)
}
}

func (f *File) WriteEvent(ctx context.Context, ev *formatters.EventMsg) {}
func (f *File) WriteEvent(ctx context.Context, ev *formatters.EventMsg) {
select {
case <-ctx.Done():
return
default:
}
var evs = []*formatters.EventMsg{ev}
for _, proc := range f.evps {
evs = proc.Apply(evs...)
}
toWrite := []byte{}
if f.Cfg.SplitEvents {
for _, pev := range evs {
var err error
var b []byte
if f.Cfg.Multiline {
b, err = json.MarshalIndent(pev, "", f.Cfg.Indent)
} else {
b, err = json.Marshal(pev)
}
if err != nil {
fmt.Printf("failed to WriteEvent: %v", err)
numberOfFailWriteMsgs.WithLabelValues(f.file.Name(), "marshal_error").Inc()
return
}
toWrite = append(toWrite, b...)
toWrite = append(toWrite, []byte(f.Cfg.Separator)...)
}
} else {
var err error
var b []byte
if f.Cfg.Multiline {
b, err = json.MarshalIndent(evs, "", f.Cfg.Indent)
} else {
b, err = json.Marshal(evs)
}
if err != nil {
fmt.Printf("failed to WriteEvent: %v", err)
numberOfFailWriteMsgs.WithLabelValues(f.file.Name(), "marshal_error").Inc()
return
}
toWrite = append(toWrite, b...)
toWrite = append(toWrite, []byte(f.Cfg.Separator)...)
}

n, err := f.file.Write(toWrite)
if err != nil {
fmt.Printf("failed to WriteEvent: %v", err)
numberOfFailWriteMsgs.WithLabelValues(f.file.Name(), "write_error").Inc()
return
}
numberOfWrittenBytes.WithLabelValues(f.file.Name()).Add(float64(n))
numberOfWrittenMsgs.WithLabelValues(f.file.Name()).Inc()
}

// Close //
func (f *File) Close() error {
Expand Down
8 changes: 4 additions & 4 deletions outputs/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ var (
template.New("target-template").
Funcs(TemplateFuncs).
Parse(defaultTargetTemplateString))
)

var TemplateFuncs = template.FuncMap{
"host": utils.GetHost,
}
TemplateFuncs = template.FuncMap{
"host": utils.GetHost,
}
)

const (
defaultTargetTemplateString = `
Expand Down

0 comments on commit f2481c5

Please sign in to comment.