-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_logger_elk.go
61 lines (51 loc) · 1.29 KB
/
request_logger_elk.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
package main
import (
"errors"
"fmt"
"log"
"time"
"github.com/google/uuid"
"github.com/spf13/viper"
"gopkg.in/olivere/elastic.v3"
)
type RequestLoggerELKPayload struct {
RequestLogEntry
Time time.Time `json:"@timestamp"`
UUID string `json:"uuid"`
}
type RequestLoggerELK struct {
client *elastic.Client
serverAddress string
index string
uuid string
}
func NewRequestLoggerELK() (*RequestLoggerELK, error) {
address := viper.GetString("logger.elk.address")
if len(address) < 1 {
return nil, errors.New("Invalid ELK address provided")
}
index := viper.GetString("logger.elk.index")
if len(index) < 1 {
return nil, errors.New("Invalid ELK index provided")
}
client, err := elastic.NewSimpleClient(elastic.SetURL(address))
if err != nil {
return nil, fmt.Errorf("Failed to create ELK client: %s", err)
}
return &RequestLoggerELK{
client: client,
index: index,
uuid: uuid.New().String(),
}, nil
}
func (l *RequestLoggerELK) Log(entry RequestLogEntry) {
row := RequestLoggerELKPayload{
Time: time.Now(),
UUID: l.uuid,
RequestLogEntry: entry,
}
_, err := l.client.Index().Index(l.index).Type("cyborgpayload").BodyJson(row).Refresh(true).Do()
if err != nil {
log.Printf("Error inserting ELK entry: %s", err)
}
}