diff --git a/packages/runtime-cli/README.md b/packages/runtime-cli/README.md index d44a30d..bdb5710 100644 --- a/packages/runtime-cli/README.md +++ b/packages/runtime-cli/README.md @@ -180,9 +180,13 @@ autter-runtime sourcemaps upload [--release ] [--url-prefix ] as fit the 32 MB limit), so issues show the lines around each frame; `--no-sources-content` leaves it out. For server output, `--url-prefix` is any tail of the path the server runs the files from (`/dist/` for - `/app/dist/server.js`), or `/`. Release: `--release`, else `GITHUB_SHA`, - `VERCEL_GIT_COMMIT_SHA`, `CF_PAGES_COMMIT_SHA`, `GIT_SHA`, then - `git rev-parse HEAD`. + `/app/dist/server.js`), or `/`. Release: `--release`, else `AUTTER_RELEASE`, else + the first of `GITHUB_SHA`, `VERCEL_GIT_COMMIT_SHA`, `CF_PAGES_COMMIT_SHA`, + `GIT_SHA` that is set (any value), else the first commit (7 to 40 + hexadecimal characters) in the other commit variables `@autter/runtime-node` + reads (`AUTTER_COMMIT`, `GIT_COMMIT`, `COMMIT_SHA`, `SOURCE_COMMIT`, + `RENDER_GIT_COMMIT`, `RAILWAY_GIT_COMMIT_SHA`, `HEROKU_SLUG_COMMIT`, + `SOURCE_VERSION`), else `git rev-parse HEAD`. - **upload --best-effort** is for build scripts that also run locally and in fork PRs: without a key, or when the upload fails, it says so and exits 0; with `--delete-after-upload`, every `.map` under `` is then deleted diff --git a/packages/runtime-cli/src/build.ts b/packages/runtime-cli/src/build.ts index 630da94..acd0b0a 100644 --- a/packages/runtime-cli/src/build.ts +++ b/packages/runtime-cli/src/build.ts @@ -7,7 +7,7 @@ import { uploadDir, type UploadSummary } from "./upload.js"; /** Options shared by the Vite/Rollup, webpack and Next.js integrations. */ export interface AutterSourceMapsOptions { - /** Default: GITHUB_SHA, VERCEL_GIT_COMMIT_SHA, CF_PAGES_COMMIT_SHA, GIT_SHA, then `git rev-parse HEAD`. */ + /** Default: AUTTER_RELEASE, else GITHUB_SHA, VERCEL_GIT_COMMIT_SHA, CF_PAGES_COMMIT_SHA, GIT_SHA (any value), else a commit in @autter/runtime-node's other commit variables (AUTTER_COMMIT, GIT_COMMIT, ...), else `git rev-parse HEAD`. */ release?: string; /** Where the build output is served from. Default: the bundler's public * path (Vite `base`, webpack `output.publicPath`, Next `/_next/`), else "/". */ diff --git a/packages/runtime-cli/src/cli.ts b/packages/runtime-cli/src/cli.ts index 4fc741d..4340dc9 100644 --- a/packages/runtime-cli/src/cli.ts +++ b/packages/runtime-cli/src/cli.ts @@ -60,8 +60,11 @@ upload Sends every map to /v1/sourcemaps with AUTTER_RUNTIME_KEY, dependencies'), so issues show the lines around each frame. Options: - --release Default: $GITHUB_SHA, $VERCEL_GIT_COMMIT_SHA, - $CF_PAGES_COMMIT_SHA, $GIT_SHA, then git rev-parse HEAD + --release Default: $AUTTER_RELEASE, else $GITHUB_SHA, + $VERCEL_GIT_COMMIT_SHA, $CF_PAGES_COMMIT_SHA, $GIT_SHA + (any value), else a commit in the SDK's other commit + variables ($AUTTER_COMMIT, $GIT_COMMIT, ...), else + git rev-parse HEAD --url-prefix Where is served from: "/" (default), "/static/", or "https://cdn.example.com/app/". For server output, any tail of the path the server runs it from diff --git a/packages/runtime-cli/src/release.ts b/packages/runtime-cli/src/release.ts index f844e17..44ac639 100644 --- a/packages/runtime-cli/src/release.ts +++ b/packages/runtime-cli/src/release.ts @@ -1,19 +1,31 @@ import { execFileSync } from "node:child_process"; +// The commit variables are shared with the SDK: the file is dependency-free +// and bundled into the CLI at build time (tsup), so the published package +// gains no dependency. +import { COMMIT_ENV_VARS, COMMIT_RE, LEGACY_RELEASE_ENV_VARS } from "../../runtime-node/src/deployment.js"; /** What the ingester accepts as a release. */ export const RELEASE_RE = /^[a-zA-Z0-9._-]{1,200}$/; -/** CI variables that carry the commit being built, in the order they are tried. */ -export const RELEASE_ENV_VARS = [ - "GITHUB_SHA", - "VERCEL_GIT_COMMIT_SHA", - "CF_PAGES_COMMIT_SHA", - "GIT_SHA", -] as const; +/** + * CI variables the release is named after, any value, in the order they are + * tried (after --release and AUTTER_RELEASE): unchanged, so existing builds + * keep uploading under the release their bundles report (the browser + * snippets `autter-runtime setup` prints read GITHUB_SHA first too). + */ +export const RELEASE_ENV_VARS = LEGACY_RELEASE_ENV_VARS; + +/** + * The rest of @autter/runtime-node's commit variables, tried after + * RELEASE_ENV_VARS: only a 7-40 character hexadecimal value counts. + */ +const OTHER_COMMIT_ENV_VARS = COMMIT_ENV_VARS.filter( + (name) => !(LEGACY_RELEASE_ENV_VARS as readonly string[]).includes(name), +); export interface ResolvedRelease { release: string; - /** Where it came from: "--release", an env var name, or "git". */ + /** Where it came from: "--release", "AUTTER_RELEASE", a commit variable's name, or "git". */ source: string; } @@ -32,34 +44,47 @@ export function gitHead(cwd: string = process.cwd()): string | null { } } +function checked(release: string, source: string): ResolvedRelease { + if (!RELEASE_RE.test(release)) { + throw new Error( + `release "${release}" (from ${source}) is not valid: use 1-200 letters, digits, ".", "_" or "-" (a commit SHA works)`, + ); + } + return { release, source }; +} + /** - * The release to upload under: the explicit value, else the first CI commit - * variable that is set (GITHUB_SHA, VERCEL_GIT_COMMIT_SHA, CF_PAGES_COMMIT_SHA, - * GIT_SHA), else `git rev-parse HEAD`. Throws with a usable message when none - * is available or the value has characters the ingester rejects. + * The release to upload under: `--release`, else AUTTER_RELEASE, else the + * first non-empty RELEASE_ENV_VARS value (GITHUB_SHA, VERCEL_GIT_COMMIT_SHA, + * CF_PAGES_COMMIT_SHA, GIT_SHA; any value, as before), else the first commit + * (7-40 hexadecimal characters) among the SDK's other commit variables + * (AUTTER_COMMIT, GIT_COMMIT, COMMIT_SHA, SOURCE_COMMIT, RENDER_GIT_COMMIT, + * RAILWAY_GIT_COMMIT_SHA, HEROKU_SLUG_COMMIT, SOURCE_VERSION), else + * `git rev-parse HEAD` (build time only; the SDK never runs git). Throws with + * a usable message when none is available or the value has characters the + * ingester rejects. */ export function resolveRelease( explicit?: string | null, env: NodeJS.ProcessEnv = process.env, cwd: string = process.cwd(), ): ResolvedRelease { - const candidates: Array<[string, string | undefined | null]> = [ + const named: Array<[string, string | undefined | null]> = [ ["--release", explicit], + ["AUTTER_RELEASE", env.AUTTER_RELEASE], ...RELEASE_ENV_VARS.map((name): [string, string | undefined] => [name, env[name]]), ]; - for (const [source, value] of candidates) { + for (const [source, value] of named) { const release = value?.trim(); - if (!release) continue; - if (!RELEASE_RE.test(release)) { - throw new Error( - `release "${release}" (from ${source}) is not valid: use 1-200 letters, digits, ".", "_" or "-" (a commit SHA works)`, - ); - } - return { release, source }; + if (release) return checked(release, source); + } + for (const source of OTHER_COMMIT_ENV_VARS) { + const value = env[source]?.trim(); + if (value && COMMIT_RE.test(value)) return { release: value, source }; } const head = gitHead(cwd); if (head) return { release: head, source: "git" }; throw new Error( - `no release: pass --release , or set one of ${RELEASE_ENV_VARS.join(", ")}, or run inside a git checkout`, + `no release: pass --release , or set AUTTER_RELEASE, one of ${RELEASE_ENV_VARS.join(", ")}, or a commit in one of ${OTHER_COMMIT_ENV_VARS.join(", ")}, or run inside a git checkout`, ); } diff --git a/packages/runtime-cli/test/upload.test.mjs b/packages/runtime-cli/test/upload.test.mjs index c785fcb..4eda2ed 100644 --- a/packages/runtime-cli/test/upload.test.mjs +++ b/packages/runtime-cli/test/upload.test.mjs @@ -10,6 +10,7 @@ import { injectDir, isInAppSource, MAX_MAP_CHARS, + RELEASE_ENV_VARS, resolveEndpoint, resolveRelease, servedFilename, @@ -239,16 +240,29 @@ test("--delete-after-upload removes uploaded maps and their sourceMappingURL com assert.match(read(dir, "app.js"), /\/\/# debugId=/); }); -test("release comes from --release, then CI variables, then git", () => { - assert.deepEqual(resolveRelease("v1.2.3", { GITHUB_SHA: "aaa" }), { release: "v1.2.3", source: "--release" }); +test("release comes from --release, then AUTTER_RELEASE, then CI variables (any value, as before), then other commit variables, then git", () => { + const a = "aaaaaaa", c = "cccccccccccccccccccccccccccccccccccccccc", d = "ddddddd"; + assert.deepEqual(resolveRelease("v1.2.3", { GITHUB_SHA: "aaa", AUTTER_RELEASE: "r9" }), { release: "v1.2.3", source: "--release" }); + assert.deepEqual(resolveRelease(undefined, { GITHUB_SHA: "aaa", AUTTER_RELEASE: "r9" }), { release: "r9", source: "AUTTER_RELEASE" }); + // Unchanged for existing builds: the four CI variables, in their order, any value. assert.deepEqual(resolveRelease(undefined, { GITHUB_SHA: "aaa", GIT_SHA: "ddd" }), { release: "aaa", source: "GITHUB_SHA" }); + assert.deepEqual(resolveRelease(undefined, { GITHUB_SHA: a, GIT_SHA: d }), { release: a, source: "GITHUB_SHA" }, "both set and different: GITHUB_SHA wins, as before"); assert.deepEqual(resolveRelease(undefined, { VERCEL_GIT_COMMIT_SHA: "bbb", GIT_SHA: "ddd" }), { release: "bbb", source: "VERCEL_GIT_COMMIT_SHA" }); assert.deepEqual(resolveRelease(undefined, { CF_PAGES_COMMIT_SHA: "ccc", GIT_SHA: "ddd" }), { release: "ccc", source: "CF_PAGES_COMMIT_SHA" }); assert.deepEqual(resolveRelease(undefined, { GIT_SHA: "ddd" }), { release: "ddd", source: "GIT_SHA" }); + assert.deepEqual(resolveRelease(undefined, { GIT_SHA: "v1.2.3" }), { release: "v1.2.3", source: "GIT_SHA" }, "a name, not a commit: still the release, as before"); + assert.deepEqual(resolveRelease(undefined, { GIT_SHA: "v1.2.3", AUTTER_COMMIT: c }), { release: "v1.2.3", source: "GIT_SHA" }); + assert.deepEqual([...RELEASE_ENV_VARS], ["GITHUB_SHA", "VERCEL_GIT_COMMIT_SHA", "CF_PAGES_COMMIT_SHA", "GIT_SHA"]); + // Then the SDK's other commit variables, in its order: only a commit counts. + assert.deepEqual(resolveRelease(undefined, { HEROKU_SLUG_COMMIT: c, AUTTER_COMMIT: d }), { release: d, source: "AUTTER_COMMIT" }); + assert.deepEqual(resolveRelease(undefined, { SOURCE_VERSION: "refs/heads/main", RENDER_GIT_COMMIT: c }), { release: c, source: "RENDER_GIT_COMMIT" }); const head = execFileSync("git", ["rev-parse", "HEAD"], { encoding: "utf8" }).trim(); assert.deepEqual(resolveRelease(undefined, {}), { release: head, source: "git" }); + assert.deepEqual(resolveRelease(undefined, { SOURCE_VERSION: "pr/12" }), { release: head, source: "git" }); assert.throws(() => resolveRelease(undefined, {}, tempDir()), /no release/); assert.throws(() => resolveRelease("my app@1.0", {}), /not valid/); + assert.throws(() => resolveRelease(undefined, { AUTTER_RELEASE: "my app@1.0" }), /from AUTTER_RELEASE\) is not valid/); + assert.throws(() => resolveRelease(undefined, { GIT_SHA: "my app@1.0" }), /from GIT_SHA\) is not valid/); }); test("served filenames and endpoints are normalised", () => { diff --git a/packages/runtime-node/src/deployment.ts b/packages/runtime-node/src/deployment.ts new file mode 100644 index 0000000..45a8d24 --- /dev/null +++ b/packages/runtime-node/src/deployment.ts @@ -0,0 +1,120 @@ +/** + * What this process is running: its commit, its default release and its + * default environment, read from environment variables only. Never runs git + * or any other subprocess: the SDK reads what the platform or the build put + * in the environment, nothing else. + * + * Dependency-free on purpose: `@autter/runtime-cli` (src/release.ts) imports + * this file by path and bundles it, so the source-map upload picks the same + * commit variables, in the same order, as the SDK that reports the errors. + * `runtime-core` is not an option there: it is a private package the CLI + * does not depend on. + */ + +/** + * Environment variables that carry the commit being built or run, in the + * order they are tried: Autter's own, then the four the source-map upload + * CLI has always read, in its order (LEGACY_RELEASE_ENV_VARS), then the + * other generic names and hosting platforms. One list for + * `initAutterServer` / `initAutterLogging`, `enrichDeployment`, + * `autter-runtime verify` and the source-map upload CLI (and the same list + * as the Python adapter). With a commit in any one of the CLI's four, the + * SDK's default release is the release the CLI uploads maps under. + */ +export const COMMIT_ENV_VARS = [ + "AUTTER_COMMIT", + "GITHUB_SHA", + "VERCEL_GIT_COMMIT_SHA", + "CF_PAGES_COMMIT_SHA", + "GIT_SHA", + "GIT_COMMIT", + "COMMIT_SHA", + "SOURCE_COMMIT", + "RENDER_GIT_COMMIT", + "RAILWAY_GIT_COMMIT_SHA", + "HEROKU_SLUG_COMMIT", + "SOURCE_VERSION", +] as const; + +/** + * The variables @autter/runtime-cli named its upload release after before + * the shared list, in its order. The CLI still takes any value from these + * (a release is a name there), so existing builds keep uploading under the + * release their bundles report. + */ +export const LEGACY_RELEASE_ENV_VARS = ["GITHUB_SHA", "VERCEL_GIT_COMMIT_SHA", "CF_PAGES_COMMIT_SHA", "GIT_SHA"] as const; + +/** A commit: 7 to 40 hexadecimal characters (an abbreviated or full git SHA-1). */ +export const COMMIT_RE = /^[0-9a-fA-F]{7,40}$/; + +type Env = Record; + +export interface DetectedCommit { + commit: string; + /** The variable it came from. */ + source: (typeof COMMIT_ENV_VARS)[number]; +} + +/** + * The first commit variable whose (trimmed) value is a commit. A variable set + * to anything else (a branch, a tag, `refs/heads/main`) is skipped, so the + * next one can still answer. + */ +export function detectCommitFrom(env: Env = process.env): DetectedCommit | null { + for (const source of COMMIT_ENV_VARS) { + const value = env[source]?.trim(); + if (value && COMMIT_RE.test(value)) return { commit: value, source }; + } + return null; +} + +/** The running commit, or undefined. See {@link detectCommitFrom}. */ +export function detectCommit(env: Env = process.env): string | undefined { + return detectCommitFrom(env)?.commit; +} + +const OFF = new Set(["false", "0", "no", "off"]); + +/** `AUTTER_DETECT_RELEASE=false` (or 0 / no / off) turns the default release off. */ +export function releaseDetectionDisabled(env: Env = process.env): boolean { + const value = env.AUTTER_DETECT_RELEASE?.trim().toLowerCase(); + return value !== undefined && OFF.has(value); +} + +/** + * The release when the app passed none: `AUTTER_RELEASE`, else the detected + * commit (the full value, so Autter can resolve it to the commit and its + * pull request). Undefined when `AUTTER_DETECT_RELEASE=false`. + */ +export function defaultRelease(env: Env = process.env): string | undefined { + if (releaseDetectionDisabled(env)) return undefined; + return env.AUTTER_RELEASE?.trim() || detectCommit(env); +} + +/** + * The release an SDK reports: an explicit non-empty `release` always wins; + * `release: false` means none; unset (or empty) falls back to + * {@link defaultRelease}. + */ +export function resolveRelease(release: string | false | null | undefined, env: Env = process.env): string | undefined { + if (release === false) return undefined; + if (typeof release === "string" && release) return release; + return defaultRelease(env); +} + +/** Variables test runners set in the processes that run tests. */ +export const TEST_RUNNER_ENV_VARS = ["JEST_WORKER_ID", "VITEST", "NODE_TEST_CONTEXT"] as const; + +/** True inside Jest, Vitest or `node --test`. */ +export function testRunnerDetected(env: Env = process.env): boolean { + return TEST_RUNNER_ENV_VARS.some((name) => !!env[name]); +} + +/** + * The environment an SDK reports: the explicit option, else `NODE_ENV`, else + * `test` under a test runner (Jest, Vitest, `node --test`), else + * `production`. An explicit value (option or `NODE_ENV`) always wins. + */ +export function resolveEnvironment(environment: string | undefined, env: Env = process.env): string { + return environment ?? env.NODE_ENV ?? (testRunnerDetected(env) ? "test" : "production"); +} diff --git a/packages/runtime-node/src/enrichers.ts b/packages/runtime-node/src/enrichers.ts index 82b5465..690bb9e 100644 --- a/packages/runtime-node/src/enrichers.ts +++ b/packages/runtime-node/src/enrichers.ts @@ -1,4 +1,5 @@ import { parseUserAgent } from "@autter/runtime-core"; +import { detectCommit } from "./deployment.js"; import type { RuntimeEnricher } from "./logger.js"; /** @@ -80,29 +81,20 @@ const REGION_VARS = [ "RENDER_REGION", "AZURE_REGION", ]; -const COMMIT_VARS = [ - "AUTTER_COMMIT", - "GIT_SHA", - "GIT_COMMIT", - "COMMIT_SHA", - "SOURCE_COMMIT", - "VERCEL_GIT_COMMIT_SHA", - "GITHUB_SHA", - "RENDER_GIT_COMMIT", - "RAILWAY_GIT_COMMIT_SHA", - "CF_PAGES_COMMIT_SHA", - "HEROKU_SLUG_COMMIT", - "SOURCE_VERSION", -]; -/** `deployment.region` and `deployment.commit` from common platform environment variables. */ +/** + * `deployment.region` and `deployment.commit` from common platform + * environment variables. The commit is read from COMMIT_ENV_VARS + * (deployment.ts), the same list initAutterServer uses, and only a 7-40 + * character hexadecimal value counts. + */ export function enrichDeployment( env: Record = process.env, ): RuntimeEnricher { const pick = (names: string[]) => names.map((name) => env[name]?.trim()).find((value) => value); const region = pick(REGION_VARS)?.slice(0, 64); - const commit = pick(COMMIT_VARS)?.slice(0, 64); + const commit = detectCommit(env); return (event) => { if (region) event.attributes["deployment.region"] = region; if (commit) event.attributes["deployment.commit"] = commit; diff --git a/packages/runtime-node/src/logging-only.ts b/packages/runtime-node/src/logging-only.ts index 55d1883..ef59c7e 100644 --- a/packages/runtime-node/src/logging-only.ts +++ b/packages/runtime-node/src/logging-only.ts @@ -34,6 +34,7 @@ import { unregisterFlushTargets, type AutoFlushHandle, } from "./lifecycle.js"; +import { resolveEnvironment, resolveRelease } from "./deployment.js"; import { makeRedactor, type RedactOptions } from "./redact.js"; import { markCaptured } from "./requests.js"; import { @@ -77,8 +78,10 @@ export interface AutterLoggingOptions { /** Ingester base URL. Default: https://otlp.autter.dev */ endpoint?: string; service: string; + /** Same default as initAutterServer: NODE_ENV, else `test` under a test runner, else `production`. */ environment?: string; - release?: string; + /** Same default as initAutterServer: AUTTER_RELEASE, else the detected commit; `false` for none. */ + release?: string | false; logging?: RuntimeLoggingOptions; /** Same semantics as initAutterServer. Default true. */ redactAttributes?: boolean | RedactOptions; @@ -133,7 +136,8 @@ export function initAutterLogging(options: AutterLoggingOptions): AutterLogging } if (isDebugEnabled() || options.debug === true) setDebugMode(true); const endpoint = (options.endpoint ?? "https://otlp.autter.dev").replace(/\/$/, ""); - const environment = options.environment ?? process.env.NODE_ENV ?? "production"; + const environment = resolveEnvironment(options.environment); + const release = resolveRelease(options.release); const redact = makeRedactor(options.redactAttributes ?? true); const mode = options.exceptions ?? "auto"; debugLog(`initialising logger-only service=${options.service} endpoint=${endpoint}`); @@ -221,7 +225,7 @@ export function initAutterLogging(options: AutterLoggingOptions): AutterLogging apiKey: resolveIngestKey(options.apiKey, {}) ?? "", service: options.service, environment, - ...(options.release ? { release: options.release } : {}), + ...(release ? { release } : {}), ...(options.logging ? { options: options.logging } : {}), redact, reportOutcome: (name, message, attributes) => { diff --git a/packages/runtime-node/src/server.ts b/packages/runtime-node/src/server.ts index 1a79f4e..1c5c940 100644 --- a/packages/runtime-node/src/server.ts +++ b/packages/runtime-node/src/server.ts @@ -66,6 +66,7 @@ import { authHeaders, resolveIngestKey, warnKeyKind, warnMissingKey } from "./in import { IngestMetricExporter, IngestSpanExporter } from "./ingest-exporters.js"; import { makeUrlQueryPolicy } from "./url-query.js"; import { startMemoryMetrics } from "./memory.js"; +import { detectCommit, resolveEnvironment, resolveRelease } from "./deployment.js"; import { configureExportErrors, exportErrorMark, @@ -235,9 +236,20 @@ export interface AutterServerOptions { service: string; /** Stable for this process lifetime; use an ECS task/container ID or pod UID plus restart count to correlate platform events. */ instanceId?: string; + /** + * Default: NODE_ENV; else `test` when a test runner is detected (Jest, + * Vitest, `node --test`); else `production`. + */ environment?: string; - /** e.g. a git SHA. Maps to service.version / release. */ - release?: string; + /** + * e.g. a git SHA. Maps to service.version / release. Default: + * AUTTER_RELEASE, else the commit read from the platform's commit + * variable (GIT_SHA, VERCEL_GIT_COMMIT_SHA, GITHUB_SHA, ...: see + * COMMIT_ENV_VARS in deployment.ts). `false`, or AUTTER_DETECT_RELEASE=false, + * reports no release. The detected commit is also sent as + * `deployment.commit` whatever the release. + */ + release?: string | false; /** Head-sampling ratio for regular traces. Default 0.01 (1%). */ traceSampleRate?: number; /** @@ -1352,12 +1364,15 @@ export function initAutterServer(options: AutterServerOptions): AutterServer { ), "traces", ); - const environment = options.environment ?? process.env.NODE_ENV ?? "production"; + const environment = resolveEnvironment(options.environment); + // Environment variables only: the SDK never runs git (deployment.ts). + const commit = detectCommit(); + const release = resolveRelease(options.release); // Debug mode: option OR AUTTER_DEBUG env (lifecycle seeds itself from the // env at import time — never clobber an env-enabled session here). if (isDebugEnabled() || options.debug === true) setDebugMode(true); - debugLog(`initialising service=${options.service} endpoint=${endpoint}`); + debugLog(`initialising service=${options.service} endpoint=${endpoint} environment=${environment} release=${release ?? "none"}`); if (apiKey === null) warnMissingKey("server", options.service); else warnKeyKind(apiKey); S.redactor = redactor; @@ -1368,7 +1383,8 @@ export function initAutterServer(options: AutterServerOptions): AutterServer { const resource = resourceFromAttributes({ [ATTR_SERVICE_NAME]: options.service, "service.instance.id": (options.instanceId || process.env.AUTTER_RUNTIME_INSTANCE_ID || randomUUID()).slice(0, 128), - ...(options.release ? { [ATTR_SERVICE_VERSION]: options.release } : {}), + ...(release ? { [ATTR_SERVICE_VERSION]: release } : {}), + ...(commit ? { "deployment.commit": commit } : {}), "deployment.environment": environment, // OTel semconv for SDK distributions: the ingester records which // Autter SDK version each service runs (version compatibility checks). @@ -1711,7 +1727,7 @@ export function initAutterServer(options: AutterServerOptions): AutterServer { } configureRuntimeLogger({ endpoint, apiKey: apiKey ?? "", service: options.service, environment, distro, - release: options.release, options: options.logging, redact: redactor, + release, options: options.logging, redact: redactor, run: (name, fn, attributes, links) => runWithSpan(processTracer, name, fn, redactor(attributes), links), reportOutcome, }); diff --git a/packages/runtime-node/src/verify.ts b/packages/runtime-node/src/verify.ts index 56d95d4..3c8cbb8 100644 --- a/packages/runtime-node/src/verify.ts +++ b/packages/runtime-node/src/verify.ts @@ -4,6 +4,7 @@ import { join } from "node:path"; import { compatRequest, fetchIngesterCompat, SDK_IDENTITY, type HttpResult } from "./compat.js"; import { diagnoseConnectivity } from "./connectivity.js"; import { checkStorage, resolveEndpoint, versionSkew, installedSdks } from "./doctor.js"; +import { defaultRelease, detectCommit } from "./deployment.js"; import { refusalReason, resolveIngestKey } from "./ingest-auth.js"; import { versionAtLeast } from "../../otlp-ingester/src/compat.js"; @@ -49,6 +50,8 @@ export interface VerifyReport { service: string; environment: string; release: string | null; + /** The commit read from the commit variables (deployment.ts), sent as `deployment.commit`. */ + commit: string | null; traceId: string; steps: VerifyStep[]; firstFailure: VerifyStep | null; @@ -84,32 +87,28 @@ function defaultService(cwd: string, env: NodeJS.ProcessEnv): string { return "autter-verify"; } -function defaultRelease(env: NodeJS.ProcessEnv): string | null { - return ( - env.AUTTER_RELEASE || - env.GIT_SHA || - env.VERCEL_GIT_COMMIT_SHA || - env.RENDER_GIT_COMMIT || - env.RAILWAY_GIT_COMMIT_SHA || - env.CF_PAGES_COMMIT_SHA || - env.GITHUB_SHA || - null - ); -} - const nowNanos = () => (BigInt(Date.now()) * 1_000_000n).toString(); const str = (key: string, value: string) => ({ key, value: { stringValue: value } }); -function resourceAttributes(opts: { service: string; environment: string; release: string | null; traceId: string }) { +interface TestResource { + service: string; + environment: string; + release: string | null; + commit: string | null; + traceId: string; +} + +function resourceAttributes(opts: TestResource) { return [ str("service.name", opts.service), str("deployment.environment", opts.environment), ...(opts.release ? [str("service.version", opts.release)] : []), + ...(opts.commit ? [str("deployment.commit", opts.commit)] : []), str("autter.verify.id", opts.traceId), ]; } -function testTraces(opts: { service: string; environment: string; release: string | null; traceId: string; withError: boolean }) { +function testTraces(opts: TestResource & { withError: boolean }) { const end = nowNanos(); const start = (BigInt(end) - 1_000_000n).toString(); return { @@ -153,7 +152,7 @@ function testTraces(opts: { service: string; environment: string; release: strin }; } -function testLogs(opts: { service: string; environment: string; release: string | null; traceId: string }) { +function testLogs(opts: TestResource) { return { resourceLogs: [ { @@ -214,7 +213,10 @@ export async function runVerify(options: VerifyOptions = {}, env: NodeJS.Process const cwd = options.cwd ?? process.cwd(); const service = options.service || defaultService(cwd, env); const environment = options.environment || "verify"; - const release = options.release || defaultRelease(env); + // The same default as the SDK (deployment.ts): AUTTER_RELEASE, else the + // detected commit, unless AUTTER_DETECT_RELEASE=false. + const release = options.release || defaultRelease(env) || null; + const commit = detectCommit(env) ?? null; const traceId = randomBytes(16).toString("hex"); const key = resolveIngestKey(options.key, env); const steps: VerifyStep[] = []; @@ -230,6 +232,7 @@ export async function runVerify(options: VerifyOptions = {}, env: NodeJS.Process service, environment, release, + commit, traceId, steps, firstFailure, @@ -327,7 +330,7 @@ export async function runVerify(options: VerifyOptions = {}, env: NodeJS.Process // ingest const ingestRes = await compatRequest("POST", `${endpoint}/v1/traces`, { - body: JSON.stringify(testTraces({ service, environment, release, traceId, withError: lookupSupported })), + body: JSON.stringify(testTraces({ service, environment, release, commit, traceId, withError: lookupSupported })), headers, unref: false, timeoutMs, @@ -397,7 +400,7 @@ export async function runVerify(options: VerifyOptions = {}, env: NodeJS.Process step: "release", status: "warn", detail: "no release set: errors are not tied to a deploy, so \"broke in release X\" can't work", - fix: "Pass `release` to initAutterServer (and initAutterBrowser), e.g. process.env.GIT_SHA; run verify with --release or GIT_SHA set.", + fix: "The SDK reports AUTTER_RELEASE, else the commit in a commit variable (GIT_SHA, VERCEL_GIT_COMMIT_SHA, GITHUB_SHA, ...), unless AUTTER_DETECT_RELEASE=false. Set one of them where the app runs (or pass `release` to initAutterServer, and initAutterBrowser); run verify with --release or the same variable set.", }); } else if (stored.occurrences.releases.includes(release) && stored.spans.releases.includes(release)) { add({ step: "release", status: "pass", detail: `release ${release} stored on the error and the span` }); @@ -417,7 +420,7 @@ export async function runVerify(options: VerifyOptions = {}, env: NodeJS.Process return finish(); } const logRes = await compatRequest("POST", `${endpoint}/v1/logs`, { - body: JSON.stringify(testLogs({ service, environment, release, traceId })), + body: JSON.stringify(testLogs({ service, environment, release, commit, traceId })), headers, unref: false, timeoutMs, @@ -488,7 +491,7 @@ Options: --key Server ingest key (default: $AUTTER_RUNTIME_KEY) --service Service name (default: $AUTTER_SERVICE, $OTEL_SERVICE_NAME, or package.json name) --environment Environment for the test event (default: verify) - --release Release to check (default: $AUTTER_RELEASE, $GIT_SHA or your platform's commit variable) + --release Release to check (default, as the SDK: $AUTTER_RELEASE, else the commit in $GIT_SHA or your platform's commit variable) --wait How long to wait for the event to be stored (default 20000) --timeout Per-request timeout (default 5000) --json Machine-readable output diff --git a/packages/runtime-node/test/environment.test.mjs b/packages/runtime-node/test/environment.test.mjs new file mode 100644 index 0000000..14840d6 --- /dev/null +++ b/packages/runtime-node/test/environment.test.mjs @@ -0,0 +1,297 @@ +// What a service says it runs: its environment, its commit and its release +// (src/deployment.ts), and that initAutterServer and initAutterLogging send +// them. The rules: +// +// - environment: the option, else NODE_ENV, else `test` when a test runner +// is detected (JEST_WORKER_ID, VITEST, NODE_TEST_CONTEXT), else +// `production`. An explicit value always wins. +// - commit: the first commit variable (COMMIT_ENV_VARS) holding 7-40 +// hexadecimal characters, sent as the resource attribute +// `deployment.commit` whenever one is found. Never read from git. +// - release: the option, else AUTTER_RELEASE, else the commit (full value). +// `release: false` or AUTTER_DETECT_RELEASE=false: no release. +// +// This file itself runs under `node --test`, which sets NODE_TEST_CONTEXT: +// the child processes below get an environment with every variable these +// rules read removed, then only what each case sets. +import assert from "node:assert/strict"; +import { spawn } from "node:child_process"; +import { createServer } from "node:http"; +import { dirname, join } from "node:path"; +import { after, before, test } from "node:test"; +import { fileURLToPath } from "node:url"; +import { tsImport } from "tsx/esm/api"; +import { enrichDeployment } from "../dist/index.js"; +import { until } from "./support/wait.mjs"; + +const { + COMMIT_ENV_VARS, + COMMIT_RE, + LEGACY_RELEASE_ENV_VARS, + TEST_RUNNER_ENV_VARS, + defaultRelease, + detectCommit, + detectCommitFrom, + resolveEnvironment, + resolveRelease, +} = await tsImport("../src/deployment.ts", import.meta.url); + +const here = dirname(fileURLToPath(import.meta.url)); +const SHA = "0123456789abcdef0123456789abcdef01234567"; +const SHORT = "abc1234"; + +// ---------------------------------------------------------------- helpers + +test("environment: an explicit option wins, then NODE_ENV, then test under a runner, then production", () => { + assert.equal(resolveEnvironment(undefined, {}), "production"); + for (const name of ["JEST_WORKER_ID", "VITEST", "NODE_TEST_CONTEXT"]) { + assert.equal(resolveEnvironment(undefined, { [name]: "1" }), "test", name); + assert.equal(resolveEnvironment(undefined, { [name]: "1", NODE_ENV: "staging" }), "staging", `${name} + NODE_ENV`); + assert.equal(resolveEnvironment("production", { [name]: "1" }), "production", `${name} + option`); + } + assert.equal(resolveEnvironment(undefined, { NODE_ENV: "development" }), "development"); + assert.equal(resolveEnvironment("eu-prod", { NODE_ENV: "development" }), "eu-prod"); + // Set but empty is not set. + assert.equal(resolveEnvironment(undefined, { VITEST: "" }), "production"); + assert.deepEqual([...TEST_RUNNER_ENV_VARS], ["JEST_WORKER_ID", "VITEST", "NODE_TEST_CONTEXT"]); +}); + +test("commit: only 7 to 40 hexadecimal characters, first valid variable in the shared order", () => { + // Autter's own, then the source-map CLI's four in its order, then the rest. + assert.deepEqual([...COMMIT_ENV_VARS], [ + "AUTTER_COMMIT", "GITHUB_SHA", "VERCEL_GIT_COMMIT_SHA", "CF_PAGES_COMMIT_SHA", "GIT_SHA", "GIT_COMMIT", + "COMMIT_SHA", "SOURCE_COMMIT", "RENDER_GIT_COMMIT", "RAILWAY_GIT_COMMIT_SHA", "HEROKU_SLUG_COMMIT", + "SOURCE_VERSION", + ]); + assert.deepEqual([...LEGACY_RELEASE_ENV_VARS], ["GITHUB_SHA", "VERCEL_GIT_COMMIT_SHA", "CF_PAGES_COMMIT_SHA", "GIT_SHA"]); + assert.deepEqual(COMMIT_ENV_VARS.slice(1, 5), [...LEGACY_RELEASE_ENV_VARS]); + assert.equal(detectCommit({}), undefined); + assert.equal(detectCommit({ GIT_SHA: SHA }), SHA); + assert.equal(detectCommit({ GIT_SHA: ` ${SHORT}\n` }), SHORT); + assert.equal(detectCommit({ GIT_SHA: SHA.toUpperCase() }), SHA.toUpperCase()); + for (const bad of ["abc123", `${SHA}0`, "main", "refs/heads/main", "v1.2.3", "0123abz", "", " "]) { + assert.equal(detectCommit({ GIT_SHA: bad }), undefined, JSON.stringify(bad)); + assert.ok(!COMMIT_RE.test(bad.trim()), bad); + } + // An earlier variable wins; one that is not a commit is skipped. + assert.deepEqual(detectCommitFrom({ AUTTER_COMMIT: SHORT, GITHUB_SHA: SHA }), { commit: SHORT, source: "AUTTER_COMMIT" }); + assert.deepEqual(detectCommitFrom({ GIT_SHA: SHORT, GITHUB_SHA: SHA }), { commit: SHA, source: "GITHUB_SHA" }, "GITHUB_SHA before GIT_SHA, as the CLI"); + assert.deepEqual(detectCommitFrom({ SOURCE_VERSION: "pr/12", GIT_SHA: "main", HEROKU_SLUG_COMMIT: SHA }), { commit: SHA, source: "HEROKU_SLUG_COMMIT" }); + for (const name of COMMIT_ENV_VARS) assert.equal(detectCommit({ [name]: SHA }), SHA, name); +}); + +test("release: explicit wins, then AUTTER_RELEASE, then the full commit; false or AUTTER_DETECT_RELEASE=false is none", () => { + assert.equal(resolveRelease(undefined, {}), undefined); + assert.equal(resolveRelease(undefined, { GITHUB_SHA: SHA }), SHA); + assert.equal(resolveRelease(undefined, { GITHUB_SHA: SHA, AUTTER_RELEASE: "2024.06.1" }), "2024.06.1"); + assert.equal(resolveRelease("v2", { GITHUB_SHA: SHA, AUTTER_RELEASE: "2024.06.1" }), "v2"); + assert.equal(resolveRelease("", { GITHUB_SHA: SHA }), SHA, "an empty release is no release given"); + assert.equal(resolveRelease(false, { GITHUB_SHA: SHA, AUTTER_RELEASE: "r" }), undefined); + for (const off of ["false", "FALSE", "0", "no", "off"]) { + assert.equal(resolveRelease(undefined, { GITHUB_SHA: SHA, AUTTER_RELEASE: "r", AUTTER_DETECT_RELEASE: off }), undefined, off); + assert.equal(resolveRelease("v2", { AUTTER_DETECT_RELEASE: off }), "v2", `explicit beats ${off}`); + } + assert.equal(resolveRelease(undefined, { GITHUB_SHA: SHA, AUTTER_DETECT_RELEASE: "true" }), SHA); + assert.equal(resolveRelease(undefined, { GIT_SHA: "main" }), undefined, "a value that is not a commit is not a release"); + assert.equal(defaultRelease({ AUTTER_RELEASE: " r1 " }), "r1"); +}); + +test("the SDK's default release is the release the source-map CLI uploads under, for a commit in any one variable", async () => { + // The CLI's resolver from source (it bundles src/deployment.ts by path). + const cli = await tsImport("../../runtime-cli/src/release.ts", import.meta.url); + assert.deepEqual([...cli.RELEASE_ENV_VARS], [...LEGACY_RELEASE_ENV_VARS]); + for (const name of COMMIT_ENV_VARS) { + for (const commit of [SHA, SHORT]) { + const env = { [name]: commit }; + assert.equal(defaultRelease(env), commit, name); + assert.deepEqual(cli.resolveRelease(undefined, env), { release: commit, source: name }, name); + } + } + const named = { AUTTER_RELEASE: "r9", GITHUB_SHA: SHA }; + assert.equal(defaultRelease(named), cli.resolveRelease(undefined, named).release); +}); + +test("enrichDeployment: deployment.commit only for a commit, from the shared list", () => { + const run = (env) => { + const event = { attributes: {} }; + enrichDeployment(env)(event, {}); + return event.attributes; + }; + assert.deepEqual(run({ RENDER_GIT_COMMIT: SHA, AWS_REGION: "eu-west-1" }), { "deployment.region": "eu-west-1", "deployment.commit": SHA }); + assert.deepEqual(run({ GIT_SHA: "main" }), {}); + assert.deepEqual(run({ GIT_SHA: "not-a-sha", AUTTER_COMMIT: SHORT }), { "deployment.commit": SHORT }); +}); + +// ------------------------------------------------- what the SDK sends + +/** Every variable the rules read, removed from each child's environment. */ +const RULE_VARS = [...TEST_RUNNER_ENV_VARS, ...COMMIT_ENV_VARS, "NODE_ENV", "AUTTER_RELEASE", "AUTTER_DETECT_RELEASE"]; + +const received = []; +let collector; +let endpoint; + +before(async () => { + collector = createServer((req, res) => { + const chunks = []; + req.on("data", (c) => chunks.push(c)); + req.on("end", () => { + try { + received.push({ path: req.url, body: JSON.parse(Buffer.concat(chunks).toString("utf8")) }); + } catch { + /* not JSON: not ours */ + } + res.end("{}"); + }); + }); + await new Promise((resolve) => collector.listen(0, "127.0.0.1", resolve)); + endpoint = `http://127.0.0.1:${collector.address().port}`; +}); + +after(async () => { + await new Promise((resolve) => { + collector.close(resolve); + collector.closeAllConnections(); + }); +}); + +const value = (v) => v?.stringValue ?? v?.intValue ?? v?.boolValue ?? v?.doubleValue; +const attrs = (list) => Object.fromEntries((list ?? []).map((a) => [a.key, value(a.value)])); + +/** The resources one service sent: of its spans, and of its log records. */ +function resourcesOf(service) { + const spans = []; + const logs = []; + for (const { body } of received) { + for (const rs of body.resourceSpans ?? []) { + const resource = attrs(rs.resource?.attributes); + if (resource["service.name"] === service) spans.push(resource); + } + for (const rl of body.resourceLogs ?? []) { + // Log records name it with the newer semconv key. + const { "deployment.environment.name": environment, ...resource } = attrs(rl.resource?.attributes); + if (resource["service.name"] === service) logs.push({ ...resource, "deployment.environment": environment }); + } + } + return { spans, logs }; +} + +let serial = 0; + +/** + * Start the SDK in a child process (`mode`: "server" for initAutterServer, + * "logging" for initAutterLogging) with `options` and only the rule + * variables in `env`; emit one span (server mode) and one log record; shut + * down. Returns the resources the collector received. + */ +async function run(mode, options = {}, env = {}) { + const service = `env-${mode}-${++serial}`; + const script = ` + import { trace } from "@opentelemetry/api"; + import { initAutterLogging, initAutterServer, runtimeLogger } from "./dist/index.js"; + const base = { + apiKey: "k", service: process.env.SERVICE, endpoint: process.env.ENDPOINT, + captureGlobalErrors: false, autoFlush: false, logging: { console: false, file: false }, + }; + const options = { ...base, ...JSON.parse(process.env.OPTIONS) }; + if (process.env.MODE === "server") { + const sdk = initAutterServer({ ...options, traceSampleRate: 1, memoryMetrics: false, compatCheck: false, metricIntervalMs: 3_600_000 }); + trace.getTracer("t").startSpan("work").end(); + runtimeLogger.info("hello"); + await sdk.shutdown(); + } else { + const logging = initAutterLogging(options); + runtimeLogger.info("hello"); + await logging.flush(); + await logging.shutdown(); + } + `; + const childEnv = { ...process.env }; + for (const name of RULE_VARS) delete childEnv[name]; + Object.assign(childEnv, env, { ENDPOINT: endpoint, SERVICE: service, MODE: mode, OPTIONS: JSON.stringify(options) }); + const child = spawn(process.execPath, ["--input-type=module", "-e", script], { + cwd: join(here, ".."), + env: childEnv, + stdio: ["ignore", "inherit", "inherit"], + }); + const code = await new Promise((resolve) => child.on("exit", resolve)); + assert.equal(code, 0); + // shutdown() awaited the exports; poll in case a response raced the exit. + await until(() => { + const got = resourcesOf(service); + return got.logs.length > 0 && (mode !== "server" || got.spans.length > 0); + }, 10_000); + const got = resourcesOf(service); + assert.ok(got.logs.length > 0, "log records exported"); + if (mode === "server") assert.ok(got.spans.length > 0, "spans exported"); + return got; +} + +/** One value of `key` across every resource the service sent (they must agree). */ +function only(resources, key) { + const values = new Set(resources.map((resource) => resource[key])); + assert.equal(values.size, 1, `${key}: ${JSON.stringify([...values])}`); + return [...values][0]; +} + +test("initAutterServer: production outside a test runner, test under one, explicit values win", async () => { + const outside = await run("server"); + assert.equal(only(outside.spans, "deployment.environment"), "production"); + assert.equal(only(outside.logs, "deployment.environment"), "production"); + assert.equal(only(outside.spans, "service.version"), undefined); + assert.equal(only(outside.spans, "deployment.commit"), undefined); + + const nodeTest = await run("server", {}, { NODE_TEST_CONTEXT: "child" }); + assert.equal(only(nodeTest.spans, "deployment.environment"), "test"); + assert.equal(only(nodeTest.logs, "deployment.environment"), "test"); + + const jest = await run("server", {}, { JEST_WORKER_ID: "1", NODE_ENV: "staging" }); + assert.equal(only(jest.spans, "deployment.environment"), "staging", "NODE_ENV wins over the runner"); + + const vitest = await run("server", { environment: "production" }, { VITEST: "true" }); + assert.equal(only(vitest.spans, "deployment.environment"), "production", "the option wins over the runner"); +}); + +test("initAutterServer: the detected commit is deployment.commit and, by default, the release", async () => { + const detected = await run("server", {}, { GITHUB_SHA: SHA }); + assert.equal(only(detected.spans, "deployment.commit"), SHA); + assert.equal(only(detected.spans, "service.version"), SHA); + assert.equal(only(detected.logs, "service.version"), SHA, "log records carry the same release"); + + const explicit = await run("server", { release: "v2" }, { GITHUB_SHA: SHA, AUTTER_RELEASE: "r9" }); + assert.equal(only(explicit.spans, "service.version"), "v2"); + assert.equal(only(explicit.spans, "deployment.commit"), SHA, "the commit is sent whatever the release"); + + const named = await run("server", {}, { GITHUB_SHA: SHA, AUTTER_RELEASE: "r9" }); + assert.equal(only(named.spans, "service.version"), "r9"); + + const off = await run("server", { release: false }, { GIT_SHA: SHORT }); + assert.equal(only(off.spans, "service.version"), undefined); + assert.equal(only(off.logs, "service.version"), undefined); + assert.equal(only(off.spans, "deployment.commit"), SHORT); + + const envOff = await run("server", {}, { GIT_SHA: SHORT, AUTTER_RELEASE: "r9", AUTTER_DETECT_RELEASE: "false" }); + assert.equal(only(envOff.spans, "service.version"), undefined); + assert.equal(only(envOff.spans, "deployment.commit"), SHORT); + + const notCommit = await run("server", {}, { GIT_SHA: "main" }); + assert.equal(only(notCommit.spans, "service.version"), undefined); + assert.equal(only(notCommit.spans, "deployment.commit"), undefined); +}); + +test("initAutterLogging: the same environment and release defaults", async () => { + const outside = await run("logging"); + assert.equal(only(outside.logs, "deployment.environment"), "production"); + assert.equal(only(outside.logs, "service.version"), undefined); + + const vitest = await run("logging", {}, { VITEST: "true", RAILWAY_GIT_COMMIT_SHA: SHA }); + assert.equal(only(vitest.logs, "deployment.environment"), "test"); + assert.equal(only(vitest.logs, "service.version"), SHA); + + const explicit = await run("logging", { environment: "eu", release: "v3" }, { VITEST: "true", GIT_SHA: SHA, NODE_ENV: "development" }); + assert.equal(only(explicit.logs, "deployment.environment"), "eu"); + assert.equal(only(explicit.logs, "service.version"), "v3"); + + const off = await run("logging", { release: false }, { GIT_SHA: SHA }); + assert.equal(only(off.logs, "service.version"), undefined); +}); diff --git a/packages/runtime-node/test/verify.test.mjs b/packages/runtime-node/test/verify.test.mjs index 47b786f..b2cd1d6 100644 --- a/packages/runtime-node/test/verify.test.mjs +++ b/packages/runtime-node/test/verify.test.mjs @@ -18,11 +18,15 @@ import { createIngesterApp } from "../../otlp-ingester/dist/server.js"; const cli = fileURLToPath(new URL("../dist/cli.js", import.meta.url)); const cwd = mkdtempSync(join(tmpdir(), "autter-verify-")); const KEY = `autter_rt_${"5".repeat(48)}`; +// The commit variables the SDK reads (src/deployment.ts), cleared in the +// child unless a test sets them: CI itself sets GITHUB_SHA. +const COMMIT_VARS = ["AUTTER_COMMIT", "GIT_SHA", "GIT_COMMIT", "COMMIT_SHA", "SOURCE_COMMIT", "VERCEL_GIT_COMMIT_SHA", "GITHUB_SHA", + "RENDER_GIT_COMMIT", "RAILWAY_GIT_COMMIT_SHA", "CF_PAGES_COMMIT_SHA", "HEROKU_SLUG_COMMIT", "SOURCE_VERSION"]; function runCli(args, env = {}) { return new Promise((resolve) => { const childEnv = { ...process.env, ...env }; - for (const name of ["AUTTER_RUNTIME_KEY", "AUTTER_ENDPOINT", "OTEL_EXPORTER_OTLP_ENDPOINT", "GIT_SHA", "AUTTER_RELEASE", "GITHUB_SHA", "AUTTER_SERVICE", "OTEL_SERVICE_NAME"]) { + for (const name of ["AUTTER_RUNTIME_KEY", "AUTTER_ENDPOINT", "OTEL_EXPORTER_OTLP_ENDPOINT", "AUTTER_RELEASE", "AUTTER_DETECT_RELEASE", ...COMMIT_VARS, "AUTTER_SERVICE", "OTEL_SERVICE_NAME"]) { if (!(name in env)) delete childEnv[name]; } const child = spawn(process.execPath, [cli, "verify", ...args], { cwd, env: childEnv }); @@ -106,6 +110,33 @@ test("verify: every step passes against a working ingester", async () => { } }); +test("verify: the release defaults like the SDK's (AUTTER_RELEASE, else the commit) and the commit is sent", async () => { + const ing = await ingester(); + const sha = "0123456789abcdef0123456789abcdef01234567"; + try { + const detected = JSON.parse((await runCli(["--endpoint", ing.url, "--key", KEY, "--json"], { RENDER_GIT_COMMIT: sha })).stdout); + assert.equal(detected.release, sha); + assert.equal(detected.commit, sha); + assert.equal(stepOf(detected, "release").status, "pass", JSON.stringify(stepOf(detected, "release"))); + + const named = JSON.parse((await runCli(["--endpoint", ing.url, "--key", KEY, "--json"], { RENDER_GIT_COMMIT: sha, AUTTER_RELEASE: "r9" })).stdout); + assert.equal(named.release, "r9"); + assert.equal(named.commit, sha); + + const off = JSON.parse((await runCli(["--endpoint", ing.url, "--key", KEY, "--json"], { GIT_SHA: sha, AUTTER_DETECT_RELEASE: "false" })).stdout); + assert.equal(off.release, null); + assert.equal(off.commit, sha); + assert.equal(stepOf(off, "release").status, "warn"); + + // Only 7-40 hexadecimal characters are a commit. + const notCommit = JSON.parse((await runCli(["--endpoint", ing.url, "--key", KEY, "--json"], { GIT_SHA: "main" })).stdout); + assert.equal(notCommit.release, null); + assert.equal(notCommit.commit, null); + } finally { + await ing.close(); + } +}); + test("verify: the human report names each step and says it never opens an issue", async () => { const ing = await ingester(); try {