diff --git a/README.md b/README.md index bf54164..c32bcae 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/lib/get-tags.sh b/lib/get-tags.sh index f0df5ae..dc638aa 100644 --- a/lib/get-tags.sh +++ b/lib/get-tags.sh @@ -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} @@ -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}" } diff --git a/plugin.yml b/plugin.yml index 52cdd7a..314d83b 100644 --- a/plugin.yml +++ b/plugin.yml @@ -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 diff --git a/tests/post-commands.bats b/tests/post-commands.bats index 0afb7bc..99f8f42 100644 --- a/tests/post-commands.bats +++ b/tests/post-commands.bats @@ -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 +}