-
Notifications
You must be signed in to change notification settings - Fork 1
/
elasticsearch.go
65 lines (58 loc) · 1.56 KB
/
elasticsearch.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
package bigbro
import (
"context"
"encoding/json"
"github.com/olivere/elastic"
"time"
)
// LogstashEvent is an event that is recognised by Elasticsearch.
type LogstashEvent struct {
Message string `json:"message"`
Version string `json:"@version"`
Timestamp time.Time `json:"@timestamp"`
Type string `json:"type"`
Host string `json:"host"`
Event Event `json:"event"`
}
// ElasticsearchFormatter is a log formatter that can output to Elasticsearch.
type ElasticsearchFormatter struct {
index string
version string
client *elastic.Client
}
func (f ElasticsearchFormatter) Format(e Event) string {
b, _ := json.Marshal(f.transformEvent(e))
return string(b)
}
func (f ElasticsearchFormatter) Write(e Event) error {
ctx := context.Background()
_, err := f.client.Index().
Index(f.index).
Type("event").
BodyJson(f.transformEvent(e)).
Do(ctx)
return err
}
// transformEvent returns an Elasticsearch compatible version of an Event.
func (f ElasticsearchFormatter) transformEvent(e Event) LogstashEvent {
return LogstashEvent{
Message: e.Name,
Version: f.version,
Timestamp: e.Time,
Type: e.Method,
Host: e.Location,
Event: e,
}
}
// NewElasticsearchFormatter creates a new formatter for Elasticsearch.
func NewElasticsearchFormatter(index, version, url string) (ElasticsearchFormatter, error) {
c, err := elastic.NewSimpleClient(elastic.SetURL(url))
if err != nil {
return ElasticsearchFormatter{}, err
}
return ElasticsearchFormatter{
index: index,
version: version,
client: c,
}, nil
}