forked from tsaikd/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
outputelastic.go
65 lines (54 loc) · 1.31 KB
/
outputelastic.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 outputelastic
import (
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/logevent"
"gopkg.in/olivere/elastic.v3"
)
const (
ModuleName = "elastic"
)
type OutputConfig struct {
config.OutputConfig
URL string `json:"url"`
Index string `json:"index"`
DocumentType string `json:"document_type"`
DocumentID string `json:"document_id"`
Sniff bool `json:"sniff"` // find all nodes of your cluster, https://github.com/olivere/elastic/wiki/Sniffing
client *elastic.Client
}
func DefaultOutputConfig() OutputConfig {
return OutputConfig{
OutputConfig: config.OutputConfig{
CommonConfig: config.CommonConfig{
Type: ModuleName,
},
},
}
}
func InitHandler(confraw *config.ConfigRaw) (retconf config.TypeOutputConfig, err error) {
conf := DefaultOutputConfig()
if err = config.ReflectConfig(confraw, &conf); err != nil {
return
}
conf.client, err = elastic.NewClient(
elastic.SetURL(conf.URL),
elastic.SetSniff(conf.Sniff),
)
if err != nil {
return
}
retconf = &conf
return
}
func (t *OutputConfig) Event(event logevent.LogEvent) (err error) {
index := event.Format(t.Index)
doctype := event.Format(t.DocumentType)
id := event.Format(t.DocumentID)
_, err = t.client.Index().
Index(index).
Type(doctype).
Id(id).
BodyJson(event).
Do()
return
}