-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkinesis.go
178 lines (159 loc) · 3.79 KB
/
kinesis.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
package main
import (
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/sam701/awstools/colors"
"github.com/sam701/awstools/sess"
"github.com/urfave/cli"
)
func kinesisPrintRecords(c *cli.Context) error {
client := kinesis.New(sess.FromEnvVar())
if c.Bool("list-streams") {
out, err := client.ListStreams(&kinesis.ListStreamsInput{})
if err != nil {
log.Fatalln("ERROR", err)
}
for _, s := range out.StreamNames {
fmt.Println(*s)
}
return nil
}
streamName := c.String("search-stream")
if streamName == "" {
cli.ShowCommandHelp(c, "kinesis")
return nil
}
var atTimestamp *time.Time
if str := c.String("at-timestamp"); str != "" {
t, err := time.Parse(time.RFC3339, str)
if err != nil {
return errors.New("Cannot parse time " + err.Error())
}
atTimestamp = &t
}
dsr, err := client.DescribeStream(&kinesis.DescribeStreamInput{
StreamName: aws.String(streamName),
Limit: aws.Int64(1000),
})
if err != nil {
log.Fatalln("ERROR", err)
}
eventsChan := make(chan *eventToPrint)
go printEvents(eventsChan)
for _, shard := range dsr.StreamDescription.Shards {
go searchInShard(client, streamName, shard.ShardId,
c.StringSlice("pattern"),
c.Bool("trim-horizon"),
atTimestamp,
!c.Bool("no-timestamp"),
c.Bool("gunzip"),
eventsChan,
)
}
ch := make(chan bool)
<-ch
return nil
}
func searchInShard(
client *kinesis.Kinesis,
streamName string,
shardId *string,
patterns []string,
trimHorizon bool,
atTimestamp *time.Time,
printTimestamp bool,
gunzip bool,
eventsToPrint chan<- *eventToPrint) {
siInput := &kinesis.GetShardIteratorInput{
StreamName: aws.String(streamName),
ShardId: shardId,
}
siInput.ShardIteratorType = aws.String("LATEST")
if trimHorizon {
siInput.ShardIteratorType = aws.String("TRIM_HORIZON")
} else if atTimestamp != nil {
siInput.ShardIteratorType = aws.String("AT_TIMESTAMP")
siInput.Timestamp = atTimestamp
}
itOut, err := client.GetShardIterator(siInput)
if err != nil {
log.Fatalln("ERROR", err)
}
shardIterator := itOut.ShardIterator
for {
if shardIterator == nil {
return
}
rOut, err := client.GetRecords(&kinesis.GetRecordsInput{
ShardIterator: shardIterator,
})
if err != nil {
log.Fatalln("ERROR", err)
}
shardIterator = rOut.NextShardIterator
for _, record := range rOut.Records {
if gunzip {
r, err := gzip.NewReader(bytes.NewReader(record.Data))
if err != nil {
log.Println("Cannot unzip data", err, string(record.Data))
continue
}
var output bytes.Buffer
_, err = io.Copy(&output, r)
if err != nil {
log.Println("Cannot unzip data", err, string(record.Data))
continue
}
r.Close()
record.Data = output.Bytes()
}
if len(patterns) == 0 {
eventsToPrint <- &eventToPrint{record, patterns, printTimestamp}
} else {
str := string(record.Data)
matched := true
for _, pat := range patterns {
if !strings.Contains(str, pat) {
matched = false
break
}
}
if matched {
eventsToPrint <- &eventToPrint{record, patterns, printTimestamp}
}
}
}
time.Sleep(1 * time.Second)
}
}
type eventToPrint struct {
record *kinesis.Record
patterns []string
printTimestamp bool
}
func printEvents(events <-chan *eventToPrint) {
for event := range events {
if event.printTimestamp {
tt := event.record.ApproximateArrivalTimestamp.Format(time.RFC3339)
tt = colors.Timestamp(tt)
fmt.Print(tt + " ")
}
str := strings.TrimSpace(string(event.record.Data))
if len(event.patterns) == 0 {
fmt.Println(str)
} else {
for _, pat := range event.patterns {
str = strings.Replace(str, pat, colors.Match(pat), -1)
}
fmt.Println(str)
}
}
}