-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkinesis-tail.go
122 lines (97 loc) · 2.48 KB
/
kinesis-tail.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kinesis"
"os"
"time"
)
func getShards(svc *kinesis.Kinesis, streamName string) ([]string, error) {
params := &kinesis.DescribeStreamInput{
StreamName: aws.String(streamName),
}
resp, err := svc.DescribeStream(params)
if err != nil {
return nil, err
}
shards := make([]string, 0, len(resp.StreamDescription.Shards))
for _, s := range resp.StreamDescription.Shards {
shards = append(shards, *s.ShardId)
}
return shards, nil
}
func getInitialShardIterator(svc *kinesis.Kinesis, streamName string, shardId string) (*string, error) {
params := &kinesis.GetShardIteratorInput{
ShardId: aws.String(shardId),
ShardIteratorType: aws.String("LATEST"),
StreamName: aws.String(streamName),
Timestamp: aws.Time(time.Now()),
}
resp, err := svc.GetShardIterator(params)
if err != nil {
return nil, err
}
return resp.ShardIterator, nil
}
func readStream(messages chan string, svc *kinesis.Kinesis, streamName string, shardId string) {
nextShardIterator, err := getInitialShardIterator(svc, streamName, shardId)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return
}
for {
getParams := &kinesis.GetRecordsInput{
ShardIterator: nextShardIterator,
}
resp, err := svc.GetRecords(getParams)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return
}
for _, e := range resp.Records {
messages <- string(e.Data)
}
nextShardIterator = resp.NextShardIterator
if len(resp.Records) == 0 {
time.Sleep(1000 * time.Millisecond)
}
}
}
func mkKinesisService() (*kinesis.Kinesis, error) {
conf := aws.NewConfig()
if os.Getenv("AWS_REGION") == "" {
conf = conf.WithRegion(endpoints.UsEast1RegionID)
}
sess, err := session.NewSession(conf)
if err != nil {
return nil, err
}
svc := kinesis.New(sess)
return svc, nil
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: kinesis-tail [stream-name]\n")
os.Exit(1)
}
streamName := os.Args[1]
svc, err := mkKinesisService()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
shardIds, err := getShards(svc, streamName)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
messages := make(chan string, 10)
for _, shardId := range shardIds {
go readStream(messages, svc, streamName, shardId)
}
for {
fmt.Println(<-messages)
}
}