/
logspoutkinesis.go
271 lines (235 loc) · 8.1 KB
/
logspoutkinesis.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Based on:
// - https://github.com/rtoma/logspout-redis-logstash/blob/master/redis.go
package logspoutkinesis
import (
"fmt"
"strings"
"strconv"
"log"
"os"
"time"
"encoding/json"
"github.com/gliderlabs/logspout/router"
kinesis "github.com/sendgridlabs/go-kinesis"
batchproducer "github.com/sendgridlabs/go-kinesis/batchproducer"
)
type KinesisAdapter struct {
route *router.Route
batch_client *kinesis.Kinesis
batch_producer batchproducer.Producer
streamName 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"`
Labels map[string]string `json:"labels,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() {
// Register this adapter
router.AdapterFactories.Register(NewLogspoutAdapter, "kinesis")
}
func NewLogspoutAdapter(route *router.Route) (router.LogAdapter, error) {
// kinesis client
batch_client := getKinesis(route)
// The kinesis stream where the logs should be sent to
streamName := route.Address
fmt.Printf("# KINESIS Adapter - Using stream: %s\n", streamName)
// Batch config
batchproducer_config := getKinesisConfig(route)
fmt.Printf("# KINESIS Adapter - Batch config: %v\n", streamName)
// Create a batchproducer
batch_producer, err := batchproducer.New(batch_client, streamName, batchproducer_config)
if err != nil {
fmt.Printf("Unable to retrieve create batchproducer: %v", err)
os.Exit(1)
}
// Host of the docker instance
docker_host := getopt("LK_DOCKER_HOST", "unknown-docker-host")
// Whether to use the v0 logtstash layout or v1
use_v0 := route.Options["use_v0_layout"] != ""
if !use_v0 {
use_v0 = getopt("LK_USE_V0_LAYOUT", "") != ""
}
// Return the kinesis adapter that will receive all the logs
return &KinesisAdapter{
route: route,
batch_producer: batch_producer,
streamName: streamName,
docker_host: docker_host,
use_v0: use_v0,
}, nil
}
func getKinesis(route *router.Route) *kinesis.Kinesis {
// set env variables AWS_ACCESS_KEY and AWS_SECRET_KEY AWS_REGION_NAME
auth, err := kinesis.NewAuthFromEnv()
if err != nil {
fmt.Printf("Unable to retrieve authentication credentials from the environment: %v", err)
os.Exit(1)
}
// AWS region
aws_region := kinesis.NewRegionFromEnv()
fmt.Printf("# KINESIS Adapter - Using region: %s\n", aws_region)
return kinesis.New(auth, aws_region)
}
func getKinesisConfig(route *router.Route) batchproducer.Config {
AddBlocksWhenBufferFull := false
AddBlocksWhenBufferFull_string, ok := route.Options["add_blocks_when_buffer_full"];
if ok && AddBlocksWhenBufferFull_string != "" {
if b, err := strconv.ParseBool(AddBlocksWhenBufferFull_string); err == nil {
AddBlocksWhenBufferFull = b
}
}
BufferSize := 10000
BufferSize_string, ok := route.Options["buffer_size"];
if ok && BufferSize_string != "" {
if i, err := strconv.ParseInt(AddBlocksWhenBufferFull_string, 10, 0); err == nil {
BufferSize = int(i)
}
}
FlushInterval := 1 * time.Second
FlushInterval_string, ok := route.Options["flush_interval"];
if ok && FlushInterval_string != "" {
if i, err := strconv.ParseInt(FlushInterval_string, 10, 0); err == nil {
FlushInterval = time.Duration(i) * time.Second
}
}
BatchSize := 10
BatchSize_string, ok := route.Options["batch_size"];
if ok && BatchSize_string != "" {
if i, err := strconv.ParseInt(BatchSize_string, 10, 0); err == nil {
BatchSize = int(i)
}
}
MaxAttemptsPerRecord := 10
MaxAttemptsPerRecord_string, ok := route.Options["max_attempts_per_record"];
if ok && MaxAttemptsPerRecord_string != "" {
if i, err := strconv.ParseInt(MaxAttemptsPerRecord_string, 10, 0); err == nil {
MaxAttemptsPerRecord = int(i)
}
}
StatInterval := 1 * time.Second
StatInterval_string, ok := route.Options["start_interval"];
if ok && StatInterval_string != "" {
if i, err := strconv.ParseInt(StatInterval_string, 10, 0); err == nil {
StatInterval = time.Duration(i) * time.Second
}
}
return batchproducer.Config{
AddBlocksWhenBufferFull: AddBlocksWhenBufferFull,
BufferSize: BufferSize,
FlushInterval: FlushInterval,
BatchSize: BatchSize,
MaxAttemptsPerRecord: MaxAttemptsPerRecord,
StatInterval: StatInterval,
Logger: log.New(os.Stderr, "kinesis: ", log.LstdFlags),
}
}
func getopt(name, dfault string) string {
value := os.Getenv(name)
if value == "" {
value = dfault
}
return value
}
func (ka *KinesisAdapter) Stream(logstream chan *router.Message) {
// Start the producer
err := ka.batch_producer.Start()
if err != nil {
fmt.Printf("Unable to start batchproducer: %v", err)
os.Exit(1)
}
// Register stop
defer ka.batch_producer.Stop()
// Handle log messages
mute := false
for m := range logstream {
msg := createLogstashMessage(m, ka.docker_host, ka.use_v0)
// Create json from log message
log_json, err := json.Marshal(msg)
if err != nil {
if !mute {
log.Println("logspoutkinesis: error on json.Marshal (muting until restored): %v\n", err)
mute = true
}
continue
}
// Send log message to kinesis
err = ka.batch_producer.Add(log_json, ka.docker_host)
if err != nil {
if !mute {
log.Println("logspoutkinesis: error on batchproducer.Stop (muting until restored): %v\n", err)
mute = true
}
continue
}
// Unmute
mute = false
}
}
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:]
labels := m.Container.Config.Labels
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,
Labels: labels,
},
},
}
}
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,
Labels: labels,
},
}
}