Skip to content

Commit

Permalink
Support specifying additional values either hardcoded or pulling from…
Browse files Browse the repository at this point in the history
… an env var
  • Loading branch information
Ethan Langevin committed May 13, 2020
1 parent 59f90b1 commit 5f6e9e1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ steps:
The host where this agent can reach a running instance of dogstatsd for
reporting metrics to.

### `dogstatsd_port` (Optional, string)

The port where this agent can reach dogstatsd on wherever is specified
in `dogstatsd_host`. This defaults to the default port for dogstatsd
which is `8125`.

### `additional_tags` (Optional, array)

This is an array of additional tags you want to send and where to find
them in the environment variables. Each entry is an object with
properties

* `tag` which is the tag name to send to datadog
* `env_var` which is the environment variable to pull the tag's value from
* `value` which is a static value to send for that tag

## Developing

To run the tests:
Expand Down
35 changes: 34 additions & 1 deletion lib/get-tags.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
#!/bin/bash

function readAdditionalTags() {
local result=()
# Provide a limit to avoid accidental infinite loops
for i in {0..100}; do
tag_var="BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_${i}_TAG"
env_var="BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_${i}_ENV_VAR"
value_var="BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_${i}_VALUE"

if [[ -z ${!tag_var:-} ]]; then
break;
fi

if [ -z ${!env_var:-} ]; then
# If env_var is not set, then a hard-coded value must be
result+=("${!tag_var}:${!value_var}")
else
local env_var_value=${!env_var}
result+=("${!tag_var}:${!env_var_value:-}")
fi
done

echo -n "$(IFS=$','; echo "${result[*]}" | sed 's/,$//')"
}

function getTags() {
BK_COMMAND=${BUILDKITE_COMMAND}
BK_LABEL=${BUILDKITE_LABEL}
Expand All @@ -10,6 +36,13 @@ function getTags() {

PIPELINE_SLUG=${BUILDKITE_PIPELINE_SLUG}
RETRY_COUNT=${BUILDKITE_RETRY_COUNT:-0}
ADDITIONAL_TAGS=$(readAdditionalTags)

RESULT="is_master:${IS_MASTER},pipeline_slug:${PIPELINE_SLUG},step_command:${BK_COMMAND},step_label:${BK_LABEL},retry_count:${RETRY_COUNT}"

if [ -n "ADDITIONAL_TAGS" ]; then
RESULT+=",${ADDITIONAL_TAGS}"
fi

echo is_master:${IS_MASTER},pipeline_slug:${PIPELINE_SLUG},step_command:${BK_COMMAND},step_label:${BK_LABEL},retry_count:${RETRY_COUNT}
echo "${RESULT}"
}
12 changes: 12 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ configuration:
type: string
dogstatsd_port:
type: string
additional_tags:
type: array
minimum: 1
items:
type: object
properties:
tag:
type: string
env_var:
type: string
value:
type: string
required:
- dogstatsd_host
additionalProperties: false
31 changes: 31 additions & 0 deletions tests/post-commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,34 @@ load "$BATS_PATH/load.bash"
unset BUILDKITE_COMMAND
unset BUILDKITE_LABEL
}

@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_BRANCH=some-branch
export BUILDKITE_PIPELINE_SLUG=monorepo
export BUILDKITE_COMMAND="cd somewhere && make do-something"
export BUILDKITE_LABEL=":shipit: deploy-prod"
export BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_0_TAG="my-tag"
export BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_0_ENV_VAR="MY_ENV_VAR"
export MY_ENV_VAR="my-tag-value"
export BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_1_TAG="my-other-tag"
export BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_1_VALUE="my-other-tag-value"

run "$PWD/hooks/post-command"

assert_success
assert_output --partial "Reporting buildkite.steps.duration as 90"
assert_output --partial "tags is_master:false,pipeline_slug:monorepo,step_command:cd somewhere && make do-something,step_label::shipit: deploy-prod,retry_count:0,my-tag:my-tag-value,my-other-tag:my-other-tag-value"

unset BUILDKITE_PLUGIN_DATADOG_STATS_COMMAND_START_TIME_MS
unset BUILDKITE_BRANCH
unset BUILDKITE_PIPELINE_SLUG
unset BUILDKITE_COMMAND
unset BUILDKITE_LABEL
unset BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_0_TAG
unset BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_0_ENV_VAR
unset MY_ENV_VAR
unset BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_0_TAG
unset BUILDKITE_PLUGIN_DATADOG_STATS_ADDITIONAL_TAGS_0_VALUE
}

0 comments on commit 5f6e9e1

Please sign in to comment.