forked from inthecloud247/heka-clever-plugins
/
keen_output.go
59 lines (50 loc) · 1.28 KB
/
keen_output.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
package heka_clever_plugins
import (
"encoding/json"
"github.com/inconshreveable/go-keen"
"github.com/mozilla-services/heka/pipeline"
)
type KeenClient interface {
AddEvent(collection string, event interface{}) error
}
type KeenOutput struct {
client KeenClient
collection string
}
type KeenOutputConfig struct {
ApiKey string `toml:"api_key"`
ProjectToken string `toml:"project_token"`
Collection string `toml:"collection_name"`
}
func (ko *KeenOutput) ConfigStruct() interface{} {
return &KeenOutputConfig{}
}
func (ko *KeenOutput) Init(rawConf interface{}) error {
config := rawConf.(*KeenOutputConfig)
ko.client = &keen.Client{ApiKey: config.ApiKey, ProjectToken: config.ProjectToken}
ko.collection = config.Collection
return nil
}
func (ko *KeenOutput) Run(or pipeline.OutputRunner, h pipeline.PluginHelper) error {
for pack := range or.InChan() {
payload := pack.Message.GetPayload()
pack.Recycle()
event := make(map[string]interface{})
err := json.Unmarshal([]byte(payload), &event)
if err != nil {
or.LogError(err)
continue
}
err = ko.client.AddEvent(ko.collection, event)
if err != nil {
or.LogError(err)
continue
}
}
return nil
}
func init() {
pipeline.RegisterPlugin("KeenOutput", func() interface{} {
return new(KeenOutput)
})
}