@@ -12,6 +12,7 @@ import (
12
12
13
13
"github.com/alecthomas/kingpin"
14
14
"github.com/aws/aws-sdk-go/aws"
15
+ "github.com/aws/aws-sdk-go/aws/credentials"
15
16
"github.com/aws/aws-sdk-go/aws/session"
16
17
"github.com/aws/aws-sdk-go/service/kinesis"
17
18
"github.com/aws/aws-sdk-go/service/kinesis/kinesisiface"
30
31
tracing = kingpin .Flag ("trace" , "Enable trace mode." ).Short ('t' ).Bool ()
31
32
debug = kingpin .Flag ("debug" , "Enable debug logging." ).Short ('d' ).Bool ()
32
33
region = kingpin .Flag ("region" , "Configure the aws region." ).Short ('r' ).String ()
34
+ profile = kingpin .Flag ("profile" , "Configure the aws profile." ).Short ('p' ).String ()
35
+ timestamp = kingpin .Flag ("timestamp" , "Start time in epoch milliseconds." ).Short ('T' ).Int64 ()
33
36
cwlogsCommand = kingpin .Command ("cwlogs" , "Process cloudwatch logs data from kinesis." )
34
37
includes = cwlogsCommand .Flag ("include" , "Include anything in log group names which match the supplied string." ).Strings ()
35
38
excludes = cwlogsCommand .Flag ("exclude" , "Exclude anything in log group names which match the supplied string." ).Strings ()
@@ -58,7 +61,6 @@ func main() {
58
61
}
59
62
60
63
defer trace .Stop ()
61
-
62
64
}
63
65
64
66
if * debug {
@@ -68,44 +70,35 @@ func main() {
68
70
logger .SetLevel (logrus .DebugLevel )
69
71
}
70
72
71
- sess := session .Must (session .NewSession ())
72
-
73
- var svc kinesisiface.KinesisAPI
74
-
75
- if * region == "" {
76
- svc = kinesis .New (sess )
77
- } else {
78
- // Create a Kinesis client with additional configuration
79
- svc = kinesis .New (sess , aws .NewConfig ().WithRegion (* region ))
80
- }
73
+ svc := newKinesis (region , profile )
81
74
82
75
logger .Debug ("built kinesis service" )
83
76
84
77
switch subCommand {
85
78
case "cwlogs" :
86
- err := processLogData (svc , * cwlogsStream , * includes , * excludes )
79
+ err := processLogData (svc , * cwlogsStream , * timestamp , * includes , * excludes )
87
80
if err != nil {
88
81
logger .WithError (err ).Fatal ("failed to process log data" )
89
82
}
90
83
case "raw" :
91
- err := processRawData (svc , * rawStream , * timeout , * count )
84
+ err := processRawData (svc , * rawStream , * timeout , * timestamp , * count )
92
85
if err != nil {
93
86
logger .WithError (err ).Fatal ("failed to process log data" )
94
87
}
95
88
}
96
89
97
90
}
98
91
99
- func processLogData (svc kinesisiface.KinesisAPI , stream string , includes []string , excludes []string ) error {
92
+ func processLogData (svc kinesisiface.KinesisAPI , stream string , timestamp int64 , includes []string , excludes []string ) error {
100
93
101
94
helper := ktail .New (svc , logger )
102
95
103
- iterators , err := helper .GetStreamIterators (stream )
96
+ iterators , err := helper .GetStreamIterators (stream , timestamp )
104
97
if err != nil {
105
98
return errors .Wrap (err , "get iterators failed" )
106
99
}
107
100
108
- kstream := streamer .New (svc , iterators , 5000 )
101
+ kstream := streamer .New (svc , iterators , 5000 , logger )
109
102
ch := kstream .StartGetRecords ()
110
103
111
104
messageSorter := sorter .New (os .Stdout , len (iterators ), formatLogsMsg )
@@ -136,16 +129,16 @@ func processLogData(svc kinesisiface.KinesisAPI, stream string, includes []strin
136
129
return nil
137
130
}
138
131
139
- func processRawData (svc kinesisiface.KinesisAPI , stream string , timeout int64 , count int ) error {
132
+ func processRawData (svc kinesisiface.KinesisAPI , stream string , timeout int64 , timestamp int64 , count int ) error {
140
133
141
134
helper := ktail .New (svc , logger )
142
135
143
- iterators , err := helper .GetStreamIterators (stream )
136
+ iterators , err := helper .GetStreamIterators (stream , timestamp )
144
137
if err != nil {
145
138
return errors .Wrap (err , "get iterators failed" )
146
139
}
147
140
148
- kstream := streamer .New (svc , iterators , 5000 )
141
+ kstream := streamer .New (svc , iterators , 5000 , logger )
149
142
ch := kstream .StartGetRecords ()
150
143
151
144
messageSorter := sorter .New (os .Stdout , len (iterators ), formatRawMsg )
@@ -217,3 +210,19 @@ func formatLogsMsg(wr io.Writer, msg *ktail.LogMessage) {
217
210
logger .WithError (err ).Fatal ("failed to create trace file" )
218
211
}
219
212
}
213
+
214
+ func newKinesis (region , profile * string ) kinesisiface.KinesisAPI {
215
+ sess := session .Must (session .NewSession ())
216
+
217
+ cfg := aws .NewConfig ()
218
+
219
+ if aws .StringValue (region ) != "" {
220
+ cfg = cfg .WithRegion (* region )
221
+ }
222
+
223
+ if aws .StringValue (profile ) != "" {
224
+ cfg = cfg .WithCredentials (credentials .NewSharedCredentials ("" , * profile ))
225
+ }
226
+
227
+ return kinesis .New (sess , cfg )
228
+ }
0 commit comments