This repository was archived by the owner on Apr 9, 2026. It is now read-only.
forked from sam701/awstools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkinesis.go
More file actions
143 lines (125 loc) · 2.96 KB
/
Copy pathkinesis.go
File metadata and controls
143 lines (125 loc) · 2.96 KB
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
package main
import (
"fmt"
"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
}
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"),
!c.Bool("no-timestamp"),
eventsChan,
)
}
ch := make(chan bool)
<-ch
return nil
}
func searchInShard(
client *kinesis.Kinesis,
streamName string,
shardId *string,
patterns []string,
trimHorizon bool,
printTimestamp bool,
eventsToPrint chan<- *eventToPrint) {
shardIteratorType := "LATEST"
if trimHorizon {
shardIteratorType = "TRIM_HORIZON"
}
itOut, err := client.GetShardIterator(&kinesis.GetShardIteratorInput{
StreamName: aws.String(streamName),
ShardId: shardId,
ShardIteratorType: aws.String(shardIteratorType),
})
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 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)
}
}
}