Monday, December 5, 2022

Getting AWS Batch Logs in Go


 


In the previous post AWS Batch in Go, we've started AWS batches, and tracked their execution, waiting for the completion. Once an AWS batch had failed, why won't we simplify the problem investigation, and fetch the log?

To get a job log, we use first AWS batch API to get the job stream name:


input := batch.DescribeJobsInput{Jobs: []*string{aws.String(jobId)}}
output, err := awsBatch.DescribeJobs(&input)
if err != nil {
panic(err)
}

streamName := output.Jobs[0].Container.LogStreamName



Then we use the AWS cloud watch API to get the events, while limit to get only tail of 100 events.


events, err := awsCloudWatchLog.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
Limit: aws.Int64(100),
LogGroupName: aws.String("/aws/batch/job"),
LogStreamName: streamName,
})


The full function is:


func GetJobLogs(jobId string) string {
awsSession, err := session.NewSession()
if err != nil {
panic(err)
}

awsBatch := batch.New(awsSession)

input := batch.DescribeJobsInput{Jobs: []*string{aws.String(jobId)}}
output, err := awsBatch.DescribeJobs(&input)
if err != nil {
panic(err)
}

streamName := output.Jobs[0].Container.LogStreamName

awsCloudWatchLog := cloudwatchlogs.New(awsSession)
events, err := awsCloudWatchLog.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
Limit: aws.Int64(100),
LogGroupName: aws.String("/aws/batch/job"),
LogStreamName: streamName,
})

var lines []string
for _, event := range events.Events {
eventTime := time.UnixMilli(*event.Timestamp)
line := fmt.Sprintf("%v %v", times.NiceTime(&eventTime), *event.Message)
lines = append(lines, line)
}

return strings.Join(lines, "\n")
}



No comments:

Post a Comment