Skip to content

Commit 732bd95

Browse files
Merge pull request #1020 from buildkite/update-log-collector
Update log collector
2 parents 7104fe4 + 44c48c1 commit 732bd95

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

utils/log-collector

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

1717
usage() {
1818
cat << EOF
19-
Usage: ./bk-log-collector -s <stack-name> -i <instance-id>
19+
Usage: $0 -s <stack-name> -i <instance-id>
2020
Collect Buildkite Elastic CI stack logs from AWS CloudWatch
21-
-s Auto Scaling Group Name, can be found at console.aws.amazon.com/ec2autoscaling/
21+
-s Stack name, can be found at https://console.aws.amazon.com/cloudformation/home
2222
-i Instance ID, the AWS instance ID for the instance you want to collect logs for
23-
-o Output format for logs(optional flag), supported values are text (default value), json, table
23+
-o Output format for logs (optional flag), supported values are text (default value), json, table
2424
EOF
2525
}
2626

@@ -54,48 +54,53 @@ 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+
dir=$(mktemp -d buildkite-logs-"$now"-XXXX)
5958

6059
echo "Collecting logs for instance $instanceId"
6160

6261
# 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
62+
for name in buildkite-agent system lifecycled docker-daemon; do
63+
aws logs get-log-events \
64+
--log-group-name "/buildkite/$name" \
65+
--log-stream-name "$instanceId" \
66+
--output "$outputFormat" \
67+
>> "$dir/$name-$instanceId.log"
68+
done
6769

6870
# CloudWatch stores the creationTime as milliseconds since epoch UTC.
6971
# 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
72+
# while still being inclusive to all platforms that might run the script
73+
7074
# Currently only collects the last 24h of logs, but it might be good to be able to specify a different value for this.
7175
logAge=$(date -v-1d "+%s000")
7276

7377
# 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
7578
lambdaFunction=$(aws cloudformation describe-stack-resources --stack-name "$stackName" --logical-resource-id Autoscaling --query "StackResources[*].PhysicalResourceId" --output text | sed -nr 's/.*stack\/(.*)\/.*/\1/p')
7679
lambdaLogGroup=$(aws cloudformation describe-stack-resources --stack-name "$lambdaFunction" --logical-resource-id LogGroup --query "StackResources[*].PhysicalResourceId" --output text)
7780
logStreams=$(aws logs describe-log-streams --log-group-name "$lambdaLogGroup" --order-by LastEventTime --query "logStreams[?creationTime > \`$logAge\`] | [*].logStreamName" --output text | tr -s '[:space:]' '\n')
7881

7982
# Iterate through all of the log streams we collected from the lambda log group
80-
8183
echo "Collecting Lambda logs for $lambdaFunction"
8284
for i in $logStreams
8385
do
8486
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
87+
aws logs get-log-events --log-group-name "$lambdaLogGroup" \
88+
--log-stream-name "$i" \
89+
--output "$outputFormat" \
90+
>> "${dir}/lambda-${fileName}.log"
8691
done
8792

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

9398
# Make an archive with the logs
94-
zip -jq buildkite-logs-"$now".zip "$dir"/*.log
99+
tar -czvf buildkite-logs-"$now".tar.gz "$dir"/*.log
95100

96101
# Clean up the unneeded files
97102
rm -r "$dir"
98103

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

101106
exit 0

0 commit comments

Comments
 (0)