-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
416 lines (344 loc) · 12.3 KB
/
main.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math"
"os"
"os/signal"
"strings"
"sync/atomic"
"syscall"
"text/template"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/lestrrat-go/strftime"
getopt "github.com/pborman/getopt/v2"
)
const maxBackoff = time.Duration(10 * time.Second)
const maxSleepWithoutSequence = time.Duration(60 * time.Second)
// WriteUsageToStderr print usage information to os.Stderr. This is provided
// for signature compatibility is getopt.SetUsage.
func WriteUsageToStderr() {
WriteUsage(os.Stderr)
}
// WriteUsage prints usage information to the specified Writer.
func WriteUsage(w io.Writer) {
fmt.Fprintf(w, `kinesis-log-watcher [options] <stream-name>
Watch incoming log entries from a Kinesis stream. This is intended to be a
companion to kinesis-log-streamer.
Valid durations are a number followed by a unit abbreviation.
Examples: 30s, 5m, 3h, 1d.
The format string uses the Go template format; full documentation is available
at https://golang.org/pkg/text/template/. Fields available are:
{{.HostId}} {{.HostID}} -- The full ARN of the host generating the log.
{{.ShortHostId}} {{.ShortHostID}} -- Short hostname (just the last part).
{{.Timestamp}} -- The timestamp when the log was sent to Kinesis.
{{.LogEntry}} -- The log entry in string format.
{{.Log}} -- If the log entry could be parsed as JSON, the resulting JSON
structure. You can get embedded fields using {{.Log.FieldName}}.
To format the timestamp in different formats (e.g. ISO 8601), you can use either:
{{.Timestamp Format "2005-01-02T15:04:05Z"}} or
{{strftime "%%Y-%%m-%%dT%%H:%%M:%%S" .Timestamp}}
`)
getopt.PrintUsage(w)
}
func main() {
getopt.BoolLong("help", 'h', "Show this usage information.")
getopt.BoolLong("one-shot", 'O', "Display logs only once.")
getopt.StringLong("format", 'f', `{{.ShortHostId}} {{.Timestamp}} {{.LogEntry}}`, "Format template for log entries.", "<template>")
getopt.StringLong("start", 's', "5m", "Start time to start polling from, specified as a duration from the current time.", "<duration>")
getopt.StringLong("watch", 'w', "10s", "Watch/poll time, specified as a duration.", "<duration>")
getopt.StringLong("region", 'r', "", "The AWS region to use. If unspecified, the value from the $AWS_REGION environment variable is used.", "<region>")
getopt.StringLong("profile", 'p', "", "If specified, obtain AWS credentials from the specified profile in ~/.aws/credentials.", "<profile>")
getopt.SetUsage(WriteUsageToStderr)
getopt.Parse()
if getopt.GetCount('h') > 0 {
WriteUsage(os.Stdout)
os.Exit(0)
}
format := getopt.GetValue('f')
startTimeStr := getopt.GetValue('s')
startTimeDuration, err := time.ParseDuration(startTimeStr)
if err != nil || startTimeDuration < 0 {
fmt.Fprintf(os.Stderr, "Invalid start time: %s\n", startTimeStr)
WriteUsageToStderr()
os.Exit(2)
}
oneShot := bool(getopt.GetCount('O') > 0)
var pollTime time.Duration
if oneShot {
pollTime = math.MaxInt64
} else {
pollTimeStr := getopt.GetValue('w')
pollTime, err = time.ParseDuration(pollTimeStr)
if err != nil || pollTime <= 0 {
fmt.Fprintf(os.Stderr, "Invalid watch/poll time: %s\n", pollTimeStr)
WriteUsageToStderr()
os.Exit(2)
}
}
formatTemplate := template.New("format")
// Register template functions
templateFuncs := make(template.FuncMap)
templateFuncs["strftime"] = templateStrftime
formatTemplate = formatTemplate.Funcs(templateFuncs)
formatTemplate, err = formatTemplate.Parse(format)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid format: %s: %v\n", format, err)
WriteUsageToStderr()
os.Exit(2)
}
// Make sure we have a stream name.
args := getopt.Args()
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "Kinesis stream must be specified.\n")
WriteUsageToStderr()
os.Exit(2)
}
if len(args) > 1 {
fmt.Fprintf(os.Stderr, "Unknown argument: %s\n", args[1])
WriteUsageToStderr()
os.Exit(2)
}
kinesisStreamName := args[0]
region := getopt.GetValue('r')
profile := getopt.GetValue('p')
awsSessionOptions := session.Options{Profile: profile}
if region != "" {
awsSessionOptions.Config.Region = aws.String(region)
}
awsSession, err := session.NewSessionWithOptions(awsSessionOptions)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to create an AWS session: %v\n", err)
os.Exit(1)
}
kinesisClient := kinesis.New(awsSession)
listShardsInput := kinesis.ListShardsInput{StreamName: aws.String(kinesisStreamName)}
shardIDs := make(map[string]bool)
closeRequestedChan := make(chan bool, 16)
doneChan := make(chan string, 16)
for {
output, err := kinesisClient.ListShards(&listShardsInput)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list shards for Kinesis stream %s: %v\n", kinesisStreamName, err)
os.Exit(1)
}
for _, shard := range output.Shards {
shardID := aws.StringValue(shard.ShardId)
shardIDs[shardID] = true
}
if output.NextToken == nil {
break
}
// More shards to list
listShardsInput.StreamName = nil
listShardsInput.NextToken = output.NextToken
}
startTime := time.Now().UTC().Add(-startTimeDuration)
for shardID := range shardIDs {
go processShard(kinesisClient, kinesisStreamName, shardID, startTime, pollTime, formatTemplate, closeRequestedChan, doneChan)
}
interruptChan := make(chan os.Signal)
closeRequested := false
// Keep running until we're interrupted.
if !oneShot {
signal.Notify(interruptChan, syscall.SIGINT)
} else {
close(closeRequestedChan)
closeRequested = true
}
for len(shardIDs) > 0 {
select {
case <-interruptChan:
if !closeRequested {
fmt.Fprintf(os.Stderr, "Ctrl+C received; stopping all shard processors.\n")
close(closeRequestedChan)
closeRequested = true
}
case shardID := <-doneChan:
_, present := shardIDs[shardID]
if !present {
fmt.Fprintf(os.Stderr, "Got spurious done response from %s\n", shardID)
} else {
delete(shardIDs, shardID)
}
}
}
os.Exit(0)
}
func signalDone(shardID string, doneChan chan string) {
doneChan <- shardID
}
func getRecords(kinesisClient *kinesis.Kinesis, shardIterator string) (*kinesis.GetRecordsOutput, error) {
gri := kinesis.GetRecordsInput{ShardIterator: aws.String(shardIterator), Limit: aws.Int64(10000)}
backoff := time.Duration(50 * time.Millisecond)
for {
gro, err := kinesisClient.GetRecords(&gri)
if err == nil {
return gro, nil
}
// Is this a throttling exception? If so, back off and retry.
if awsErr, castOk := err.(awserr.Error); castOk {
errCode := awsErr.Code()
if errCode == "ProvisionedThroughputExceededException" || errCode == "KMSThrottlingException" {
time.Sleep(backoff)
backoff = (3 * backoff) / 2
if backoff > maxBackoff {
backoff = maxBackoff
}
continue
}
}
// Nope, propagate the error.
return nil, err
}
}
var formatErrorPrinted uint32 = 0
func printRecords(records []*kinesis.Record, formatTemplate *template.Template) {
for _, record := range records {
// The partition key is actually the host ID, in ARN format or ip-address:a.b.c.d or uuid:...
hostID := aws.StringValue(record.PartitionKey)
// Compute a short host ID that eliminates most of the ARN garbage
hostIDParts := strings.Split(hostID, ":")
shortHostID := hostIDParts[len(hostIDParts)-1]
if strings.HasPrefix(shortHostID, "task/") {
// For ECS tasks, remove the task/ prefix.
shortHostID = shortHostID[5:]
} else if strings.HasPrefix(shortHostID, "instance/") {
// for EC2 instances, remove the instance/ prefix.
shortHostID = shortHostID[9:]
}
logData := make(map[string]interface{})
logData["Timestamp"] = aws.TimeValue(record.ApproximateArrivalTimestamp)
logData["HostId"] = hostID
logData["HostID"] = hostID
logData["ShortHostId"] = shortHostID
logData["ShortHostID"] = shortHostID
logData["SequenceNumber"] = aws.StringValue(record.SequenceNumber)
logData["LogEntry"] = string(record.Data)
var jsonData interface{}
err := json.Unmarshal(record.Data, &jsonData)
if err != nil {
logData["Log"] = nil
} else {
logData["Log"] = jsonData
}
lineBuffer := new(bytes.Buffer)
err = formatTemplate.Execute(lineBuffer, logData)
if err != nil {
if atomic.CompareAndSwapUint32(&formatErrorPrinted, 0, 1) {
fmt.Fprintf(os.Stderr, "Template formatting error: %v\n", err)
}
} else {
fmt.Println(string(lineBuffer.Bytes()))
}
}
os.Stdout.Sync()
}
func processShard(kinesisClient *kinesis.Kinesis, streamName string, shardID string, startTime time.Time, pollTime time.Duration, formatTemplate *template.Template, closeRequestedChan chan bool, doneChan chan string) {
// Send a done signal for this shard upon return
defer signalDone(shardID, doneChan)
// Get the first shard iterator, starting at the specified start time.
gsii := kinesis.GetShardIteratorInput{
StreamName: aws.String(streamName),
ShardId: aws.String(shardID),
ShardIteratorType: aws.String("AT_TIMESTAMP"),
Timestamp: aws.Time(startTime),
}
gsio, err := kinesisClient.GetShardIterator(&gsii)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to get initial shard iterator for stream %s shard %s: %v\n", streamName, shardID, err)
return
}
shardIterator := aws.StringValue(gsio.ShardIterator)
if shardIterator == "" {
fmt.Fprintf(os.Stderr, "Unable to get initial shard iterator for stream %s shard %s: empty shard iterator\n", streamName, shardID)
return
}
// Loop over log entries until we've been requested to exit.
latestSequenceNumber := ""
for {
var gro *kinesis.GetRecordsOutput
var err error
// If this isn't the first we're reading logs (e.g., after we've paused), we may end up with an expired shard iterator
for retries := 0; retries < 2; retries++ {
gro, err = getRecords(kinesisClient, shardIterator)
if err == nil {
break
}
if awsErr, castOk := err.(awserr.Error); retries == 0 && castOk {
if awsErr.Code() == "ExpiredIteratorException" && latestSequenceNumber != "" {
// Get a new shard iterator.
gsii.ShardIteratorType = aws.String("AT_SEQUENCE_NUMBER")
gsii.StartingSequenceNumber = aws.String(latestSequenceNumber)
gsii.Timestamp = nil
gsio, err = kinesisClient.GetShardIterator(&gsii)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to renew shard iterator for stream %s shard %s: %v\n", streamName, shardID, err)
return
}
shardIterator := aws.StringValue(gsio.ShardIterator)
if shardIterator == "" {
fmt.Fprintf(os.Stderr, "Unable to renew initial shard iterator for stream %s shard %s: empty shard iterator\n", streamName, shardID)
return
}
continue
}
}
fmt.Fprintf(os.Stderr, "Unable to get initial records for stream %s shard %s: %v\n", streamName, shardID, err)
return
}
if len(gro.Records) > 0 {
printRecords(gro.Records, formatTemplate)
// Grab the sequence number from the last record
latestSequenceNumber = aws.StringValue(gro.Records[len(gro.Records)-1].SequenceNumber)
}
if aws.Int64Value(gro.MillisBehindLatest) == 0 {
// We're caught up.
currentPollTime := pollTime
if latestSequenceNumber == "" {
// We don't have a sequence number to grab a new shard iterator; don't sleep for longer than 1 minute
if currentPollTime > maxSleepWithoutSequence {
currentPollTime = maxSleepWithoutSequence
}
}
// Pause until we've been requested to exit or our poll timer expires.
select {
case <-closeRequestedChan:
return
case <-time.After(currentPollTime):
// Just fall through.
}
} else {
// Stop if we've been asked to exit.
select {
case <-closeRequestedChan:
return
default:
// Just fall through.
}
// We're not caught up; keep going.
shardIterator = aws.StringValue(gro.NextShardIterator)
}
}
}
var invalidTimestampFormatErrorPrinted uint32 = 0
func templateStrftime(timeFormat string, timeGeneric interface{}) string {
timeProper, castOk := timeGeneric.(time.Time)
if !castOk {
return ""
}
result, err := strftime.Format(timeFormat, timeProper, strftime.WithMicroseconds('f'), strftime.WithMilliseconds('L'), strftime.WithUnixSeconds('s'))
if err == nil {
return result
}
if atomic.CompareAndSwapUint32(&invalidTimestampFormatErrorPrinted, 0, 1) {
fmt.Fprintf(os.Stderr, "Invalid timestamp format: %s. Using RFC3339Nano instead.\n", timeFormat)
}
return timeProper.Format(time.RFC3339Nano)
}