Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(ci): emit test-selection analytics for backend and storybook (#71304)

Georges-Antoine Assi1af84a8f7324
diff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.yml
index 732499e77b95..4205f77c66b9 100644
--- a/.depot/workflows/ci-backend.yml
+++ b/.depot/workflows/ci-backend.yml
@@ -13,7 +13,9 @@
# and setup-sqlx-cli are byte-equivalent mirrors of the canonical composites)
# - permissions: pull-requests:read (no comment/auto-commit side effects)
# - Stripped side effects: artifact uploads, CI report section posters, snapshot
-# auto-commit (handle-snapshots), posthog-github-action telemetry,
+# auto-commit (handle-snapshots), posthog-github-action telemetry (including the
+# capture-test-selection job — select-tests still emits its telemetry outputs to
+# stay apples-to-apples, they just have no consumer here),
# --store-durations write, test-selection verdict, Trunk flaky-test quarantine
# gate (canonical runs per-shard analytics-uploader steps that post to the
# external Trunk service; the upload is best-effort and a local "Fail on test
@@ -552,6 +554,13 @@ jobs:
temporal_files: ${{ steps.classify.outputs.temporal_files }}
run_poe: ${{ steps.classify.outputs.run_poe }}
run_temporal: ${{ steps.classify.outputs.run_temporal }}
+ # Kept apples-to-apples with canonical, though the shadow strips the
+ # capture job that consumes them (posthog-github-action telemetry).
+ narrowed: ${{ steps.classify.outputs.narrowed }}
+ skip_reason: ${{ steps.classify.outputs.skip_reason }}
+ changed_file_count: ${{ steps.classify.outputs.changed_file_count }}
+ selected_test_count: ${{ steps.classify.outputs.selected_test_count }}
+ full_run_reasons_count: ${{ steps.classify.outputs.full_run_reasons_count }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
@@ -609,6 +618,24 @@ jobs:
shell: bash
run: |
set -euo pipefail
+ # Selection telemetry, kept apples-to-apples with canonical (the shadow
+ # strips the capture job that consumes these, per the header).
+ emit_metrics() {
+ local changed="" selected="" reasons=""
+ if [[ -s /tmp/selection.json ]]; then
+ changed=$(jq -r '.changed_file_count // empty' /tmp/selection.json)
+ selected=$(jq -r '.combined.count // empty' /tmp/selection.json)
+ reasons=$(jq -r '.ast.full_run_reasons | length' /tmp/selection.json)
+ fi
+ {
+ echo "narrowed=$1"
+ echo "skip_reason=$2"
+ echo "changed_file_count=$changed"
+ echo "selected_test_count=$selected"
+ echo "full_run_reasons_count=$reasons"
+ } >> "$GITHUB_OUTPUT"
+ }
+
# Untrusted selection on a draft skips the heavy matrices (the
# pre-selection draft behavior); the ready-for-review run is full.
fall_back_to_skip() {
@@ -618,16 +645,17 @@ jobs:
echo "temporal_files=" >> "$GITHUB_OUTPUT"
echo "run_poe=false" >> "$GITHUB_OUTPUT"
echo "run_temporal=false" >> "$GITHUB_OUTPUT"
+ emit_metrics false "$1"
}
if [[ "$FALLBACK_SKIP" == "true" ]]; then
- fall_back_to_skip
+ fall_back_to_skip untrusted
exit 0
fi
if [[ "$SELECT_OUTCOME" != "success" ]] || [[ ! -s /tmp/selection.json ]]; then
echo "::warning::shadow selector did not produce output; draft skips heavy matrices (full run happens on ready for review)"
- fall_back_to_skip
+ fall_back_to_skip selector_error
exit 0
fi
@@ -635,7 +663,7 @@ jobs:
if [[ "$full_run_reasons" -gt 0 ]]; then
echo "Selector requested a full run; draft defers it to ready for review:"
jq -r '.ast.full_run_reasons[]' /tmp/selection.json
- fall_back_to_skip
+ fall_back_to_skip full_run_requested
exit 0
fi
@@ -676,6 +704,7 @@ jobs:
echo "temporal_files=${temporal[*]:-}" >> "$GITHUB_OUTPUT"
echo "run_poe=$([[ ${#poe[@]} -gt 0 ]] && echo true || echo false)" >> "$GITHUB_OUTPUT"
echo "run_temporal=$([[ ${#temporal[@]} -gt 0 ]] && echo true || echo false)" >> "$GITHUB_OUTPUT"
+ emit_metrics true ""
echo "Selected: ${#core[@]} core, ${#poe[@]} POE-eligible, ${#temporal[@]} temporal"
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index 2173cbd8b016..da8f1f57ad22 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -482,6 +482,12 @@ jobs:
temporal_files: ${{ steps.classify.outputs.temporal_files }}
run_poe: ${{ steps.classify.outputs.run_poe }}
run_temporal: ${{ steps.classify.outputs.run_temporal }}
+ # Telemetry for the capture-test-selection job below.
+ narrowed: ${{ steps.classify.outputs.narrowed }}
+ skip_reason: ${{ steps.classify.outputs.skip_reason }}
+ changed_file_count: ${{ steps.classify.outputs.changed_file_count }}
+ selected_test_count: ${{ steps.classify.outputs.selected_test_count }}
+ full_run_reasons_count: ${{ steps.classify.outputs.full_run_reasons_count }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
@@ -539,6 +545,24 @@ jobs:
shell: bash
run: |
set -euo pipefail
+ # Selection telemetry (see the capture-test-selection job). Best-effort
+ # from the selector JSON when it exists; narrowed=false marks a fallback.
+ emit_metrics() {
+ local changed="" selected="" reasons=""
+ if [[ -s /tmp/selection.json ]]; then
+ changed=$(jq -r '.changed_file_count // empty' /tmp/selection.json)
+ selected=$(jq -r '.combined.count // empty' /tmp/selection.json)
+ reasons=$(jq -r '.ast.full_run_reasons | length' /tmp/selection.json)
+ fi
+ {
+ echo "narrowed=$1"
+ echo "skip_reason=$2"
+ echo "changed_file_count=$changed"
+ echo "selected_test_count=$selected"
+ echo "full_run_reasons_count=$reasons"
+ } >> "$GITHUB_OUTPUT"
+ }
+
# Untrusted selection on a draft skips the heavy matrices (the
# pre-selection draft behavior); the ready-for-review run is full.
fall_back_to_skip() {
@@ -548,16 +572,17 @@ jobs:
echo "temporal_files=" >> "$GITHUB_OUTPUT"
echo "run_poe=false" >> "$GITHUB_OUTPUT"
echo "run_temporal=false" >> "$GITHUB_OUTPUT"
+ emit_metrics false "$1"
}
if [[ "$FALLBACK_SKIP" == "true" ]]; then
- fall_back_to_skip
+ fall_back_to_skip untrusted
exit 0
fi
if [[ "$SELECT_OUTCOME" != "success" ]] || [[ ! -s /tmp/selection.json ]]; then
echo "::warning::shadow selector did not produce output; draft skips heavy matrices (full run happens on ready for review)"
- fall_back_to_skip
+ fall_back_to_skip selector_error
exit 0
fi
@@ -565,7 +590,7 @@ jobs:
if [[ "$full_run_reasons" -gt 0 ]]; then
echo "Selector requested a full run; draft defers it to ready for review:"
jq -r '.ast.full_run_reasons[]' /tmp/selection.json
- fall_back_to_skip
+ fall_back_to_skip full_run_requested
exit 0
fi
@@ -606,6 +631,7 @@ jobs:
echo "temporal_files=${temporal[*]:-}" >> "$GITHUB_OUTPUT"
echo "run_poe=$([[ ${#poe[@]} -gt 0 ]] && echo true || echo false)" >> "$GITHUB_OUTPUT"
echo "run_temporal=$([[ ${#temporal[@]} -gt 0 ]] && echo true || echo false)" >> "$GITHUB_OUTPUT"
+ emit_metrics true ""
echo "Selected: ${#core[@]} core, ${#poe[@]} POE-eligible, ${#temporal[@]} temporal"
@@ -3238,6 +3264,43 @@ jobs:
status-job: 'Django Tests Pass'
runner: 'depot'
+ capture-test-selection:
+ name: Capture test selection
+ needs: [select-tests]
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ # select-tests only runs on internal draft PRs; fire only when it actually
+ # produced a decision. DevEx project only, mirroring monitor-github-rate-limit;
+ # continue-on-error so telemetry never reds CI. Skipped on forks/Dependabot,
+ # which never have the secret (and where select-tests didn't run anyway).
+ if: |
+ always() && github.run_attempt == '1'
+ && github.actor != 'dependabot[bot]'
+ && github.event.pull_request.head.repo.full_name == 'PostHog/posthog'
+ && needs.select-tests.result == 'success'
+ steps:
+ - name: Capture test selection to DevEx PostHog
+ continue-on-error: true
+ uses: PostHog/posthog-github-action@58dea254b598fb5d469c0699c98af8288a7f7650 # v1.2.0
+ with:
+ posthog-token: ${{ secrets.POSTHOG_DEVEX_PROJECT_API_TOKEN }}
+ event: 'posthog-ci-test-selection'
+ properties: |
+ {
+ "suite": "backend",
+ "mode": ${{ toJSON(needs.select-tests.outputs.mode) }},
+ "narrowed": ${{ needs.select-tests.outputs.narrowed || 'false' }},
+ "skip_reason": ${{ toJSON(needs.select-tests.outputs.skip_reason || '') }},
+ "changed_file_count": ${{ needs.select-tests.outputs.changed_file_count || 'null' }},
+ "selected_test_count": ${{ needs.select-tests.outputs.selected_test_count || 'null' }},
+ "full_run_reasons_count": ${{ needs.select-tests.outputs.full_run_reasons_count || 'null' }},
+ "event_type": ${{ toJSON(github.event_name) }},
+ "branch": ${{ toJSON(github.head_ref || github.ref_name) }},
+ "sha": ${{ toJSON(github.sha) }},
+ "pr_number": ${{ github.event.pull_request.number || 'null' }},
+ "run_id": ${{ toJSON(github.run_id) }}
+ }
+
report-test-timings:
name: Report per-test traces
needs: [django_tests]
diff --git a/.github/workflows/ci-storybook.yml b/.github/workflows/ci-storybook.yml
index e6abde87cdcb..8986a01382de 100644
--- a/.github/workflows/ci-storybook.yml
+++ b/.github/workflows/ci-storybook.yml
@@ -734,6 +734,42 @@ jobs:
github-token: ${{ steps.telemetry-app-token.outputs.token }}
status-job: 'Visual regression tests pass'
+ capture-story-selection:
+ name: Capture story selection
+ needs: [select-stories]
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ # select-stories only runs on internal draft PRs; fire only when it produced a
+ # decision. DevEx project only; continue-on-error so telemetry never reds CI.
+ if: |
+ always() && github.run_attempt == '1'
+ && github.actor != 'dependabot[bot]'
+ && github.event.pull_request.head.repo.full_name == 'PostHog/posthog'
+ && needs.select-stories.result == 'success'
+ steps:
+ - name: Capture story selection to DevEx PostHog
+ continue-on-error: true
+ uses: PostHog/posthog-github-action@58dea254b598fb5d469c0699c98af8288a7f7650 # v1.2.0
+ with:
+ posthog-token: ${{ secrets.POSTHOG_DEVEX_PROJECT_API_TOKEN }}
+ event: 'posthog-ci-test-selection'
+ properties: |
+ {
+ "suite": "storybook",
+ "mode": ${{ toJSON(needs.select-stories.outputs.mode) }},
+ "narrowed": ${{ needs.select-stories.outputs.narrowed || 'false' }},
+ "skip_reason": ${{ toJSON(needs.select-stories.outputs.skip_reason || '') }},
+ "total_story_count": ${{ needs.select-stories.outputs.total_story_count || 'null' }},
+ "affected_story_count": ${{ needs.select-stories.outputs.affected_story_count || 'null' }},
+ "suggested_shards": ${{ needs.select-stories.outputs.suggested_shards || 'null' }},
+ "estimated_minutes_saved": ${{ needs.select-stories.outputs.estimated_minutes_saved || 'null' }},
+ "event_type": ${{ toJSON(github.event_name) }},
+ "branch": ${{ toJSON(github.head_ref || github.ref_name) }},
+ "sha": ${{ toJSON(github.sha) }},
+ "pr_number": ${{ github.event.pull_request.number || 'null' }},
+ "run_id": ${{ toJSON(github.run_id) }}
+ }
+
# Pick a narrowed visual-regression matrix for draft PRs based on which stories
# the Vite module graph says are affected by the diff. Ready PRs and drafts with
# the run-ci-frontend label skip this job and use the full chromium matrix.
@@ -754,6 +790,13 @@ jobs:
matrix: ${{ steps.classify.outputs.matrix }}
should_run: ${{ steps.classify.outputs.should_run }}
affected_files_json: ${{ steps.classify.outputs.affected_files_json }}
+ # Telemetry for the capture-story-selection job below.
+ narrowed: ${{ steps.classify.outputs.narrowed }}
+ skip_reason: ${{ steps.classify.outputs.skip_reason }}
+ total_story_count: ${{ steps.classify.outputs.total_story_count }}
+ affected_story_count: ${{ steps.classify.outputs.affected_story_count }}
+ suggested_shards: ${{ steps.classify.outputs.suggested_shards }}
+ estimated_minutes_saved: ${{ steps.classify.outputs.estimated_minutes_saved }}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
@@ -804,23 +847,43 @@ jobs:
run: |
set -euo pipefail
+ # Selection telemetry (see the capture-story-selection job). Best-effort
+ # story counts from the selector JSON; narrowed=false marks a skip/fallback.
+ emit_metrics() {
+ local total="" affected="" shards=""
+ if [[ -s /tmp/selection-result.json ]]; then
+ total=$(jq -r '.total_story_count // empty' /tmp/selection-result.json)
+ affected=$(jq -r '.affected_story_count // empty' /tmp/selection-result.json)
+ shards=$(jq -r '(([.suggested_shards, 16] | min) // empty)' /tmp/selection-result.json)
+ fi
+ {
+ echo "narrowed=$1"
+ echo "skip_reason=$2"
+ echo "total_story_count=$total"
+ echo "affected_story_count=$affected"
+ echo "suggested_shards=$shards"
+ echo "estimated_minutes_saved=${3:-}"
+ } >> "$GITHUB_OUTPUT"
+ }
+
fall_back_to_skip() {
echo "mode=skip" >> "$GITHUB_OUTPUT"
echo 'matrix={"include":[]}' >> "$GITHUB_OUTPUT"
echo "should_run=false" >> "$GITHUB_OUTPUT"
echo "affected_files_json=[]" >> "$GITHUB_OUTPUT"
+ emit_metrics false "$1"
}
if [[ "$SELECT_OUTCOME" != "success" ]] || [[ ! -s /tmp/selection-result.json ]]; then
echo "::warning::story selector did not produce output; skipping draft visual regression"
- fall_back_to_skip
+ fall_back_to_skip selector_error
exit 0
fi
MODE=$(jq -r '.mode' /tmp/selection-result.json)
if [[ "$MODE" != "selective" ]]; then
echo "Selector declined to narrow (mode=$MODE); skipping draft visual regression"
- fall_back_to_skip
+ fall_back_to_skip "$(jq -r '.reason // "full_requested"' /tmp/selection-result.json)"
exit 0
fi
@@ -830,6 +893,7 @@ jobs:
echo 'matrix={"include":[]}' >> "$GITHUB_OUTPUT"
echo "should_run=false" >> "$GITHUB_OUTPUT"
echo "affected_files_json=[]" >> "$GITHUB_OUTPUT"
+ emit_metrics true no_affected_stories
echo "Selective mode: 0 affected stories, skipping draft visual regression"
exit 0
fi
@@ -843,6 +907,14 @@ jobs:
echo "matrix=$MATRIX" >> "$GITHUB_OUTPUT"
echo "should_run=true" >> "$GITHUB_OUTPUT"
echo "affected_files_json=$AFFECTED_FILES_JSON" >> "$GITHUB_OUTPUT"
+
+ # Est. minutes saved vs the full 16-shard run — same formula as the
+ # "Write summary" step (CURRENT_SHARDS=16, SHARD_OVERHEAD_MINUTES=3).
+ AFFECTED_DUR=$(jq -r '.affected_duration_seconds' /tmp/selection-result.json)
+ TOTAL_DUR=$(jq -r '.total_duration_seconds' /tmp/selection-result.json)
+ FULL_MINUTES=$(( (16 * 3) + (TOTAL_DUR / 60) ))
+ SELECTIVE_MINUTES=$(( (SHARDS * 3) + (AFFECTED_DUR / 60) ))
+ emit_metrics true "" "$(( FULL_MINUTES - SELECTIVE_MINUTES ))"
echo "Selective mode: $COUNT affected story files across $SHARDS shard(s)"
- name: Write summary