forked from fuyufjh/splunk-hec-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
394 lines (323 loc) · 8.95 KB
/
client.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package hec
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"sync"
"time"
"github.com/google/uuid"
)
const (
retryWaitTime = 1 * time.Second
defaultMaxContentLength = 1000000
defaultAcknowledgementTimeout = 90 * time.Second
)
type Client struct {
HEC
// HTTP Client for communication with (optional)
httpClient *http.Client
// Splunk Server URL for API requests (required)
serverURL string
// HEC Token (required)
token string
// Keep-Alive (optional, default: true)
keepAlive bool
// Channel (required for Raw mode)
channel string
// Max retrying times (optional, default: 2)
retries int
// Max content length (optional, default: 1000000)
maxLength int
// List of acknowledgement IDs provided by Splunk
ackIDs []int
// Mutex to allow threadsafe acknowledgement checking
ackMux sync.Mutex
}
func NewClient(serverURL string, token string) HEC {
id := uuid.New()
return &Client{
httpClient: http.DefaultClient,
serverURL: serverURL,
token: token,
keepAlive: true,
channel: id.String(),
retries: 2,
maxLength: defaultMaxContentLength,
}
}
func (hec *Client) SetHTTPClient(client *http.Client) {
hec.httpClient = client
}
func (hec *Client) SetKeepAlive(enable bool) {
hec.keepAlive = enable
}
func (hec *Client) SetChannel(channel string) {
hec.channel = channel
}
func (hec *Client) SetMaxRetry(retries int) {
hec.retries = retries
}
func (hec *Client) SetMaxContentLength(size int) {
hec.maxLength = size
}
func (hec *Client) WriteEventWithContext(ctx context.Context, event *Event) error {
if event.empty() {
return nil // skip empty events
}
endpoint := "/services/collector?channel=" + hec.channel
data, _ := json.Marshal(event)
if len(data) > hec.maxLength {
return ErrEventTooLong
}
return hec.write(ctx, endpoint, data)
}
func (hec *Client) WriteEvent(event *Event) error {
return hec.WriteEventWithContext(context.Background(), event)
}
func (hec *Client) WriteBatchWithContext(ctx context.Context, events []*Event) error {
if len(events) == 0 {
return nil
}
endpoint := "/services/collector?channel=" + hec.channel
var buffer bytes.Buffer
var tooLongs []int
for index, event := range events {
if event.empty() {
continue // skip empty events
}
data, _ := json.Marshal(event)
if len(data) > hec.maxLength {
tooLongs = append(tooLongs, index)
continue
}
// Send out bytes in buffer immediately if the limit exceeded after adding this event
if buffer.Len()+len(data) > hec.maxLength {
if err := hec.write(ctx, endpoint, buffer.Bytes()); err != nil {
return err
}
buffer.Reset()
}
buffer.Write(data)
}
if buffer.Len() > 0 {
if err := hec.write(ctx, endpoint, buffer.Bytes()); err != nil {
return err
}
}
if len(tooLongs) > 0 {
return ErrEventTooLong
}
return nil
}
func (hec *Client) WriteBatch(events []*Event) error {
return hec.WriteBatchWithContext(context.Background(), events)
}
type EventMetadata struct {
Host *string
Index *string
Source *string
SourceType *string
Time *time.Time
}
func (hec *Client) WriteRawWithContext(ctx context.Context, reader io.ReadSeeker, metadata *EventMetadata) error {
endpoint := rawHecEndpoint(hec.channel, metadata)
return breakStream(reader, hec.maxLength, func(chunk []byte) error {
if err := hec.write(ctx, endpoint, chunk); err != nil {
// Ignore NoData error (e.g. "\n\n" will cause NoData error)
if res, ok := err.(*Response); !ok || res.Code != StatusNoData {
return err
}
}
return nil
})
}
func (hec *Client) WriteRaw(reader io.ReadSeeker, metadata *EventMetadata) error {
return hec.WriteRawWithContext(context.Background(), reader, metadata)
}
type acknowledgementRequest struct {
Acks []int `json:"acks"`
}
// WaitForAcknowledgementWithContext blocks until the Splunk indexer has
// acknowledged that all previously submitted data has been successfully
// indexed or if the provided context is cancelled. This requires the HEC token
// configuration in Splunk to have indexer acknowledgement enabled.
func (hec *Client) WaitForAcknowledgementWithContext(ctx context.Context) error {
// Make our own copy of the list of acknowledgement IDs and remove them
// from the client while we check them.
hec.ackMux.Lock()
ackIDs := hec.ackIDs
hec.ackIDs = nil
hec.ackMux.Unlock()
if len(ackIDs) == 0 {
return nil
}
endpoint := "/services/collector/ack?channel=" + hec.channel
for {
ackRequestData, _ := json.Marshal(acknowledgementRequest{Acks: ackIDs})
response, err := hec.makeRequest(ctx, endpoint, ackRequestData)
if err != nil {
// Put the remaining unacknowledged IDs back
hec.ackMux.Lock()
hec.ackIDs = append(hec.ackIDs, ackIDs...)
hec.ackMux.Unlock()
return err
}
for ackIDString, status := range response.Acks {
if status {
ackID, err := strconv.Atoi(ackIDString)
if err != nil {
return fmt.Errorf("could not convert ack ID to int: %v", err)
}
ackIDs = remove(ackIDs, ackID)
}
}
if len(ackIDs) == 0 {
break
}
// If the server did not indicate that all acknowledgements have been
// made, check again after a short delay.
select {
case <-time.After(retryWaitTime):
continue
case <-ctx.Done():
// Put the remaining unacknowledged IDs back
hec.ackMux.Lock()
hec.ackIDs = append(hec.ackIDs, ackIDs...)
hec.ackMux.Unlock()
return ctx.Err()
}
}
return nil
}
// WaitForAcknowledgement blocks until the Splunk indexer has acknowledged
// that all previously submitted data has been successfully indexed or if the
// default acknowledgement timeout is reached. This requires the HEC token
// configuration in Splunk to have indexer acknowledgement enabled.
func (hec *Client) WaitForAcknowledgement() error {
ctx, cancel := context.WithTimeout(context.Background(), defaultAcknowledgementTimeout)
defer cancel()
return hec.WaitForAcknowledgementWithContext(ctx)
}
// breakStream breaks text from reader into chunks, with every chunk less than max.
// Unless a single line is longer than max, it always cut at end of lines ("\n")
func breakStream(reader io.ReadSeeker, max int, callback func(chunk []byte) error) error {
var buf []byte = make([]byte, max+1)
var writeAt int
for {
n, err := reader.Read(buf[writeAt:max])
if n == 0 && err == io.EOF {
break
}
// If last line does not end with LF, add one for it
if err == io.EOF && buf[writeAt+n-1] != '\n' {
n++
buf[writeAt+n-1] = '\n'
}
data := buf[0 : writeAt+n]
// Cut after the last LF character
cut := bytes.LastIndexByte(data, '\n') + 1
if cut == 0 {
// This line is too long, but just let it break here
cut = len(data)
}
if err := callback(buf[:cut]); err != nil {
return err
}
writeAt = copy(buf, data[cut:])
if err != nil && err != io.EOF {
return err
}
}
if writeAt != 0 {
return callback(buf[:writeAt])
}
return nil
}
func responseFrom(body []byte) *Response {
var res Response
json.Unmarshal(body, &res)
return &res
}
func (res *Response) Error() string {
return res.Text
}
func (res *Response) String() string {
b, _ := json.Marshal(res)
return string(b)
}
func (hec *Client) makeRequest(ctx context.Context, endpoint string, data []byte) (*Response, error) {
retries := 0
RETRY:
req, err := http.NewRequest(http.MethodPost, hec.serverURL+endpoint, bytes.NewReader(data))
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
if hec.keepAlive {
req.Header.Set("Connection", "keep-alive")
}
req.Header.Set("Authorization", "Splunk "+hec.token)
res, err := hec.httpClient.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, err
}
response := responseFrom(body)
if res.StatusCode != http.StatusOK {
if retriable(response.Code) && retries < hec.retries {
retries++
time.Sleep(retryWaitTime)
goto RETRY
}
}
return response, nil
}
func (hec *Client) write(ctx context.Context, endpoint string, data []byte) error {
response, err := hec.makeRequest(ctx, endpoint, data)
if err != nil {
return err
}
// TODO: find out the correct code
if response.Text != "Success" {
return response
}
// Check for acknowledgement IDs and store them if provided
if response.AckID != nil {
hec.ackMux.Lock()
defer hec.ackMux.Unlock()
hec.ackIDs = append(hec.ackIDs, *response.AckID)
}
return nil
}
func rawHecEndpoint(channel string, metadata *EventMetadata) string {
var buffer bytes.Buffer
buffer.WriteString("/services/collector/raw?channel=" + channel)
if metadata == nil {
return buffer.String()
}
if metadata.Host != nil {
buffer.WriteString("&host=" + *metadata.Host)
}
if metadata.Index != nil {
buffer.WriteString("&index=" + *metadata.Index)
}
if metadata.Source != nil {
buffer.WriteString("&source=" + *metadata.Source)
}
if metadata.SourceType != nil {
buffer.WriteString("&sourcetype=" + *metadata.SourceType)
}
if metadata.Time != nil {
buffer.WriteString("&time=" + epochTime(metadata.Time))
}
return buffer.String()
}