forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.go
207 lines (172 loc) · 3.8 KB
/
enc.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package elasticsearch
import (
"bytes"
"compress/gzip"
"io"
"net/http"
"time"
"github.com/urso/go-structform/gotype"
"github.com/urso/go-structform/json"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/outputs/codec"
)
type bodyEncoder interface {
bulkBodyEncoder
Reader() io.Reader
Marshal(doc interface{}) error
}
type bulkBodyEncoder interface {
bulkWriter
AddHeader(*http.Header)
Reset()
}
type bulkWriter interface {
Add(meta, obj interface{}) error
AddRaw(raw interface{}) error
}
type jsonEncoder struct {
buf *bytes.Buffer
folder *gotype.Iterator
}
type gzipEncoder struct {
buf *bytes.Buffer
gzip *gzip.Writer
folder *gotype.Iterator
}
type event struct {
Timestamp time.Time `struct:"@timestamp"`
Fields common.MapStr `struct:",inline"`
}
func newJSONEncoder(buf *bytes.Buffer) *jsonEncoder {
if buf == nil {
buf = bytes.NewBuffer(nil)
}
e := &jsonEncoder{buf: buf}
e.resetState()
return e
}
func (b *jsonEncoder) Reset() {
b.buf.Reset()
}
func (b *jsonEncoder) resetState() {
var err error
visitor := json.NewVisitor(b.buf)
b.folder, err = gotype.NewIterator(visitor,
gotype.Folders(
codec.MakeTimestampEncoder(),
codec.MakeBCTimestampEncoder()))
if err != nil {
panic(err)
}
}
func (b *jsonEncoder) AddHeader(header *http.Header) {
header.Add("Content-Type", "application/json; charset=UTF-8")
}
func (b *jsonEncoder) Reader() io.Reader {
return b.buf
}
func (b *jsonEncoder) Marshal(obj interface{}) error {
b.Reset()
return b.AddRaw(obj)
}
func (b *jsonEncoder) AddRaw(obj interface{}) error {
var err error
switch v := obj.(type) {
case beat.Event:
err = b.folder.Fold(event{Timestamp: v.Timestamp, Fields: v.Fields})
case *beat.Event:
err = b.folder.Fold(event{Timestamp: v.Timestamp, Fields: v.Fields})
default:
err = b.folder.Fold(obj)
}
if err != nil {
b.resetState()
}
b.buf.WriteByte('\n')
return err
}
func (b *jsonEncoder) Add(meta, obj interface{}) error {
pos := b.buf.Len()
if err := b.AddRaw(meta); err != nil {
b.buf.Truncate(pos)
return err
}
if err := b.AddRaw(obj); err != nil {
b.buf.Truncate(pos)
return err
}
return nil
}
func newGzipEncoder(level int, buf *bytes.Buffer) (*gzipEncoder, error) {
if buf == nil {
buf = bytes.NewBuffer(nil)
}
w, err := gzip.NewWriterLevel(buf, level)
if err != nil {
return nil, err
}
g := &gzipEncoder{buf: buf, gzip: w}
g.resetState()
return g, nil
}
func (g *gzipEncoder) resetState() {
var err error
visitor := json.NewVisitor(g.gzip)
g.folder, err = gotype.NewIterator(visitor,
gotype.Folders(
codec.MakeTimestampEncoder(),
codec.MakeBCTimestampEncoder()))
if err != nil {
panic(err)
}
}
func (b *gzipEncoder) Reset() {
b.buf.Reset()
b.gzip.Reset(b.buf)
}
func (b *gzipEncoder) Reader() io.Reader {
b.gzip.Close()
return b.buf
}
func (b *gzipEncoder) AddHeader(header *http.Header) {
header.Add("Content-Type", "application/json; charset=UTF-8")
header.Add("Content-Encoding", "gzip")
}
func (b *gzipEncoder) Marshal(obj interface{}) error {
b.Reset()
return b.AddRaw(obj)
}
var nl = []byte("\n")
func (b *gzipEncoder) AddRaw(obj interface{}) error {
var err error
switch v := obj.(type) {
case beat.Event:
err = b.folder.Fold(event{Timestamp: v.Timestamp, Fields: v.Fields})
case *beat.Event:
err = b.folder.Fold(event{Timestamp: v.Timestamp, Fields: v.Fields})
default:
err = b.folder.Fold(obj)
}
if err != nil {
b.resetState()
}
_, err = b.gzip.Write(nl)
if err != nil {
b.resetState()
}
return nil
}
func (b *gzipEncoder) Add(meta, obj interface{}) error {
pos := b.buf.Len()
if err := b.AddRaw(meta); err != nil {
b.buf.Truncate(pos)
return err
}
if err := b.AddRaw(obj); err != nil {
b.buf.Truncate(pos)
return err
}
b.gzip.Flush()
return nil
}