forked from rtoma/logspout-redis-logstash
/
redis.go
233 lines (202 loc) · 6.14 KB
/
redis.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Based on:
// - https://github.com/looplab/logspout-logstash/blob/master/logstash.go
// - https://github.com/gettyimages/logspout-kafka/blob/master/kafka.go
// - https://github.com/gliderlabs/logspout/pull/41/files
// - https://github.com/fsouza/go-dockerclient/blob/master/container.go#L222
package redis
import (
"fmt"
"strings"
"log"
"os"
"time"
"encoding/json"
"github.com/gliderlabs/logspout/router"
"github.com/garyburd/redigo/redis"
)
type RedisAdapter struct {
route *router.Route
pool *redis.Pool
key string
docker_host string
use_v0 bool
}
type DockerFields struct {
Name string `json:"name"`
CID string `json:"cid"`
Image string `json:"image"`
ImageTag string `json:"image_tag,omitempty"`
Source string `json:"source"`
DockerHost string `json:"docker_host,omitempty"`
}
type LogstashFields struct {
Docker DockerFields `json:"docker"`
}
type LogstashMessageV0 struct {
Timestamp string `json:"@timestamp"`
Sourcehost string `json:"@source_host"`
Message string `json:"@message"`
Fields LogstashFields `json:"@fields"`
}
type LogstashMessageV1 struct {
Timestamp string `json:"@timestamp"`
Sourcehost string `json:"host"`
Message string `json:"message"`
Fields DockerFields `json:"docker"`
}
func init() {
router.AdapterFactories.Register(NewRedisAdapter, "redis")
}
func NewRedisAdapter(route *router.Route) (router.LogAdapter, error) {
address := getopt("REDIS_HOST", "")
if address == "" {
address := route.Address
if !strings.Contains(address, ":") {
address = address + ":6379"
}
} else {
port := getopt("REDIS_PORT", "6379")
address = address + ":" + port
}
route.Address = address
key := route.Options["key"]
if key == "" {
key = getopt("REDIS_KEY", "logspout")
}
password := route.Options["password"]
if password == "" {
password = getopt("REDIS_PASSWORD", "")
}
docker_host := getopt("REDIS_DOCKER_HOST", "")
use_v0 := route.Options["use_v0_layout"] != ""
if !use_v0 {
use_v0 = getopt("REDIS_USE_V0_LAYOUT", "") != ""
}
if os.Getenv("DEBUG") != "" {
log.Printf("Using Redis server '%s', password: %t, pushkey: '%s', v0 layout: %t\n",
address, password != "", key, use_v0)
}
pool := newRedisConnectionPool(address, password)
// lets test the water
conn := pool.Get()
defer conn.Close()
res, err := conn.Do("PING")
if err != nil {
return nil, errorf("Cannot connect to Redis server %s: %v", address, err)
}
if os.Getenv("DEBUG") != "" {
log.Printf("Redis connect successful, got response: %s\n", res)
}
return &RedisAdapter{
route: route,
pool: pool,
key: key,
docker_host: docker_host,
use_v0: use_v0,
}, nil
}
func (a *RedisAdapter) Stream(logstream chan *router.Message) {
conn := a.pool.Get()
defer conn.Close()
mute := false
for m := range logstream {
msg := createLogstashMessage(m, a.docker_host, a.use_v0)
js, err := json.Marshal(msg)
if err != nil {
if !mute {
log.Println("redis: error on json.Marshal (muting until restored):", err)
mute = true
}
continue
}
_, err = conn.Do("RPUSH", a.key, js)
if err != nil {
if !mute {
log.Println("redis: error on rpush (muting until restored):", err)
mute = true
}
continue
}
mute = false
}
}
func errorf(format string, a ...interface{}) (err error) {
err = fmt.Errorf(format, a...)
if os.Getenv("DEBUG") != "" {
fmt.Println(err.Error())
}
return
}
func getopt(name, dfault string) string {
value := os.Getenv(name)
if value == "" {
value = dfault
}
return value
}
func newRedisConnectionPool(server, password string) *redis.Pool {
return &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func () (redis.Conn, error) {
c, err := redis.Dial("tcp", server)
if err != nil {
return nil, err
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
func splitImage(image string) (string, string) {
n := strings.Index(image, ":")
if n > -1 {
return image[0:n], image[n+1:]
}
return image, ""
}
func createLogstashMessage(m *router.Message, docker_host string, use_v0 bool) interface{} {
image_name, image_tag := splitImage(m.Container.Config.Image)
cid := m.Container.ID[0:12]
name := m.Container.Name[1:]
timestamp := m.Time.Format(time.RFC3339Nano)
if use_v0 {
return LogstashMessageV0{
Message: m.Data,
Timestamp: timestamp,
Sourcehost: m.Container.Config.Hostname,
Fields: LogstashFields{
Docker: DockerFields{
CID: cid,
Name: name,
Image: image_name,
ImageTag: image_tag,
Source: m.Source,
DockerHost: docker_host,
},
},
}
}
return LogstashMessageV1{
Message: m.Data,
Timestamp: timestamp,
Sourcehost: m.Container.Config.Hostname,
Fields: DockerFields{
CID: cid,
Name: name,
Image: image_name,
ImageTag: image_tag,
Source: m.Source,
DockerHost: docker_host,
},
}
}