Skip to content

Commit 0d3999d

Browse files
committed
- Refactor log collection for better readability
- Change `zip` to `tar` as more systems have `tar` available - Remove references to absolute paths
1 parent 7104fe4 commit 0d3999d

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

utils/log-collector

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ set -euo pipefail
1616

1717
usage() {
1818
cat << EOF
19-
Usage: ./bk-log-collector -s <stack-name> -i <instance-id>
19+
Usage: ./log-collector -s <stack-name> -i <instance-id>
2020
Collect Buildkite Elastic CI stack logs from AWS CloudWatch
2121
-s Auto Scaling Group Name, can be found at console.aws.amazon.com/ec2autoscaling/
2222
-i Instance ID, the AWS instance ID for the instance you want to collect logs for
@@ -54,48 +54,55 @@ now=$(date +"%F")
5454
outputFormat="${outputFormat:=text}"
5555

5656
# Create a temporary directory to store our logs
57-
pwd=$(pwd)
58-
dir=$(mktemp -d "$pwd"/buildkite-logs-"$now")
57+
#pwd=$(pwd)
58+
dir=$(mktemp -d buildkite-logs-"$now")
5959

6060
echo "Collecting logs for instance $instanceId"
6161

6262
# Collect the logs for the instance
63-
aws logs get-log-events --log-group-name /buildkite/buildkite-agent --log-stream-name "$instanceId" --output "$outputFormat" >> "$dir"/buildkite-agent-"$instanceId".log
64-
aws logs get-log-events --log-group-name /buildkite/system --log-stream-name "$instanceId" --output "$outputFormat" >> "$dir"/system-"$instanceId".log
65-
aws logs get-log-events --log-group-name /buildkite/lifecycled --log-stream-name "$instanceId" --output "$outputFormat" >> "$dir"/lifecycled-"$instanceId".log
66-
aws logs get-log-events --log-group-name /buildkite/docker-daemon --log-stream-name "$instanceId" --output "$outputFormat" >> "$dir"/docker-daemon-"$instanceId".log
63+
for name in buildkite-agent system lifecycled docker-daemon; do
64+
aws logs get-log-events \
65+
--log-group-name "/buildkite/$name" \
66+
--log-stream-name "$instanceId" \
67+
--output "$outputFormat" \
68+
>> "$dir/$name-$instanceId.log"
69+
done
6770

6871
# CloudWatch stores the creationTime as milliseconds since epoch UTC.
6972
# Since macOS doesn't support calculating the time since epoch in milliseconds, we don't have perfect accuracy on this, but it should be as close to 24 hours as possible
73+
# while still being inclusive to all platforms that might run the script
74+
7075
# Currently only collects the last 24h of logs, but it might be good to be able to specify a different value for this.
7176
logAge=$(date -v-1d "+%s000")
7277

7378
# Collect some information about the Lambda function and associated log groups
74-
# Change the timeframe to be dynamically set with a default of 24h. Currently collects the last 24 hours of Lambda logs
7579
lambdaFunction=$(aws cloudformation describe-stack-resources --stack-name "$stackName" --logical-resource-id Autoscaling --query "StackResources[*].PhysicalResourceId" --output text | sed -nr 's/.*stack\/(.*)\/.*/\1/p')
7680
lambdaLogGroup=$(aws cloudformation describe-stack-resources --stack-name "$lambdaFunction" --logical-resource-id LogGroup --query "StackResources[*].PhysicalResourceId" --output text)
7781
logStreams=$(aws logs describe-log-streams --log-group-name "$lambdaLogGroup" --order-by LastEventTime --query "logStreams[?creationTime > \`$logAge\`] | [*].logStreamName" --output text | tr -s '[:space:]' '\n')
7882

7983
# Iterate through all of the log streams we collected from the lambda log group
80-
8184
echo "Collecting Lambda logs for $lambdaFunction"
8285
for i in $logStreams
8386
do
8487
fileName=$(echo "$i" | sed -r 's/\//-/g')
85-
aws logs get-log-events --log-group-name "$lambdaLogGroup" --log-stream-name "$i" --output "$outputFormat" >> "$dir"/lambda-"$fileName".log
88+
aws logs get-log-events --log-group-name "$lambdaLogGroup" \
89+
--log-stream-name "$i" \
90+
--output "$outputFormat" \
91+
>> "${dir}/lambda-${fileName}.log"
8692
done
8793

8894
# Collect ASG activity for the scaling group
8995
autoScalingGroup=$(aws cloudformation describe-stack-resources --stack-name "$stackName" --logical-resource-id AgentAutoScaleGroup --query "StackResources[*].PhysicalResourceId" --output text)
9096
echo "Collecting ASG activity for $autoScalingGroup"
91-
aws autoscaling describe-scaling-activities --auto-scaling-group-name "$autoScalingGroup" >> "$dir"/asg-activities-"$stackName".log
97+
aws autoscaling describe-scaling-activities --auto-scaling-group-name "$autoScalingGroup" >> "${dir}/asg-activities-${stackName}.log"
9298

9399
# Make an archive with the logs
94-
zip -jq buildkite-logs-"$now".zip "$dir"/*.log
100+
## zip -jq buildkite-logs-"$now".zip "$dir"/*.log
101+
tar -czvf buildkite-logs-"$now".tar.gz "$dir"/*.log
95102

96103
# Clean up the unneeded files
97104
rm -r "$dir"
98105

99-
echo "Finished collecting logs, you can find them here: $pwd/buildkite-logs-$now.zip"
106+
echo "Finished collecting logs, you can find them here: ${dir}/buildkite-logs-${now}.tar.gz"
100107

101108
exit 0

0 commit comments

Comments
 (0)