forked from aws/amazon-ssm-agent
/
cloudwatchlogsservice.go
395 lines (317 loc) · 14.4 KB
/
cloudwatchlogsservice.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
// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
// cloudwatchlogspublisher is responsible for pulling logs from the log queue and publishing them to cloudwatch
package cloudwatchlogspublisher
import (
"errors"
"github.com/aws/amazon-ssm-agent/agent/agentlogstocloudwatch/cloudwatchlogspublisher/cloudwatchlogsinterface"
"github.com/aws/amazon-ssm-agent/agent/log"
"github.com/aws/amazon-ssm-agent/agent/sdkutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
const (
stopPolicyErrorThreshold = 10
stopPolicyName = "CloudWatchLogsService"
maxRetries = 5
)
// CloudWatchLogsService encapsulates the client and stop policy as a wrapper to call the cloudwatchlogs API
type CloudWatchLogsService struct {
cloudWatchLogsClient cloudwatchlogsinterface.CloudWatchLogsClient
stopPolicy *sdkutil.StopPolicy
}
// createCloudWatchStopPolicy creates a new policy for cloudwatchlogs
func createCloudWatchStopPolicy() *sdkutil.StopPolicy {
return sdkutil.NewStopPolicy(stopPolicyName, stopPolicyErrorThreshold)
}
// createCloudWatchClient creates a client to call CloudWatchLogs APIs
func createCloudWatchClient() cloudwatchlogsinterface.CloudWatchLogsClient {
config := sdkutil.AwsConfig()
//Adding the AWS SDK Retrier with Exponential Backoff
config = request.WithRetryer(config, client.DefaultRetryer{
NumMaxRetries: maxRetries,
})
sess := session.New(config)
return cloudwatchlogs.New(sess)
}
// createCloudWatchClientWithCredentials creates a client to call CloudWatchLogs APIs using credentials from the id and secret passed
func createCloudWatchClientWithCredentials(id, secret string) cloudwatchlogsinterface.CloudWatchLogsClient {
config := sdkutil.AwsConfig().WithCredentials(credentials.NewStaticCredentials(id, secret, ""))
//Adding the AWS SDK Retrier with Exponential Backoff
config = request.WithRetryer(config, client.DefaultRetryer{
NumMaxRetries: maxRetries,
})
sess := session.New(config)
return cloudwatchlogs.New(sess)
}
// NewCloudWatchLogsService Creates a new instance of the CloudWatchLogsService
func NewCloudWatchLogsService() *CloudWatchLogsService {
cloudWatchLogsService := CloudWatchLogsService{
cloudWatchLogsClient: createCloudWatchClient(),
stopPolicy: createCloudWatchStopPolicy(),
}
return &cloudWatchLogsService
}
// NewCloudWatchLogsServiceWithCredentials Creates a new instance of the CloudWatchLogsService using credentials from the Id and Secret passed
func NewCloudWatchLogsServiceWithCredentials(id, secret string) *CloudWatchLogsService {
cloudWatchLogsService := CloudWatchLogsService{
cloudWatchLogsClient: createCloudWatchClientWithCredentials(id, secret),
stopPolicy: createCloudWatchStopPolicy(),
}
return &cloudWatchLogsService
}
// CreateNewServiceIfUnHealthy checks service healthy and create new service if original is unhealthy
func (service *CloudWatchLogsService) CreateNewServiceIfUnHealthy() {
if service.stopPolicy == nil {
service.stopPolicy = createCloudWatchStopPolicy()
}
if !service.stopPolicy.IsHealthy() {
service.stopPolicy.ResetErrorCount()
service.cloudWatchLogsClient = createCloudWatchClient()
return
}
}
// CreateLogGroup calls the CreateLogGroup API to create a log group
func (service *CloudWatchLogsService) CreateLogGroup(log log.T, logGroup string) (err error) {
service.CreateNewServiceIfUnHealthy()
//Creating the parameters for the API Call
params := &cloudwatchlogs.CreateLogGroupInput{
LogGroupName: aws.String(logGroup),
}
//Calling the API
if _, err = service.cloudWatchLogsClient.CreateLogGroup(params); err != nil {
// Handle the common AWS errors and update the stop policy accordingly
sdkutil.HandleAwsError(log, err, service.stopPolicy)
// Cast err to awserr.Error to get the Code
errorCode := sdkutil.GetAwsErrorCode(err)
switch errorCode {
// Check for error code. Note that the AWS Retrier has already made retries for the 5xx Response Codes
case resourceAlreadyExistsException:
// 400 Error, occurs when the LogGroup already exists
// Ignoring the error
err = nil
default:
// Other 400 Errors, 500 Errors even after retries. Log the error
log.Errorf("Error Calling CreateLogGroup:%v", err.Error())
}
}
return
}
// CreateLogStream calls the CreateLogStream API to create log stream within the specified log group
func (service *CloudWatchLogsService) CreateLogStream(log log.T, logGroup, logStream string) (err error) {
service.CreateNewServiceIfUnHealthy()
//Creating the parameters for the API Call
params := &cloudwatchlogs.CreateLogStreamInput{
LogGroupName: aws.String(logGroup),
LogStreamName: aws.String(logStream),
}
//Calling the API
if _, err = service.cloudWatchLogsClient.CreateLogStream(params); err != nil {
// Handle the common AWS errors and update the stop policy accordingly
sdkutil.HandleAwsError(log, err, service.stopPolicy)
// Cast err to awserr.Error to get the Code
errorCode := sdkutil.GetAwsErrorCode(err)
switch errorCode {
// Check for error code. Note that the AWS Retrier has already made retries for the 5xx Response Codes
case resourceAlreadyExistsException:
// 400 Error, occurs when the LogStream already exists
// Ignoring the error
err = nil
default:
// Other 400 Errors, 500 Errors even after retries. Log the error
log.Errorf("Error Calling CreateLogStream:%v", err.Error())
}
}
return
}
// DescribeLogGroups calls the DescribeLogGroups API to get the details of log groups of account
func (service *CloudWatchLogsService) DescribeLogGroups(log log.T, logGroupPrefix, nextToken string) (response *cloudwatchlogs.DescribeLogGroupsOutput, err error) {
service.CreateNewServiceIfUnHealthy()
// Creating the parameters for the API Call
params := &cloudwatchlogs.DescribeLogGroupsInput{}
if logGroupPrefix != "" {
params.LogGroupNamePrefix = aws.String(logGroupPrefix)
}
if nextToken != "" {
params.NextToken = aws.String(nextToken)
}
// Calling the API
if response, err = service.cloudWatchLogsClient.DescribeLogGroups(params); err != nil {
// Handle the common AWS errors and update the stop policy accordingly
sdkutil.HandleAwsError(log, err, service.stopPolicy)
// AWS Retrier has already made retries for the 5xx Response Codes. Logging and Returning the error
log.Errorf("Error Calling DescribeLogGroups:%v", err.Error())
return
}
// Pretty-print the response data.
log.Debugf("DescribeLogGroups Response:%v", response)
return
}
// DescribeLogStreams calls the DescribeLogStreams API to get the details of the log streams present
func (service *CloudWatchLogsService) DescribeLogStreams(log log.T, logGroup, logStreamPrefix, nextToken string) (response *cloudwatchlogs.DescribeLogStreamsOutput, err error) {
service.CreateNewServiceIfUnHealthy()
// Creating the parameters for the API Call
params := &cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: aws.String(logGroup),
}
if logStreamPrefix != "" {
params.LogStreamNamePrefix = aws.String(logStreamPrefix)
}
if nextToken != "" {
params.NextToken = aws.String(nextToken)
}
// Calling the API
if response, err = service.cloudWatchLogsClient.DescribeLogStreams(params); err != nil {
// Handle the common AWS errors and update the stop policy accordingly
sdkutil.HandleAwsError(log, err, service.stopPolicy)
// AWS Retrier has already made retries for the 5xx Response Codes. Logging and Returning the error
log.Errorf("Error Calling DescribeLogStreams:%v", err.Error())
return
}
// Pretty-print the response data.
log.Debugf("DescribeLogStreams Response:%v", response)
return
}
//getLogGroupDetails Calls the DescribeLogGroups API to get the details of the loggroup specified. Returns nil if not found
func (service *CloudWatchLogsService) getLogGroupDetails(log log.T, logGroup string) (logGroupDetails *cloudwatchlogs.LogGroup) {
// Keeping the nextToken as empty in the beginning. Might get filled from response for subsequent calls
nextToken := ""
// The API implements paginations. The bool if true means more results are present and thus need to call the API again.
nextBatchPresent := true
// Continue calling the API until we find the group or next batch of groups is not present
for nextBatchPresent {
describeLogGroupsOutput, err := service.DescribeLogGroups(log, logGroup, nextToken)
if err != nil {
log.Errorf("Error in calling DescribeLogGroups:%v", err)
return
}
// Iterate through the log streams and check for the input log stream
for _, stream := range describeLogGroupsOutput.LogGroups {
if logGroup == *stream.LogGroupName {
// Log Group Matched
logGroupDetails = stream
return
}
}
// Group not found. Check if nextToken is returned. If yes, need to call the API again to get the next set of log groups
if describeLogGroupsOutput.NextToken == nil {
// Stream not found and nextToken not present
nextBatchPresent = false
} else {
// There is a NextToken present. Use it to call and continue calling the API
nextToken = *describeLogGroupsOutput.NextToken
}
}
return
}
// IsLogGroupPresent checks and returns true when the log group is present
func (service *CloudWatchLogsService) IsLogGroupPresent(log log.T, logGroup string) bool {
return service.getLogGroupDetails(log, logGroup) != nil
}
// IsLogStreamPresent checks and returns true when the log stream is present
func (service *CloudWatchLogsService) IsLogStreamPresent(log log.T, logGroupName, logStreamName string) bool {
return service.getLogStreamDetails(log, logGroupName, logStreamName) != nil
}
// GetSequenceTokenForStream returns the current sequence token for the stream specified
func (service *CloudWatchLogsService) GetSequenceTokenForStream(log log.T, logGroupName, logStreamName string) (sequenceToken *string) {
logStream := service.getLogStreamDetails(log, logGroupName, logStreamName)
if logStream != nil {
sequenceToken = logStream.UploadSequenceToken
}
return
}
// getLogStreamDetails Calls the DescribeLogStreams API to get the details of the Log Stream specified. Returns nil if the stream is not found
func (service *CloudWatchLogsService) getLogStreamDetails(log log.T, logGroupName, logStreamName string) (logStream *cloudwatchlogs.LogStream) {
// Keeping the nextToken as empty in the beginning. Might get filled from response for subsequent calls
nextToken := ""
// Takes note of whether need to call the API again
nextBatchPresent := true
// Continue calling the API until we find the stream or next batch of streams is not present
for nextBatchPresent {
describeLogStreamsOutput, err := service.DescribeLogStreams(log, logGroupName, logStreamName, nextToken)
if err != nil {
log.Errorf("Error in calling DescribeLogStreams:%v", err)
return
}
// Iterate through the log streams and check for the input log stream
for _, stream := range describeLogStreamsOutput.LogStreams {
if logStreamName == *stream.LogStreamName {
// Log Stream Matched
logStream = stream
return
}
}
// Stream not found. Check if nextToken is returned. If yes, need to call the API again to get the next set of log streams
if describeLogStreamsOutput.NextToken == nil {
// Stream not found and nextToken not present
nextBatchPresent = false
} else {
// There is a NextToken present. Use it to call and continue calling the API
nextToken = *describeLogStreamsOutput.NextToken
}
}
return
}
// PutLogEvents calls the PutLogEvents API to push messages to CloudWatchLogs
func (service *CloudWatchLogsService) PutLogEvents(log log.T, messages []*cloudwatchlogs.InputLogEvent, logGroup, logStream string, sequenceToken *string) (nextSequenceToken *string, err error) {
service.CreateNewServiceIfUnHealthy()
// Creating the parameters for the API Call
params := &cloudwatchlogs.PutLogEventsInput{
LogEvents: messages,
LogGroupName: aws.String(logGroup),
LogStreamName: aws.String(logStream),
SequenceToken: sequenceToken,
}
// Calling the API
response, err := service.cloudWatchLogsClient.PutLogEvents(params)
if err != nil {
// Handle the common AWS errors and update the stop policy accordingly
sdkutil.HandleAwsError(log, err, service.stopPolicy)
// Cast err to awserr.Error to get the Code
errorCode := sdkutil.GetAwsErrorCode(err)
switch errorCode {
// Check for error code. Note that the AWS Retrier has already made retries for the 5xx Response Codes
case invalidSequenceTokenException:
// 400 Error, occurs when the SequenceToken is invalid. Create new SequenceToken and use it again
fallthrough
case dataAlreadyAcceptedException:
// 400 Error, occurs when the SequenceToken has been used. Create new SequenceToken and use it again
// Adding Error Count to StopPolicy before retrying to ensure the retries stop after Stop Policy error counts exceed
service.stopPolicy.AddErrorCount(1)
return service.retryPutWithNewSequenceToken(log, messages, logGroup, logStream)
default:
// Other 400 Errors, 500 Errors even after retries. Log the error
log.Errorf("Error in PutLogEvents:%v", err.Error())
}
return
}
nextSequenceToken = response.NextSequenceToken
return
}
// retryPutWithNewSequenceToken gets a new sequence token and retries pushing messages to cloudwatchlogs
func (service *CloudWatchLogsService) retryPutWithNewSequenceToken(log log.T, messages []*cloudwatchlogs.InputLogEvent, logGroupName, logStreamName string) (*string, error) {
// Get the sequence token by calling the DescribeLogStreams API
logStream := service.getLogStreamDetails(log, logGroupName, logStreamName)
if logStream == nil {
// Failed to get log stream and hence the sequence token. Log the error
err := errors.New("Failed to get sequence token")
return nil, err
}
sequenceToken := logStream.UploadSequenceToken
// Successfully got the new sequence token. Retry the PutLogEvents API
return service.PutLogEvents(log, messages, logGroupName, logStreamName, sequenceToken)
}