diff --git a/.github/actions/emit-metrics/action.yml b/.github/actions/emit-metrics/action.yml new file mode 100644 index 0000000..4e54ff4 --- /dev/null +++ b/.github/actions/emit-metrics/action.yml @@ -0,0 +1,77 @@ +name: Emit Metrics +description: Emit metrics using the kat tool +inputs: + namespace: + description: The CloudWatch namespace in which to emit metrics + required: true + dimensions: + description: |- + The dimensions to include with emitted metrics, as a collection of `name=value` pairs. Multiple dimensions are + delimited by newlines (`\n`) and whitespace is trimmed from before/after each `name=value` pair. When passing + multiple dimensions, make sure to use YAML's block literal syntax (`|` or `|-`). + + For example: + ``` + dimensions: | + Artifact=foo-artifact + Platform=Kotlin/JVM + Variant=External + ``` + required: true + metrics: + description: |- + The metrics to emit into the given namespace and using the specified dimensions. Individual metrics are written in + the form of `:[:]` where: + * is the metric name and may include spaces but not colons (`:`) + * is the numeric value of the metric + * is the CloudWatch unit name (if omitted, `None` is assumed) + + Multiple metrics are delimited by newlines (`\n`) and whitespace is trimmed from before/after each metric + specification. When passing multiple metrics, make sure to use YAML's block literal syntax (`|` or `|-`). + + For example: + ``` + metrics: | + BuildTime:4.532:Seconds + BuildSucceeded:1:Count + ``` + required: true + +runs: + using: composite + steps: + - name: Verify kat exists + shell: bash + run: which kat || ( echo "Cannot find kat installation. Did you forget to run setup-kat first?" && exit 1 ) + - name: Emit Metrics + shell: bash + run: | + # Trim a string and echo the result + trim() { + # args: + # $1 the string to trim + + echo -n "$1" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' + } + + # Convert a newline-delimited string into a single-line string delimited by spaces with each element prefixed + format() { + # args: + # $1 the prefix for each element + # $2 the newline-delimited string + + PROCESSED="" + IFS=$'\n'; ARR=($2); unset IFS + for ITEM in "${ARR[@]}"; do + PROCESSED="$PROCESSED $1 \"$(trim "$ITEM")\"" + done + echo -n "$(trim "$PROCESSED")" + } + + NAMESPACE="--namespace \"${{ inputs.namespace }}\"" + DIMENSIONS="$(format "--dimension" "${{ inputs.dimensions }}")" + METRICS="$(format "--metric" "${{ inputs.metrics }}")" + CMD="kat metrics emit $NAMESPACE $DIMENSIONS $METRICS" + + echo "Executing command \"$CMD\"" + eval "$CMD"