|
| 1 | +#!/bin/bash |
| 2 | +# Reports a single AgentControlBinarySize custom event to the New Relic Events API, |
| 3 | +# and appends a warning line to WARNINGS_FILE if the binary grew more than 10% versus |
| 4 | +# its previous build. Sending that warning to Slack is not this script's job: the |
| 5 | +# workflow step collects WARNINGS_FILE across all binaries and a separate job hands |
| 6 | +# it to the existing component_send_warning_via_slack.yml, instead of duplicating that |
| 7 | +# webhook call here. |
| 8 | +# |
| 9 | +# Required environment variables (set via the workflow step's `env:` block): |
| 10 | +# BINARY_NAME - e.g. newrelic-agent-control |
| 11 | +# BINARY_TARGET - Rust target triple, e.g. x86_64-unknown-linux-musl |
| 12 | +# BINARY_PATH - path to the built binary file |
| 13 | +# BINARY_VERSION - version/tag being built |
| 14 | +# NR_ACCOUNT_ID - New Relic account ID |
| 15 | +# NR_LICENSE_KEY - New Relic license key (used as the Events API ingest key) |
| 16 | +# |
| 17 | +# Optional environment variables: |
| 18 | +# NR_USER_API_KEY - New Relic User API key (NerdGraph). Needed to look up the previous |
| 19 | +# build's size and compute growth. If unset, the event is still |
| 20 | +# reported but the growth check is skipped. |
| 21 | +# WARNINGS_FILE - Path to append growth warnings to. Defaults to binary-size-warnings.txt |
| 22 | +# in the current directory. |
| 23 | +# |
| 24 | +# Standard GitHub Actions variables (automatically injected by the runner): |
| 25 | +# GITHUB_EVENT_NAME, GITHUB_HEAD_REF, GITHUB_REF_NAME, GITHUB_RUN_ID |
| 26 | +# |
| 27 | +# If NR_ACCOUNT_ID/NR_LICENSE_KEY are not set, the report is skipped without failing the build |
| 28 | +# (some callers of component_packages.yml, e.g. e2e-only workflows, don't wire these secrets). |
| 29 | + |
| 30 | +set -euo pipefail |
| 31 | + |
| 32 | +GROWTH_THRESHOLD_PCT=10 |
| 33 | + |
| 34 | +if [[ -z "${NR_ACCOUNT_ID:-}" || -z "${NR_LICENSE_KEY:-}" ]]; then |
| 35 | + echo "NR_ACCOUNT_ID/NR_LICENSE_KEY not set, skipping binary size report for ${BINARY_PATH:-unknown}" |
| 36 | + exit 0 |
| 37 | +fi |
| 38 | + |
| 39 | +SIZE_BYTES=$(stat -c%s "$BINARY_PATH") |
| 40 | + |
| 41 | +# GITHUB_HEAD_REF is set for pull requests; GITHUB_REF_NAME covers push/schedule/dispatch. |
| 42 | +BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME:-}}" |
| 43 | + |
| 44 | +# Look up the previous build's size for this exact binary+target via NerdGraph, before |
| 45 | +# reporting the current one (so we compare against the true previous value, not a race |
| 46 | +# against data we're about to insert ourselves). |
| 47 | +PREVIOUS_SIZE_BYTES="" |
| 48 | +if [[ -n "${NR_USER_API_KEY:-}" ]]; then |
| 49 | + nrql_query="SELECT sizeBytes FROM AgentControlBinarySize WHERE binaryName = '${BINARY_NAME}' AND target = '${BINARY_TARGET}' SINCE 90 days ago ORDER BY timestamp DESC LIMIT 1" |
| 50 | + graphql_query="{ actor { account(id: ${NR_ACCOUNT_ID}) { nrql(query: \"${nrql_query}\") { results } } } }" |
| 51 | + request_body=$(jq -n --arg query "$graphql_query" '{query: $query}') |
| 52 | + |
| 53 | + nerdgraph_response=$(curl -s -X POST "https://api.newrelic.com/graphql" \ |
| 54 | + -H "Content-Type: application/json" \ |
| 55 | + -H "API-Key: ${NR_USER_API_KEY}" \ |
| 56 | + -d "$request_body") |
| 57 | + |
| 58 | + PREVIOUS_SIZE_BYTES=$(echo "$nerdgraph_response" | jq -r '.data.actor.account.nrql.results[0].sizeBytes // empty') |
| 59 | +fi |
| 60 | + |
| 61 | +event=$(jq -n \ |
| 62 | + --arg binaryName "$BINARY_NAME" \ |
| 63 | + --arg target "$BINARY_TARGET" \ |
| 64 | + --arg version "$BINARY_VERSION" \ |
| 65 | + --arg branch "$BRANCH" \ |
| 66 | + --arg runId "$GITHUB_RUN_ID" \ |
| 67 | + --arg triggerEvent "$GITHUB_EVENT_NAME" \ |
| 68 | + --argjson sizeBytes "$SIZE_BYTES" \ |
| 69 | + '[{ |
| 70 | + eventType: "AgentControlBinarySize", |
| 71 | + binaryName: $binaryName, |
| 72 | + target: $target, |
| 73 | + version: $version, |
| 74 | + branch: $branch, |
| 75 | + runId: $runId, |
| 76 | + triggerEvent: $triggerEvent, |
| 77 | + sizeBytes: $sizeBytes |
| 78 | + }]') |
| 79 | + |
| 80 | +curl -s -X POST \ |
| 81 | + "https://insights-collector.newrelic.com/v1/accounts/${NR_ACCOUNT_ID}/events" \ |
| 82 | + -H "Content-Type: application/json" \ |
| 83 | + -H "Api-Key: ${NR_LICENSE_KEY}" \ |
| 84 | + -d "$event" |
| 85 | + |
| 86 | +if [[ -n "$PREVIOUS_SIZE_BYTES" ]]; then |
| 87 | + growth_pct=$(awk -v cur="$SIZE_BYTES" -v prev="$PREVIOUS_SIZE_BYTES" \ |
| 88 | + 'BEGIN { printf "%.2f", (prev == 0) ? 0 : ((cur - prev) * 100.0 / prev) }') |
| 89 | + |
| 90 | + if awk -v p="$growth_pct" -v t="$GROWTH_THRESHOLD_PCT" 'BEGIN { exit !(p > t) }'; then |
| 91 | + warning="Agent Control binary \`${BINARY_NAME}\` (${BINARY_TARGET}) grew ${growth_pct}% versus its previous build: ${PREVIOUS_SIZE_BYTES} -> ${SIZE_BYTES} bytes (version ${BINARY_VERSION})" |
| 92 | + echo "Binary size regression: $warning" |
| 93 | + echo "$warning" >> "${WARNINGS_FILE:-binary-size-warnings.txt}" |
| 94 | + fi |
| 95 | +else |
| 96 | + echo "No previous build found for ${BINARY_NAME} (${BINARY_TARGET}), skipping growth check" |
| 97 | +fi |
0 commit comments