Skip to content

Commit

Permalink
Fill in remaining hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Langevin committed May 14, 2020
1 parent 7e1f00d commit 1597200
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 7 deletions.
11 changes: 11 additions & 0 deletions hooks/environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -euo pipefail

echo "--- :datadog: Recording step start time"

BUILDKITE_PLUGIN_DATADOG_STATS_STEP_START_TIME_MS=$(date +%s%3N)

export BUILDKITE_PLUGIN_DATADOG_STATS_STEP_START_TIME_MS

echo "Step started at ${BUILDKITE_PLUGIN_DATADOG_STATS_STEP_START_TIME_MS}"
4 changes: 2 additions & 2 deletions hooks/post-checkout
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ basedir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"

NOW=$(date +%s%3N)

echo "--- :datadog: Recording command run time"
echo "--- :datadog: Recording checkout run time"

METRIC_NAME=$(getMetricName "checkout.duration")
METRIC_VALUE=$(timeDiff "$BUILDKITE_PLUGIN_DATADOG_STATS_COMMAND_START_TIME_MS")
METRIC_VALUE=$(timeDiff "$BUILDKITE_PLUGIN_DATADOG_STATS_CHECKOUT_START_TIME_MS")
TAGS=$(getTags)

reportToDatadog "${METRIC_NAME}" "${METRIC_VALUE}" d "${TAGS}"
2 changes: 1 addition & 1 deletion hooks/pre-checkout
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ BUILDKITE_PLUGIN_DATADOG_STATS_CHECKOUT_START_TIME_MS=$(date +%s%3N)

export BUILDKITE_PLUGIN_DATADOG_STATS_CHECKOUT_START_TIME_MS

echo "Command started at ${BUILDKITE_PLUGIN_DATADOG_STATS_CHECKOUT_START_TIME_MS}"
echo "Checkout started at ${BUILDKITE_PLUGIN_DATADOG_STATS_CHECKOUT_START_TIME_MS}"
24 changes: 24 additions & 0 deletions hooks/pre-exit
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -euo pipefail

basedir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"

# shellcheck source=lib/get-tags.sh
. "$basedir/lib/get-tags.sh"
# shellcheck source=lib/report-to-datadog.sh
. "$basedir/lib/report-to-datadog.sh"
# shellcheck source=lib/time-diff.sh
. "$basedir/lib/time-diff.sh"
# shellcheck source=lib/get-metric-name.sh
. "$basedir/lib/get-metric-name.sh"

NOW=$(date +%s%3N)

echo "--- :datadog: Recording step run time"

METRIC_NAME=$(getMetricName "step.duration")
METRIC_VALUE=$(timeDiff "$BUILDKITE_PLUGIN_DATADOG_STATS_STEP_START_TIME_MS")
TAGS=$(getTags)

reportToDatadog "${METRIC_NAME}" "${METRIC_VALUE}" d "${TAGS}"
10 changes: 10 additions & 0 deletions tests/environment.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bats

load "$BATS_PATH/load.bash"

@test "Records the start time of the step" {
run "$PWD/hooks/environment"

assert_success
assert_output --partial "Step started at $(date +%s)"
}
24 changes: 24 additions & 0 deletions tests/post-checkout.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bats

load "$BATS_PATH/load.bash"

@test "Sets the correct tags and runtime for the master branch" {
NOW=$(date +%s%3N)
export BUILDKITE_PLUGIN_DATADOG_STATS_CHECKOUT_START_TIME_MS=$((NOW-900))
export BUILDKITE_BRANCH=master
export BUILDKITE_PIPELINE_SLUG=monorepo
export BUILDKITE_COMMAND="cd somewhere && make do-something"
export BUILDKITE_LABEL=":shipit: deploy-prod"

run "$PWD/hooks/post-checkout"

assert_success
assert_output --partial "Reporting buildkite.steps.checkout.duration with value=90"
assert_output --partial "tags=is_master:true,pipeline_slug:monorepo,step_command:cd somewhere && make do-something,step_label::shipit: deploy-prod,retry_count:0"

unset BUILDKITE_PLUGIN_DATADOG_STATS_COMMAND_START_TIME_MS
unset BUILDKITE_BRANCH
unset BUILDKITE_PIPELINE_SLUG
unset BUILDKITE_COMMAND
unset BUILDKITE_LABEL
}
6 changes: 3 additions & 3 deletions tests/post-commands.bats → tests/post-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load "$BATS_PATH/load.bash"

@test "Sets the correct tags and runtime for the master branch" {
NOW=$(date +%s%3N)
export BUILDKITE_PLUGIN_DATADOG_STATS_COMMAND_START_TIME_MS=$(($NOW-900))
export BUILDKITE_PLUGIN_DATADOG_STATS_COMMAND_START_TIME_MS=$((NOW-900))
export BUILDKITE_BRANCH=master
export BUILDKITE_PIPELINE_SLUG=monorepo
export BUILDKITE_COMMAND="cd somewhere && make do-something"
Expand All @@ -25,7 +25,7 @@ load "$BATS_PATH/load.bash"

@test "Sets the correct tags and runtime for a non-master branch" {
NOW=$(date +%s%3N)
export BUILDKITE_PLUGIN_DATADOG_STATS_COMMAND_START_TIME_MS=$(($NOW-900))
export BUILDKITE_PLUGIN_DATADOG_STATS_COMMAND_START_TIME_MS=$((NOW-900))
export BUILDKITE_BRANCH=some-branch
export BUILDKITE_PIPELINE_SLUG=monorepo
export BUILDKITE_COMMAND="cd somewhere && make do-something"
Expand All @@ -46,7 +46,7 @@ load "$BATS_PATH/load.bash"

@test "It supports specifying additional tags by value and env var" {
NOW=$(date +%s%3N)
export BUILDKITE_PLUGIN_DATADOG_STATS_COMMAND_START_TIME_MS=$(($NOW-900))
export BUILDKITE_PLUGIN_DATADOG_STATS_COMMAND_START_TIME_MS=$((NOW-900))
export BUILDKITE_BRANCH=some-branch
export BUILDKITE_PIPELINE_SLUG=monorepo
export BUILDKITE_COMMAND="cd somewhere && make do-something"
Expand Down
10 changes: 10 additions & 0 deletions tests/pre-checkout.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bats

load "$BATS_PATH/load.bash"

@test "Records the start time of the checkout" {
run "$PWD/hooks/pre-checkout"

assert_success
assert_output --partial "Checkout started at $(date +%s)"
}
1 change: 0 additions & 1 deletion tests/pre-commands.bats → tests/pre-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ load "$BATS_PATH/load.bash"
run "$PWD/hooks/pre-command"

assert_success
# Check that we produced the right output at least to the second
assert_output --partial "Command started at $(date +%s)"
}
24 changes: 24 additions & 0 deletions tests/pre-exit.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bats

load "$BATS_PATH/load.bash"

@test "Sets the correct tags and runtime for the master branch" {
NOW=$(date +%s%3N)
export BUILDKITE_PLUGIN_DATADOG_STATS_STEP_START_TIME_MS=$((NOW-900))
export BUILDKITE_BRANCH=master
export BUILDKITE_PIPELINE_SLUG=monorepo
export BUILDKITE_COMMAND="cd somewhere && make do-something"
export BUILDKITE_LABEL=":shipit: deploy-prod"

run "$PWD/hooks/pre-exit"

assert_success
assert_output --partial "Reporting buildkite.steps.step.duration with value=90"
assert_output --partial "tags=is_master:true,pipeline_slug:monorepo,step_command:cd somewhere && make do-something,step_label::shipit: deploy-prod,retry_count:0"

unset BUILDKITE_PLUGIN_DATADOG_STATS_COMMAND_START_TIME_MS
unset BUILDKITE_BRANCH
unset BUILDKITE_PIPELINE_SLUG
unset BUILDKITE_COMMAND
unset BUILDKITE_LABEL
}

0 comments on commit 1597200

Please sign in to comment.