From 7495b38c56ffc9b3e5639f4f3cd8f2a2727f171d Mon Sep 17 00:00:00 2001 From: FFMMM Date: Mon, 20 Dec 2021 09:10:13 -0800 Subject: [PATCH] add a metrics test check GH action (#11536) Signed-off-by: FFMMM --- .github/scripts/metrics_checker.sh | 33 +++++++++++++++++++ .github/workflows/pr-metrics-test-checker.yml | 25 ++++++++++++++ 2 files changed, 58 insertions(+) create mode 100755 .github/scripts/metrics_checker.sh create mode 100644 .github/workflows/pr-metrics-test-checker.yml diff --git a/.github/scripts/metrics_checker.sh b/.github/scripts/metrics_checker.sh new file mode 100755 index 000000000..067fdd96a --- /dev/null +++ b/.github/scripts/metrics_checker.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -uo pipefail + +### This script checks if any metric behavior has been modified. +### The checks rely on the git diff against origin/main +### It is still up to the reviewer to make sure that any tests added are needed and meaningful. + +# search for any "new" or modified metric emissions +metrics_modified=$(git --no-pager diff HEAD origin/main | grep -i "SetGauge\|EmitKey\|IncrCounter\|AddSample\|MeasureSince\|UpdateFilter") +# search for PR body or title metric references +metrics_in_pr_body=$(echo "${PR_BODY-""}" | grep -i "metric") +metrics_in_pr_title=$(echo "${PR_TITLE-""}" | grep -i "metric") + +# if there have been code changes to any metric or mention of metrics in the pull request body +if [ "$metrics_modified" ] || [ "$metrics_in_pr_body" ] || [ "$metrics_in_pr_title" ]; then + # need to check if there are modifications to metrics_test + test_files_regex="*_test.go" + modified_metrics_test_files=$(git --no-pager diff HEAD "$(git merge-base HEAD "origin/main")" -- "$test_files_regex" | grep -i "metric") + if [ "$modified_metrics_test_files" ]; then + # 1 happy path: metrics_test has been modified bc we modified metrics behavior + echo "PR seems to modify metrics behavior. It seems it may have added tests to the metrics as well." + exit 0 + else + echo "PR seems to modify metrics behavior. It seems no tests or test behavior has been modified." + echo "Please update the PR with any relevant updated testing or add a pr/no-metrics-test label to skip this check." + exit 1 + fi + +else + # no metrics modified in code, nothing to check + echo "No metric behavior seems to be modified." + exit 0 +fi diff --git a/.github/workflows/pr-metrics-test-checker.yml b/.github/workflows/pr-metrics-test-checker.yml new file mode 100644 index 000000000..3019d357c --- /dev/null +++ b/.github/workflows/pr-metrics-test-checker.yml @@ -0,0 +1,25 @@ +name: "Check for metrics tests" +on: + pull_request: + types: [ opened, synchronize, labeled ] + # Runs on PRs to main + branches: + - main + +jobs: + metrics_test_check: + if: "!contains(github.event.pull_request.labels.*.name, 'pr/no-metrics-test')" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + name: "checkout repo" + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 # by default the checkout action doesn't checkout all branches + - name: "Check for metrics modifications" + run: ./.github/scripts/metrics_checker.sh + # as of now, cannot use github vars in "external" scripts; ref: https://github.community/t/using-github-action-environment-variables-in-shell-script/18330 + env: + PR_TITLE: ${{ github.event.pull_request.title }} + PR_BODY: ${{ github.event.pull_request.body }} + shell: bash