Skip to content

Commit

Permalink
use go for ecs service logs
Browse files Browse the repository at this point in the history
  • Loading branch information
pjdufour-truss committed Feb 13, 2019
1 parent 6a471be commit 621a945
Show file tree
Hide file tree
Showing 7 changed files with 599 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -38,6 +38,7 @@ __snapshots__

/bin/callgraph
/bin/chamber
/bin/ecs-service-logs
/bin/soda
/bin/golint
/bin/swagger
Expand Down
111 changes: 110 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Expand Up @@ -31,3 +31,7 @@ required = [
[[constraint]]
name = "github.com/stretchr/objx"
version = "0.1.1"

[[constraint]]
name = "github.com/99designs/aws-vault"
branch = "master"
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -170,6 +170,7 @@ build_tools: server_deps server_generate
go build -i -ldflags "$(LDFLAGS)" -o bin/make-tsp-user ./cmd/make_tsp_user
go build -i -ldflags "$(LDFLAGS)" -o bin/paperwork ./cmd/paperwork
go build -i -ldflags "$(LDFLAGS)" -o bin/tsp-award-queue ./cmd/tsp_award_queue
go build -i -ldflags "$(LDFLAGS)" -o bin/ecs-service-logs ./cmd/ecs-service-logs

tsp_run: build_tools db_dev_run
./bin/tsp-award-queue
Expand Down
27 changes: 4 additions & 23 deletions bin/ecs-show-service-logs
Expand Up @@ -3,6 +3,8 @@
# Show logs from the containers running for the named service.
#
set -eo pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
readonly DIR

usage() {
echo "$0 <name> <environment> | less"
Expand All @@ -11,27 +13,6 @@ usage() {
[[ -z $1 || -z $2 ]] && usage
set -u

readonly prefix="app"
readonly name=$1
readonly environment=$2
readonly cluster=$prefix-$environment
[[ -f "$DIR/ecs-service-logs" ]] || (echo "Missing bin/ecs-service-logs. Run make build_tools" && exit 1)

# awslogs-stream-prefix settings
readonly container=$name-$environment
readonly log_group_name=ecs-tasks-$name-$environment

# Get list of running tasks
for task_arn in $(aws ecs list-tasks --cluster "$cluster" --service-name "$name" --query 'taskArns' | jq -r '.[]'); do
[[ -z $task_arn ]] && { echo "Missing task ARN"; exit 1; }

# Parse out the task ID
task_id=$(echo "$task_arn" | perl -ne 'm|^arn:aws:ecs:([^:]+:){2}task/([\S]+)|; print "$2\n";')
[[ -z $task_id ]] && { echo "Couldn't parse task ID: $task_arn"; exit 1; }

# Display logs for this task
log_stream_name=$prefix/$container/$task_id
echo "Task $task_id"
echo "-----------------------------------------"
aws logs get-log-events --log-group-name "$log_group_name" --log-stream-name "$log_stream_name" --query 'events[].message' | jq -r '.[]' || true
echo
done
"$DIR/ecs-service-logs" --cluster "app-${2}" --service "${1}" --status "RUNNING" --verbose
23 changes: 4 additions & 19 deletions bin/ecs-show-service-stopped-logs
Expand Up @@ -3,6 +3,8 @@
# Show logs from the most recently stopped app tasks.
#
set -eo pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
readonly DIR
readonly LIMIT=${LIMIT:-25}

usage() {
Expand All @@ -12,23 +14,6 @@ usage() {
[[ -z $1 || -z $2 ]] && usage
set -u

readonly name=$1
readonly environment=$2
readonly cluster=app-$environment
[[ -f "$DIR/ecs-service-logs" ]] || (echo "Missing bin/ecs-service-logs. Run make build_tools" && exit 1)

# awslogs-stream-prefix settings
readonly prefix=$name
readonly container=$name-$environment
readonly log_group_name=ecs-tasks-$name-$environment

# Get list of recently stopped tasks
for task_id in $(aws ecs describe-services --cluster "$cluster" --services "$name" --query 'services[].events[].message' | grep stopped | grep -o 'task [0-9a-f-]*' | cut -f 2 -d ' '); do
[[ -z $task_id ]] && { echo "Missing task ID"; exit 1; }

# Display logs for this task
log_stream_name=$prefix/$container/$task_id
echo "Task $task_id"
echo "-----------------------------------------"
aws logs get-log-events --limit "$LIMIT" --log-group-name "$log_group_name" --log-stream-name "$log_stream_name" --query 'events[].message' | jq -r '.[]' || true
echo
done
"$DIR/ecs-service-logs" --cluster "app-${2}" --service "${1}" --environment "${2}" --status "STOPPED" --verbose

0 comments on commit 621a945

Please sign in to comment.