/
redis_output.go
58 lines (50 loc) · 1.13 KB
/
redis_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
package heka_redis
import (
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/mozilla-services/heka/pipeline"
)
type RedisOutputConfig struct {
Address string `toml:"address"`
Key string `toml:"key"`
}
type RedisOutput struct {
conf *RedisOutputConfig
conn redis.Conn
}
func (rop *RedisOutput) ConfigStruct() interface{} {
return &RedisOutputConfig{
Address: "127.0.0.1:6379",
Key: "heka",
}
}
func (rop *RedisOutput) Init(config interface{}) error {
rop.conf = config.(*RedisOutputConfig)
var err error
rop.conn, err = redis.Dial("tcp", rop.conf.Address)
if err != nil {
return fmt.Errorf("connecting to - %s", err.Error())
}
return nil
}
func (rop *RedisOutput) Run(or pipeline.OutputRunner, h pipeline.PluginHelper) error {
inChan := or.InChan()
for pack := range inChan {
payload := pack.Message.GetPayload()
_,err := rop.conn.Do("LPUSH", rop.conf.Key, payload)
if err != nil {
or.LogError(err)
continue
}
pack.Recycle()
}
return nil
}
func (rop *RedisOutput) Stop() {
rop.conn.Close()
}
func init() {
pipeline.RegisterPlugin("RedisOutput", func() interface{} {
return new(RedisOutput)
})
}