forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
45 lines (36 loc) · 989 Bytes
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package format
import (
"errors"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/fmtstr"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
)
type Encoder struct {
Format *fmtstr.EventFormatString
}
type Config struct {
String *fmtstr.EventFormatString `config:"string" validate:"required"`
}
func init() {
outputs.RegisterOutputCodec("format", func(cfg *common.Config) (outputs.Codec, error) {
config := Config{}
if cfg == nil {
return nil, errors.New("empty format codec configuration")
}
if err := cfg.Unpack(&config); err != nil {
return nil, err
}
return New(config.String), nil
})
}
func New(fmt *fmtstr.EventFormatString) *Encoder {
return &Encoder{fmt}
}
func (w *Encoder) Encode(event common.MapStr) ([]byte, error) {
serializedEvent, err := w.Format.RunBytes(event)
if err != nil {
logp.Err("Fail to format event (%v): %#v", err, event)
}
return serializedEvent, err
}