Chore: Publish frontend metrics from github actions (#110271)
* remove build size from ci scripts, test adding a github action step * generate pa11y results file * setup node * don't need grabpl * get key from vault * doublequote env var * write node script for publishing * update CODEOWNERS * add some logging * yarn install... * tidy up * only on main branch
This commit is contained in:
@@ -1234,6 +1234,7 @@ embed.go @grafana/grafana-as-code
|
||||
/.github/workflows/i18n-verify.yml @grafana/grafana-frontend-platform
|
||||
/.github/workflows/deploy-storybook-preview.yml @grafana/grafana-frontend-platform
|
||||
/.github/workflows/scripts/crowdin/create-tasks.ts @grafana/grafana-frontend-platform
|
||||
/.github/workflows/scripts/publish-frontend-metrics.mts @grafana/grafana-frontend-platform
|
||||
/.github/workflows/pr-go-workspace-check.yml @grafana/grafana-app-platform-squad
|
||||
/.github/workflows/pr-dependabot-update-go-workspace.yml @grafana/grafana-app-platform-squad
|
||||
/.github/workflows/pr-k8s-codegen-check.yml @grafana/grafana-app-platform-squad
|
||||
|
||||
@@ -499,7 +499,49 @@ jobs:
|
||||
uses: dagger/dagger-for-github@e47aba410ef9bb9ed81a4d2a97df31061e5e842e
|
||||
with:
|
||||
verb: run
|
||||
args: go run ./pkg/build/a11y --package=grafana.tar.gz --no-threshold-fail
|
||||
args: go run ./pkg/build/a11y --package=grafana.tar.gz --no-threshold-fail --results=./pa11y-ci-results.json
|
||||
- name: Upload pa11y results
|
||||
if: github.event_name != 'pull_request'
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
retention-days: 1
|
||||
name: pa11y-ci-results
|
||||
path: pa11y-ci-results.json
|
||||
|
||||
publish-metrics:
|
||||
needs:
|
||||
- run-a11y-test
|
||||
name: Publish metrics
|
||||
# Run on `grafana/grafana` main branch only
|
||||
if: github.event_name == 'push' && github.repository == 'grafana/grafana' && github.ref_name == 'main'
|
||||
permissions:
|
||||
contents: read
|
||||
id-token: write
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- id: vault-secrets
|
||||
uses: grafana/shared-workflows/actions/get-vault-secrets@main
|
||||
with:
|
||||
repo_secrets: |
|
||||
GRAFANA_MISC_STATS_API_KEY=grafana-misc-stats:api_key
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version-file: '.nvmrc'
|
||||
- name: Install dependencies
|
||||
run: yarn install --immutable
|
||||
- name: Get pa11y results
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: pa11y-ci-results
|
||||
- name: Extract and publish metrics
|
||||
run: ./scripts/ci-frontend-metrics.sh | node --experimental-strip-types .github/workflows/scripts/publish-frontend-metrics.mts
|
||||
env:
|
||||
GRAFANA_MISC_STATS_API_KEY: ${{ env.GRAFANA_MISC_STATS_API_KEY}}
|
||||
|
||||
# This is the job that is actually required by rulesets.
|
||||
# We want to only require one job instead of all the individual tests.
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import fs from 'node:fs'
|
||||
|
||||
interface Payload {
|
||||
name: string;
|
||||
value: number;
|
||||
interval: number;
|
||||
mtype: string;
|
||||
time: number;
|
||||
}
|
||||
|
||||
console.log("Publishing metrics");
|
||||
|
||||
// Get API key from environment variable
|
||||
const key = process.env.GRAFANA_MISC_STATS_API_KEY;
|
||||
if (!key) {
|
||||
throw new Error("API key is required. Provide it via the GRAFANA_MISC_STATS_API_KEY environment variable");
|
||||
}
|
||||
|
||||
const unixTimestamp = Math.floor(Date.now() / 1000);
|
||||
const data: Payload[] = [];
|
||||
|
||||
const input = fs.readFileSync(0, "utf-8");
|
||||
// parse metrics from input
|
||||
const regexp = /^Metrics: (\{.+\})/ms;
|
||||
const matches = input.match(regexp);
|
||||
|
||||
if (!matches) {
|
||||
throw new Error("No metrics found");
|
||||
}
|
||||
|
||||
console.log('matches[0]', matches[0])
|
||||
console.log('matches[1]', matches[1])
|
||||
|
||||
const metrics: Record<string, string> = JSON.parse(matches[1]);
|
||||
|
||||
// Convert metrics to payload format
|
||||
for (const [metricName, valueStr] of Object.entries(metrics)) {
|
||||
const value = parseInt(valueStr, 10);
|
||||
if (isNaN(value)) {
|
||||
throw new Error(`Metric "${metricName}" has invalid value format: "${valueStr}"`);
|
||||
}
|
||||
|
||||
data.push({
|
||||
name: metricName,
|
||||
value: value,
|
||||
interval: 60,
|
||||
mtype: "gauge",
|
||||
time: unixTimestamp,
|
||||
});
|
||||
}
|
||||
|
||||
const jsonPayload = JSON.stringify(data);
|
||||
console.log(`Publishing metrics to https://graphite-us-central1.grafana.net/metrics, JSON: ${jsonPayload}`);
|
||||
|
||||
const url = 'https://graphite-us-central1.grafana.net/metrics';
|
||||
const username = '6371';
|
||||
const headers = new Headers();
|
||||
headers.set("Content-Type", "application/json");
|
||||
headers.set('Authorization', 'Basic ' + Buffer.from(username + ":" + key).toString('base64'));
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: jsonPayload,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Metrics publishing failed with status code ${response.status}`);
|
||||
}
|
||||
|
||||
console.log("Metrics successfully published");
|
||||
} catch (error) {
|
||||
throw new Error(`Metrics publishing failed: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
Reference in New Issue
Block a user