forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
41 lines (32 loc) · 882 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
package format
import (
"errors"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/fmtstr"
"github.com/elastic/beats/libbeat/outputs/codec"
)
type Encoder struct {
Format *fmtstr.EventFormatString
}
type Config struct {
String *fmtstr.EventFormatString `config:"string" validate:"required"`
}
func init() {
codec.RegisterType("format", func(_ beat.Info, cfg *common.Config) (codec.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 (e *Encoder) Encode(_ string, event *beat.Event) ([]byte, error) {
return e.Format.RunBytes(event)
}