diff --git a/.github/workflows/package-feed.yml b/.github/workflows/package-feed.yml index 0fe973d4f..636375fa1 100644 --- a/.github/workflows/package-feed.yml +++ b/.github/workflows/package-feed.yml @@ -1,6 +1,6 @@ -# Advisory unless "Package Feed Check" is added to required checks in branch protection / rulesets. +# Advisory unless the individual job names are added to required checks in branch protection / rulesets. # A failed check stays red; optional does not mean suppressing policy, network or authentication failures. -name: Package Feed Check +name: Advisory Checks permissions: contents: read @@ -41,3 +41,27 @@ jobs: # No npm ci or secrets: the checker uses only Node built-ins and the no-auth Microsoft proxy. - name: Check package feed policy and availability run: npm run feed:check -- --visibility public + + coda-freshness: + name: CODA Freshness + # Temporarily paused until hosted freshness verification and private-source access are needed. + if: ${{ false }} + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Set up Node.js + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version-file: .nvmrc + package-manager-cache: false + + # Independent optional job, not continue-on-error: stale/unverifiable is red without cancelling other checks. + # Never expose private-source credentials to PR code, including same-repository PRs. + - name: Compare configured CODA branch HEAD and pin + env: + CODA_READ_TOKEN: ${{ github.event_name != 'pull_request' && secrets.CODA_READ_TOKEN || '' }} + run: npm run coda:check diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0a1f1c432..b06299ece 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -33,6 +33,7 @@ another revision; normal builds and unit tests never fetch or repin CODA. npm run fetch-coda npm run test:coda -- --detectors DX-001,DX-011,DX-020 npm run test:coda +npm run coda:check ``` The fetch command requires Git and access to the configured private Azure DevOps repository through your existing @@ -55,11 +56,27 @@ not-selected counts. This report and downloaded files are private, Git-ignored, excluded from VSIX packaging. Do not upload them to public logs or artifacts. Public GitHub Actions execution is refused unless an operator explicitly sets `CODA_TRUSTED_PRIVATE_RUNNER=1` on a genuinely trusted private runner. Ordinary contributors without private-source access can run the infrastructure's synthetic unit tests without CODA. -`npm run test:coda-tools` checks download integrity against disposable local Git repositories. +`npm run test:coda-tools` checks download integrity and freshness relationships against disposable local Git repositories. It runs as an independent yellow `coda-tools` branch alongside magenta Vitest and cyan migration-skill tests in both `npm run vitest` and `npm run vitest:coverage`. Parser/binding regressions do not need private-source access. Public CI against the original contracts requires an approved redistributable upstream bundle. +`npm run coda:check` compares the configured `trackingRef` HEAD with the immutable pin without repinning or touching the +conformance cache. Equality exits 0; a mismatch exits 1 with exact upstream-ahead and upstream-behind commit counts. +Rewound, divergent and unrelated histories are distinguished. Authentication, missing refs/objects and network errors +also fail explicitly as unverifiable. History is fetched into a disposable bare repository, never checked out or executed. + +The existing package-feed workflow has an independent `CODA Freshness` advisory job. It is temporarily disabled for all +hosted triggers with `if: ${{ false }}` until server-side verification and private-source access are needed. The job +appears as skipped, not as verified current; local `npm run coda:check` remains available. +When re-enabled, failures remain red without cancelling the package-feed job or adding dependencies to build/package +workflows. Keep this job optional in branch rulesets; required-check configuration is an owner responsibility and is not +changed by the workflow. +Push/manual runs can use an owner-configured read-only Azure DevOps `CODA_READ_TOKEN` secret. Git credentials also work +locally. **All pull requests receive no private-source token**, including same-repository PRs; without another authorized +credential the private-repository check fails as unverifiable, not as current. This metadata-only job does not run +private scenarios or publish their logs/artifacts. + This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. diff --git a/package.json b/package.json index 87f8d741b..c5fc2d22a 100644 --- a/package.json +++ b/package.json @@ -87,13 +87,14 @@ "l10n": "node l10n/scripts/build.l10n.mjs", "l10n:check": "node l10n/scripts/check.l10n.mjs", "feed:check": "node scripts/check-package-feed.mjs", + "coda:check": "node scripts/check-coda-freshness.mjs", "l10n:auto": "cross-env AZURE_TRANSLATOR_KEY= cross-env AZURE_TRANSLATOR_REGION= npx @vscode/l10n-dev generate-azure --outDir ./l10n ./l10n/bundle.l10n.json ./package.nls.json", "l10n:export": "npx @vscode/l10n-dev generate-xlf ./l10n/bundle.l10n.json --outFile vscode-cosmosdb.xlf", "l10n:import": "npx @vscode/l10n-dev import-xlf ./translations.xlf", "fetch-skill": "node scripts/fetch-skill.mjs", "fetch-coda": "node scripts/coda.mjs fetch", "test:coda": "node scripts/coda.mjs test", - "test:coda-tools": "node --test scripts/coda.test.mjs", + "test:coda-tools": "node --test scripts/coda.test.mjs scripts/check-coda-freshness.test.mjs", "pre-commit": "npm run build && npm run prettier-fix && npm run lint-fix && npm run l10n && npm run vitest", "docker-up": "docker compose up -d", "docker-down": "docker compose down", diff --git a/scripts/check-coda-freshness.mjs b/scripts/check-coda-freshness.mjs new file mode 100644 index 000000000..0ebf85aa9 --- /dev/null +++ b/scripts/check-coda-freshness.mjs @@ -0,0 +1,136 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { spawnSync } from 'node:child_process'; +import { appendFileSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { readCodaPin } from './coda.mjs'; + +const root = fileURLToPath(new URL('../', import.meta.url)); + +export function checkCodaFreshness(pin, { token = process.env.CODA_READ_TOKEN, temporaryRoot = tmpdir() } = {}) { + const env = { ...process.env, GIT_TERMINAL_PROMPT: '0' }; + if (token) { + // Scope authentication to this repository; never store the token in a URL, argument or Git config file. + const index = Number(env.GIT_CONFIG_COUNT ?? 0); + env.GIT_CONFIG_COUNT = String(index + 1); + env[`GIT_CONFIG_KEY_${index}`] = `http.${pin.repo}.extraheader`; + env[`GIT_CONFIG_VALUE_${index}`] = `Authorization: Basic ${Buffer.from(`:${token}`).toString('base64')}`; + } + function git(directory, args, allowMissingAncestor = false) { + const result = spawnSync( + 'git', + [ + '--no-pager', + '--git-dir', + directory, + '-c', + 'credential.interactive=never', + '-c', + 'core.hooksPath=', + ...args, + ], + { + encoding: 'utf8', + env, + windowsHide: true, + timeout: 180_000, + maxBuffer: 8 * 1024 * 1024, + }, + ); + if (allowMissingAncestor && !result.error && result.status === 1) { + return undefined; + } + if (result.error || result.status !== 0) { + throw new Error( + `CODA freshness Git operation failed (${args[0]}): ${result.error?.message ?? result.stderr.trim()}`, + ); + } + return result.stdout.trim(); + } + const directory = mkdtempSync(path.join(temporaryRoot, 'coda-freshness-')); + try { + git(directory, ['init', '--quiet', '--bare']); + const remote = git(directory, ['ls-remote', '--exit-code', pin.repo, pin.trackingRef]); + const lines = remote.split(/\r?\n/); + if (lines.length !== 1) { + throw new Error('CODA tracking ref must resolve to exactly one branch HEAD.'); + } + const [head, ref] = lines[0].split(/\s+/); + if (!/^[a-f0-9]{40}$/.test(head) || ref !== pin.trackingRef) { + throw new Error('CODA returned an invalid tracking ref or HEAD.'); + } + if (head === pin.commit) { + return { pin: pin.commit, head, ref, ahead: 0, behind: 0, relationship: 'equal' }; + } + // Fetch immutable objects after resolving HEAD, without a working checkout or a shallow-history count. + git(directory, ['fetch', '--quiet', '--no-tags', '--filter=tree:0', pin.repo, pin.commit, head]); + const counts = git(directory, ['rev-list', '--left-right', '--count', `${pin.commit}...${head}`]).split(/\s+/); + const [behind, ahead] = counts.map(Number); + if ( + counts.length !== 2 || + !Number.isSafeInteger(ahead) || + !Number.isSafeInteger(behind) || + ahead < 0 || + behind < 0 + ) { + throw new Error('CODA returned invalid ahead/behind counts.'); + } + const ancestor = git(directory, ['merge-base', pin.commit, head], true); + const relationship = !ancestor ? 'unrelated' : behind === 0 ? 'ahead' : ahead === 0 ? 'behind' : 'diverged'; + return { pin: pin.commit, head, ref, ahead, behind, relationship }; + } finally { + rmSync(directory, { recursive: true, force: true }); + } +} + +export function formatFreshness(result) { + return [ + `# CODA Freshness: ${result.relationship === 'equal' ? 'PASSED' : 'FAILED'}`, + '', + `Tracking ref: \`${result.ref}\``, + `Pinned commit: \`${result.pin}\``, + `Upstream HEAD: \`${result.head}\``, + '', + `Upstream HEAD is **${result.ahead} commits ahead** and **${result.behind} commits behind** the pin (${result.relationship}).`, + '', + 'Read-only advisory: no pin, working checkout or cached conformance source was changed.', + '', + ].join('\n'); +} + +export function main(args = process.argv.slice(2), configuredPin, env = process.env) { + if (args.length > 0) { + throw new Error('Usage: npm run coda:check'); + } + const pin = configuredPin ?? readCodaPin(JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8'))); + const result = checkCodaFreshness(pin, { token: env.CODA_READ_TOKEN ?? '' }); + const report = formatFreshness(result); + console.log(report); + if (env.GITHUB_STEP_SUMMARY) { + appendFileSync(env.GITHUB_STEP_SUMMARY, report); + } + if (result.relationship !== 'equal' && env.GITHUB_ACTIONS === 'true') { + console.error( + '::error::CODA branch HEAD differs from the pin. See the ahead/behind counts in the job summary.', + ); + } + return result.relationship === 'equal' ? 0 : 1; +} + +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + try { + process.exitCode = main(); + } catch (error) { + const message = `CODA freshness is unverifiable: ${error instanceof Error ? error.message : String(error)}`; + console.error(message); + if (process.env.GITHUB_STEP_SUMMARY) { + appendFileSync(process.env.GITHUB_STEP_SUMMARY, `# CODA Freshness: FAILED\n\n${message}\n`); + } + process.exitCode = 1; + } +} diff --git a/scripts/check-coda-freshness.test.mjs b/scripts/check-coda-freshness.test.mjs new file mode 100644 index 000000000..4d5ff2329 --- /dev/null +++ b/scripts/check-coda-freshness.test.mjs @@ -0,0 +1,176 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; +import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { test } from 'node:test'; +import { checkCodaFreshness, formatFreshness, main } from './check-coda-freshness.mjs'; + +function git(directory, ...args) { + const result = spawnSync('git', ['--no-pager', '-C', directory, '-c', 'core.hooksPath=', ...args], { + encoding: 'utf8', + windowsHide: true, + }); + assert.equal(result.status, 0, result.stderr); + return result.stdout.trim(); +} + +function commit(source, message) { + git( + source, + '-c', + 'user.name=Fixture', + '-c', + 'user.email=fixture@example.test', + '-c', + 'commit.gpgsign=false', + 'commit', + '--quiet', + '--allow-empty', + '-m', + message, + ); + return git(source, 'rev-parse', 'HEAD'); +} + +function fixture(t) { + const directory = mkdtempSync(path.join(tmpdir(), 'coda-freshness-test-')); + const source = path.join(directory, 'source'); + const temporaryRoot = path.join(directory, 'scratch'); + mkdirSync(source); + mkdirSync(temporaryRoot); + t.after(() => { + try { + assert.deepEqual(readdirSync(temporaryRoot), [], 'Every temporary graph must be cleaned up.'); + } finally { + rmSync(directory, { recursive: true, force: true }); + } + }); + git(source, 'init', '--quiet', '--initial-branch=main'); + const pin = { + repo: source, + trackingRef: 'refs/heads/main', + commit: commit(source, 'initial'), + featurePath: 'specs/detectors', + }; + const options = { temporaryRoot, token: '' }; + return { source, pin, options }; +} + +test('equal HEAD passes without changing the configured pin, repository or cached source', (t) => { + const { source, pin, options } = fixture(t); + const original = JSON.stringify(pin); + writeFileSync(path.join(source, 'local-cache-sentinel'), 'keep local work'); + const status = git(source, 'status', '--porcelain'); + assert.deepEqual(checkCodaFreshness(pin, options), { + pin: pin.commit, + head: pin.commit, + ref: pin.trackingRef, + ahead: 0, + behind: 0, + relationship: 'equal', + }); + assert.equal(JSON.stringify(pin), original); + assert.equal(git(source, 'status', '--porcelain'), status); + assert.equal(readFileSync(path.join(source, 'local-cache-sentinel'), 'utf8'), 'keep local work'); +}); + +test('counts every upstream-only commit, not a shallow fetch depth', (t) => { + const { source, pin, options } = fixture(t); + commit(source, 'next'); + const head = commit(source, 'latest'); + const result = checkCodaFreshness(pin, options); + assert.deepEqual(result, { + pin: pin.commit, + head, + ref: pin.trackingRef, + ahead: 2, + behind: 0, + relationship: 'ahead', + }); + assert.match(formatFreshness(result), /2 commits ahead.*0 commits behind/); +}); + +test('reports an upstream branch behind the pinned commit', (t) => { + const { source, pin, options } = fixture(t); + git(source, 'checkout', '--quiet', '-b', 'future-pin'); + commit(source, 'newer'); + const future = commit(source, 'newest'); + const result = checkCodaFreshness({ ...pin, commit: future }, options); + assert.equal(result.relationship, 'behind'); + assert.equal(result.ahead, 0); + assert.equal(result.behind, 2); +}); + +test('distinguishes divergent branches and reports both exact counts', (t) => { + const { source, pin, options } = fixture(t); + git(source, 'checkout', '--quiet', '-b', 'pin-side'); + const pinned = commit(source, 'pin-only'); + git(source, 'checkout', '--quiet', 'main'); + commit(source, 'upstream-only-1'); + commit(source, 'upstream-only-2'); + const result = checkCodaFreshness({ ...pin, commit: pinned }, options); + assert.equal(result.relationship, 'diverged'); + assert.equal(result.ahead, 2); + assert.equal(result.behind, 1); +}); + +test('does not describe unrelated histories as an ordinary ahead update', (t) => { + const { source, pin, options } = fixture(t); + git(source, 'checkout', '--quiet', '--orphan', 'replacement'); + const head = commit(source, 'new root'); + const result = checkCodaFreshness({ ...pin, trackingRef: 'refs/heads/replacement' }, options); + assert.equal(result.head, head); + assert.equal(result.relationship, 'unrelated'); + assert.equal(result.ahead, 1); + assert.equal(result.behind, 1); +}); + +test('missing refs and unavailable pinned objects fail rather than passing or repinning', (t) => { + const { pin, options } = fixture(t); + assert.throws(() => checkCodaFreshness({ ...pin, trackingRef: 'refs/heads/missing' }, options), /operation failed/); + assert.throws(() => checkCodaFreshness({ ...pin, commit: 'f'.repeat(40) }, options), /operation failed/); + assert.throws( + () => checkCodaFreshness({ ...pin, repo: path.join(pin.repo, 'missing-repository') }, options), + /operation failed/, + ); +}); + +test('credentials are absent from public reports and local repository configuration', (t) => { + const { source, pin, options } = fixture(t); + commit(source, 'upstream'); + const token = 'synthetic-sensitive-value'; + const result = checkCodaFreshness(pin, { ...options, token }); + assert.doesNotMatch(formatFreshness(result), /synthetic-sensitive-value|Authorization|Basic/); + assert.doesNotMatch( + readFileSync(path.join(source, '.git', 'config'), 'utf8'), + /synthetic-sensitive-value|extraheader/, + ); +}); + +test('command returns nonzero on mismatch and zero only on equality', (t) => { + const { source, pin } = fixture(t); + t.mock.method(console, 'log', () => {}); + assert.equal(main([], pin, {}), 0); + commit(source, 'upstream'); + assert.equal(main([], pin, {}), 1); + assert.throws(() => main(['--repin'], pin), /Usage/); +}); + +test('hosted freshness is paused and independent, with private credentials withheld from every PR', () => { + const workflow = readFileSync(new URL('../.github/workflows/package-feed.yml', import.meta.url), 'utf8'); + const job = workflow.split('\n coda-freshness:\n')[1]; + assert.ok(job, 'The checker must have its own sibling job.'); + assert.match(workflow, /\n package-feed:\n/); + assert.match(job, /^ if: \$\{\{ false \}\}$/m); + assert.doesNotMatch(job, /^\s+(?:needs|continue-on-error|strategy):/m); + assert.match(job, /persist-credentials: false/); + assert.match(job, /github\.event_name != 'pull_request' && secrets\.CODA_READ_TOKEN/); + assert.match(job, /run: npm run coda:check/); + assert.doesNotMatch(job, /pull_request_target|npm ci|test:coda|upload-artifact/); +}); diff --git a/specs/account-overview.md b/specs/account-overview.md index 2d4254281..79b9b75b4 100644 --- a/specs/account-overview.md +++ b/specs/account-overview.md @@ -552,7 +552,12 @@ Apply this selection before database/container/operation filters. Fan-out, inges shared-pool queries use the same selection policy; different tables may select different workspaces. Selection runs inside each KQL query without additional HTTP requests. It avoids counting diagnostic copies together, but row counts do not prove complete collection or visibility into every destination. -Schema-only probes remain independent and do not select a destination. +Table-presence probes remain independent and do not select a destination. Each applies the same explicit +half-open analysis window as its downstream queries, takes at most one row, and returns only a validated +zero/one count. Aggregate probes use the same five-minute-aligned boundaries as aggregate analysis. +An accessible table with no rows skips only that table's dependent queries, without reporting a service failure, +falling back to another source or claiming a healthy workload. Query-runtime structural checks still run when +query-runtime data exists but request logs are empty. Request-log acquisition prefers `CDBDataPlaneRequests5M`, with `CDBDataPlaneRequests` as compatibility source only when the aggregate table is explicitly missing. A denied, malformed, partial or failed aggregate query is not @@ -566,11 +571,13 @@ counts, invalid RU and misaligned source buckets invalidate the affected check, partial coverage. Successful all-zero totals remain distinct from invalid aggregates. Burst evidence uses observed five-minute write buckets for aggregates and one-minute write buckets for raw events, without filling missing buckets with zero. Findings retain source table/resolution and absolute bounds. -For a window without write buckets, Kusto's `NaN` average is normalized to null before validation and the burst -factor is zero. Nonempty invalid burst measurements and invalid source-row counts remain failures. +For a window without write buckets, Kusto's `NaN` average (serialized as `"NaN"`) is normalized to null before +validation and the burst factor is zero. This requires a null peak and zero invalid source-row count. +Nonempty invalid burst measurements and invalid source-row counts remain failures. DX-002/007 keep their query-runtime-only structural decision. Raw data-plane logs optionally enrich query RU. Charge reads start only after nonempty structural observations, without a separate raw-table probe. +An already selected raw source with no rows skips optional charge reads as well. Each table selects one destination over the same absolute window; distinct logical-query/activity/shape tuples join charges pre-aggregated by activity ID. Mean logical-query RU is exposed only when every execution and its activities matched valid charges. Runtime rows with missing activity IDs stay on the left side of the @@ -586,6 +593,7 @@ DX-016 optionally observes only live-validated `Read`, `Query` and `QueryPlan` o case-insensitive account-resource predicate and the selected request-log window/source. Invalid or duplicate operation aggregates are failures, not zero observations. Successful empty results record `noObservations`; failed/unavailable enrichment records `unavailable`, while the ARM configuration finding stays visible. +An empty successful source probe also records `noObservations` without an additional read-charge query. Observed read RU and half that amount as a theoretical upper bound are separate evidence: diagnostic logs do not identify per-request consistency overrides. Neither value establishes measured or monetary savings. @@ -600,6 +608,12 @@ cost/usage and positive totals. Signed billing adjustments participate in those Pagination is rejected rather than treating a first page as a complete rate. The native client reuses the authenticated ARM pipeline and configured cloud endpoint, including bounded SDK retries; it does not acquire credentials, add dependencies or change role assignments. +Cost Management HTTP 429 responses also honor the service's +`x-ms-ratelimit-microsoft.costmanagement-{qpu,entity,tenant,clienttype}-retry-after` headers, in seconds. +A response adapter supplies the longest valid service/standard delay to that same SDK retry policy, including +standard HTTP-date delays, without adding a retry loop or changing its attempt limits or cancellation. +Empty, invalid, negative and timer-overflowing service delays do not create an immediate retry; unhandled or +exhausted throttling remains a categorized cost-coverage failure. Other ARM providers are unchanged. The blended rate is `(amortizedCost / billed100RuHours) / 100 * 730`; the estimate is the detector's positive recoverable RU/s times that rate, matching the pinned CODA implementation. No additional regional, @@ -620,11 +634,22 @@ Only the public Azure cloud is supported for this source. Every configured accou unambiguously through subscription location metadata, and ARM container inventory must have completed. Accepted region aliases resolve separately to the canonical retail key and the Monitor display name. Storage filters, dimension validation and provenance use the resolved display name, not the caller's alias. -`DataUsageV2` (`Total`, bytes, five-minute grain, last 24 hours) is read per known logical container in one -reference region. The newest complete common timestamp must contain every inventory container exactly once, +`DataUsageV2` (`Total`, bytes, five-minute grain, last 24 hours) is read for one reference region, +with exact current-inventory database/container filters. Requests group containers within each database, +with at most twenty containers and a 4,096-character URL-encoded filter per request; the series limit equals +that batch's inventory size rather than Azure Monitor's default ten. This excludes historical deleted-container +series before limiting results, without introducing cross-product identities across databases. +Wildcard identities or a single identity exceeding the filter budget are unsupported, not broad wildcard reads. +Returned identities must belong to the requested batch; missing or truncated series never establish a partial +account total or trigger pricing. Snapshot completeness is checked across all batches. +Inventory-filtered requests run in waves of at most five concurrent reads, not serial round trips per database. +A failed wave prevents subsequent waves and pricing; successful reads in that wave never establish a partial total. +The newest complete common timestamp must contain every inventory container exactly once, with correct resource/region dimensions, finite nonnegative bytes, age at most six hours and future skew at most five minutes. Missing/duplicate series and partial inventories never become zero storage. Observed zero bytes are valid. No time buckets or replicated-region storage copies are added together. +Monitor dimension values are matched case-insensitively to ARM database/container names. +Inventories with identities that collide after case folding are rejected rather than double-counted. Unauthenticated Azure Retail Prices requests accept no redirects and follow only bounded, non-looping pagination on the fixed HTTPS endpoint. Every configured region needs exactly one current primary consumption @@ -644,6 +669,12 @@ Any future billing-model correction must be coordinated with CODA's versioned co the extension does not independently reinterpret the pinned model. Denied inventory retains `backup/accessDenied` rather than becoming missing storage data. Unsupported-cost guidance covers both account API restrictions and cloud restrictions. +All optional cost reads share a 20-second cancellable deadline, including SDK retry waits, storage waves and +retail-price pagination. A server retry delay longer than the remaining budget cancels enrichment rather than +retrying early or blocking qualitative findings. Completed independent estimates remain available; unfinished +operations report timeout coverage. +Cleanup aborts the owned signal before removing the timer, including early failed waves, so outstanding siblings cannot +outlive enrichment. It does not abort the caller's controller. Authentication, access denial, rate limiting, timeouts, service/query errors, invalid/incomplete evidence, unsupported clouds and no usable data stay distinguishable without forwarding raw service errors or billing rows. Missing access or data means no monetary figure, never fabricated zero. No billing query runs without @@ -862,6 +893,13 @@ billable savings or permission to remove regions. Extra-region capacity is only finding; legitimate HA has no removable extra-region capacity. Autoscale, serverless and incomplete inventories are not quantified. MongoDB metadata support in this pure configuration evaluation does not introduce a MongoDB provider. +`npm run coda:check` independently compares the tracked branch HEAD and pin, reports exact ahead/behind counts including +rewound/divergent/unrelated histories, and exits nonzero for mismatch or unverifiable source access. It never repins. +The package-feed workflow defines a separate advisory job without build dependencies or `continue-on-error`; +that hosted job is currently disabled. The local command remains available. +Private read credentials are withheld from every PR; owner-managed push/manual credentials and optional branch-rule +configuration are required separately. Freshness equality is not a conformance result. + **Adoption limits:** native collection and computation remain separate; there is no CODA MCP/Python dependency. Aggregate request-log support, complete per-rule/per-scope evaluation outcomes, other upstream enrichment and approved public contract distribution remain separate work. Private upstream files are not vendored into tracked source. diff --git a/src/panels/accountOverview/services/advisories/collect.ts b/src/panels/accountOverview/services/advisories/collect.ts index c03ca5059..d23afaaf2 100644 --- a/src/panels/accountOverview/services/advisories/collect.ts +++ b/src/panels/accountOverview/services/advisories/collect.ts @@ -54,6 +54,7 @@ import { fetchUncontrolledIngestion, logWindow, probeCdbLogs, + requestLogEvidence, requestLogWindow, selectDataPlaneSource, type LogsQueryExecutor, @@ -817,11 +818,23 @@ export async function collectTier2Logs( const now = Date.now(); const timespan = logWindow(now); - const [dataPlaneSource, queryRuntime] = await Promise.all([ + const [dataPlane, queryRuntime] = await Promise.all([ selectDataPlaneSource(logsClient, accountId, timespan, report), probeCdbLogs(logsClient, accountId, timespan, { onDiagnostic: report, table: 'CDBQueryRuntimeStatistics' }), ]); + const dataPlaneSource = dataPlane?.source; + const hasRequestData = dataPlane?.hasData === true; + const hasQueryData = queryRuntime.available && queryRuntime.hasData; const dataPlaneTimespan = dataPlaneSource ? requestLogWindow(dataPlaneSource, now) : timespan; + const emptyReadCharge: ConsistencyReadRuInput | undefined = + includeConsistencyReadRu && dataPlaneSource && !hasRequestData + ? { + ...requestLogEvidence(dataPlaneSource, dataPlaneTimespan), + status: 'noObservations', + readRequests: 0, + readRu: 0, + } + : undefined; const sharedDatabases = new Map(); for (const row of rows) { @@ -834,7 +847,7 @@ export async function collectTier2Logs( } const [crossPartitionResults, ingestionResults, sharedResults, consistencyReadRu] = await Promise.all([ - mapInBatches(queryRuntime.available ? scanTargets : [], (row) => + mapInBatches(hasQueryData ? scanTargets : [], (row) => fetchCrossPartitionShapes( logsClient, accountId, @@ -842,10 +855,10 @@ export async function collectTier2Logs( row.containerId, timespan, report, - true, + dataPlaneSource !== 'CDBDataPlaneRequests' || hasRequestData, ).catch(() => undefined), ), - mapInBatches(dataPlaneSource ? ingestionTargets : [], (row) => + mapInBatches(hasRequestData ? ingestionTargets : [], (row) => fetchUncontrolledIngestion( logsClient, accountId, @@ -856,7 +869,7 @@ export async function collectTier2Logs( dataPlaneSource, ).catch(() => undefined), ), - mapInBatches(dataPlaneSource ? [...sharedDatabases] : [], ([databaseId, sharedRu]) => + mapInBatches(hasRequestData ? [...sharedDatabases] : [], ([databaseId, sharedRu]) => fetchSharedThroughputTraffic( logsClient, accountId, @@ -867,11 +880,11 @@ export async function collectTier2Logs( dataPlaneSource, ).catch(() => undefined), ), - includeConsistencyReadRu && dataPlaneSource + includeConsistencyReadRu && dataPlaneSource && hasRequestData ? fetchConsistencyReadRu(logsClient, accountId, dataPlaneTimespan, report, dataPlaneSource).catch( () => undefined, ) - : undefined, + : emptyReadCharge, ]); return { diff --git a/src/panels/accountOverview/services/advisories/collectLogs.test.ts b/src/panels/accountOverview/services/advisories/collectLogs.test.ts index 137116dbd..b6e7839ea 100644 --- a/src/panels/accountOverview/services/advisories/collectLogs.test.ts +++ b/src/panels/accountOverview/services/advisories/collectLogs.test.ts @@ -23,18 +23,21 @@ function success(columns: string[] = [], rows: LogsTable['rows'] = []): LogsQuer }; } -function client(fail?: (query: string) => unknown): LogsQueryExecutor { +function client(fail?: (query: string) => unknown, hasData = (_query: string) => true): LogsQueryExecutor { return { queryResource: vi.fn(async (_id, query) => { const error = fail?.(query); if (error) { throw Object.assign(new Error('Simulated Log Analytics failure'), error); } - if (query === 'CDBDataPlaneRequests5M | take 0') { + if (query.startsWith('CDBDataPlaneRequests5M ') && query.endsWith('| take 1 | summarize n = count()')) { throw Object.assign(new Error('Failed to resolve table CDBDataPlaneRequests5M'), { code: 'SemanticError', }); } + if (query.endsWith('| take 1 | summarize n = count()')) { + return success(['n'], [[hasData(query) ? 1 : 0]]); + } if (query.includes('totalQueryRu')) { return success( ['QueryText', 'executions', 'matchedExecutions', 'totalQueryRu'], @@ -136,10 +139,45 @@ describe('Log Analytics collection coverage', () => { ); }); + it('uses only two presence probes when both analysis-window sources are empty, regardless of container count', async () => { + const queryResource = vi.fn(async (_id, query) => + query.endsWith('| take 1 | summarize n = count()') ? success(['n'], [[0]]) : success(), + ); + const containers = Array.from({ length: 5 }, (_, index) => ({ + databaseId: 'db', + containerId: `container${index}`, + })); + const report = vi.fn(); + const result = await collectTier2Logs( + { queryResource }, + resource, + containers, + inventory, + report, + containers, + containers, + true, + ); + expect(queryResource).toHaveBeenCalledTimes(2); + expect(result).toMatchObject({ + crossPartition: [], + ingestion: [], + sharedThroughput: [], + consistencyReadRu: { + status: 'noObservations', + readRequests: 0, + readRu: 0, + sourceTable: 'CDBDataPlaneRequests5M', + }, + logSource: { available: true }, + }); + expect(report).not.toHaveBeenCalled(); + }); + it('uses one aligned aggregate source for ingestion, shared-pool and consistency evidence, with raw-only fan-out enrichment', async () => { const queryResource = vi.fn(async (_id, query) => { - if (query.endsWith('| take 0')) { - return success(); + if (query.endsWith('| take 1 | summarize n = count()')) { + return success(['n'], [[1]]); } if (query.includes('totalQueryRu')) { return success( @@ -187,7 +225,8 @@ describe('Log Analytics collection coverage', () => { readRu: 100, }); const aggregateCalls = queryResource.mock.calls.filter( - (call) => call[1].startsWith('CDBDataPlaneRequests5M ') && !call[1].endsWith('| take 0'), + (call) => + call[1].startsWith('CDBDataPlaneRequests5M ') && !call[1].endsWith('| take 1 | summarize n = count()'), ); expect(aggregateCalls).toHaveLength(5); for (const [, query, span] of aggregateCalls) { @@ -198,6 +237,119 @@ describe('Log Analytics collection coverage', () => { } }); + it.each(['CDBDataPlaneRequests5M', 'CDBDataPlaneRequests'] as const)( + 'preserves query-runtime checks independently of an empty %s source', + async (source) => { + const queryResource = vi.fn(async (_id, query) => { + if (query.endsWith('| take 1 | summarize n = count()')) { + if (source === 'CDBDataPlaneRequests' && query.startsWith('CDBDataPlaneRequests5M ')) { + throw Object.assign(new Error('Failed to resolve table CDBDataPlaneRequests5M'), { + code: 'SemanticError', + }); + } + return success( + ['n'], + [ + [ + query.startsWith('CDBQueryRuntimeStatistics ') || + (source === 'CDBDataPlaneRequests5M' && query.startsWith('CDBDataPlaneRequests ')) + ? 1 + : 0, + ], + ], + ); + } + expect(query).toMatch(/^CDBQueryRuntimeStatistics /); + if (query.includes('totalQueryRu')) { + return success( + ['QueryText', 'executions', 'matchedExecutions', 'totalQueryRu'], + [['SELECT 1', 100_000, 100_000, 2_000_000]], + ); + } + return success( + ['QueryText', 'executions', 'avgFanout', 'maxFanout'], + [['SELECT 1', 100_000, 100, 100]], + ); + }); + const report = vi.fn(); + const result = await collectTier2Logs( + { queryResource }, + resource, + targets, + inventory, + report, + targets, + targets, + true, + ); + expect(queryResource).toHaveBeenCalledTimes(4); + expect(result.crossPartition).toHaveLength(1); + expect(result.crossPartition[0].shapes).toEqual([ + { + text: 'SELECT 1', + executions: 100_000, + avgFanout: 100, + maxFanout: 100, + ...(source === 'CDBDataPlaneRequests5M' ? { avgRu: 20 } : {}), + }, + ]); + expect(queryResource.mock.calls.filter((call) => call[1].includes('totalQueryRu'))).toHaveLength( + source === 'CDBDataPlaneRequests5M' ? 1 : 0, + ); + expect( + queryResource.mock.calls.filter( + (call) => + call[1].startsWith('CDBDataPlaneRequests ') && + call[1].endsWith('| take 1 | summarize n = count()'), + ), + ).toHaveLength(source === 'CDBDataPlaneRequests5M' ? 0 : 1); + expect(result.ingestion).toEqual([]); + expect(result.sharedThroughput).toEqual([]); + expect(result.consistencyReadRu).toMatchObject({ sourceTable: source, status: 'noObservations' }); + expect(result.logSource).toEqual({ available: true }); + expect(report).not.toHaveBeenCalled(); + const findings = computeDerivedAdvisories( + { + weeklyRuPercents: [], + hasManualThroughput: false, + partitions: [], + storage: [], + indexing: [], + crossPartition: result.crossPartition, + consistencyReadRu: result.consistencyReadRu, + accountConfig: { + accountName: 'prod', + consistencyLevel: 'Strong', + regionCount: 2, + multiRegionWritesEnabled: false, + writeRegionCount: 1, + api: API.Core, + }, + }, + DEFAULT_ADVISORY_THRESHOLDS, + ); + expect(findings).toEqual( + expect.arrayContaining([ + expect.objectContaining({ rule: 'CrossPartitionQuery' }), + expect.objectContaining({ + rule: 'ExpensiveConsistency', + evidence: expect.objectContaining({ readRuStatus: 'noObservations' }), + }), + ]), + ); + }, + ); + + it('skips empty query-runtime checks without suppressing independent raw request checks', async () => { + const executor = client(undefined, (query) => !query.startsWith('CDBQueryRuntimeStatistics ')); + const result = await collectTier2Logs(executor, resource, targets, inventory); + expect(executor.queryResource).toHaveBeenCalledTimes(7); + expect(result.crossPartition).toEqual([]); + expect(result.ingestion).toHaveLength(1); + expect(result.sharedThroughput).toHaveLength(1); + expect(result.logSource).toEqual({ available: true }); + }); + it('retains structural and config findings when optional query/read RU fails, and reports both failures', async () => { const executor = client((query) => query.includes('totalQueryRu') || query.includes('by OperationName') @@ -293,7 +445,7 @@ describe('Log Analytics collection coverage', () => { }); it('keeps successful empty tables distinct from failures', async () => { - const executor: LogsQueryExecutor = { queryResource: async () => success() }; + const executor: LogsQueryExecutor = { queryResource: async () => success(['n'], [[0]]) }; const report = vi.fn(); const result = await collectTier2Logs(executor, resource, targets, inventory, report); expect(result.logSource).toEqual({ available: true }); diff --git a/src/panels/accountOverview/services/advisories/collectParity.test.ts b/src/panels/accountOverview/services/advisories/collectParity.test.ts index 023a12b71..002f6b626 100644 --- a/src/panels/accountOverview/services/advisories/collectParity.test.ts +++ b/src/panels/accountOverview/services/advisories/collectParity.test.ts @@ -87,7 +87,7 @@ import { beforeEach(() => { vi.mocked(getAccountRequestStatusCounts).mockResolvedValue({ 200: 1000 }); - vi.mocked(selectDataPlaneSource).mockResolvedValue('CDBDataPlaneRequests'); + vi.mocked(selectDataPlaneSource).mockResolvedValue({ source: 'CDBDataPlaneRequests', hasData: true }); vi.mocked(probeCdbLogs).mockResolvedValue({ available: false, reason: 'noData' }); }); @@ -1276,7 +1276,7 @@ describe('collectDerivedAdvisories parity wiring', () => { peakRuPerSec: 100, sampleCount: 50, }); - vi.mocked(probeCdbLogs).mockResolvedValue({ available: true }); + vi.mocked(probeCdbLogs).mockResolvedValue({ available: true, hasData: true }); vi.mocked(fetchCrossPartitionShapes).mockImplementation(async (_logs, _account, databaseId, containerId) => ({ databaseId, containerId, @@ -1418,7 +1418,7 @@ describe('collectDerivedAdvisories parity wiring', () => { peakRuPerSec: 100, sampleCount: 50, }); - vi.mocked(probeCdbLogs).mockResolvedValue({ available: true }); + vi.mocked(probeCdbLogs).mockResolvedValue({ available: true, hasData: true }); vi.mocked(fetchCrossPartitionShapes).mockResolvedValue({ databaseId: 'db', containerId: 'c', @@ -1459,7 +1459,7 @@ describe('collectDerivedAdvisories parity wiring', () => { peakRuPerSec: 20, sampleCount: 30, }); - vi.mocked(probeCdbLogs).mockResolvedValue({ available: true }); + vi.mocked(probeCdbLogs).mockResolvedValue({ available: true, hasData: true }); vi.mocked(fetchCrossPartitionShapes).mockResolvedValue({ databaseId: 'db', containerId: 'c', diff --git a/src/panels/accountOverview/services/advisories/costEstimates.test.ts b/src/panels/accountOverview/services/advisories/costEstimates.test.ts index d03888f3c..6a1dc648f 100644 --- a/src/panels/accountOverview/services/advisories/costEstimates.test.ts +++ b/src/panels/accountOverview/services/advisories/costEstimates.test.ts @@ -7,6 +7,7 @@ import { CosmosDBManagementClient } from '@azure/arm-cosmosdb'; import { MonitorClient } from '@azure/arm-monitor'; import { createHttpHeaders } from '@azure/core-rest-pipeline'; import { afterEach, describe, expect, it, vi } from 'vitest'; +import { type InventoryContainerRow } from '../inventory'; import { type DerivedAdvisory, type DerivedAdvisoryRule } from './core/types'; import { enrichAdvisoryCosts, recoverableCapacity } from './costEstimates'; import { type CostContext } from './fetchers/costManagement'; @@ -54,6 +55,8 @@ function setup(currency = 'EUR', cost = 8, usage = 1000) { })); return { send, + cosmosClient, + monitorClient, enrich: (advisories: DerivedAdvisory[], costContext: CostContext | undefined = context) => enrichAdvisoryCosts({ advisories, @@ -70,6 +73,71 @@ function setup(currency = 'EUR', cost = 8, usage = 1000) { afterEach(() => vi.restoreAllMocks()); describe('optional financial enrichment', () => { + it('aborts pending storage siblings when one request fails before the deadline', async () => { + vi.useFakeTimers(); + const caller = new AbortController(); + try { + const { cosmosClient, monitorClient, send } = setup(); + send.mockImplementation(async (_http, request) => ({ + request, + status: 200, + headers: createHttpHeaders(), + bodyAsText: JSON.stringify({ value: [{ name: 'eastus', displayName: 'East US' }] }), + })); + const cancelled = vi.fn(); + const list = vi.spyOn(monitorClient.metrics, 'list').mockImplementation(async (_id, options) => { + const signal = options?.abortSignal; + if (!signal) { + throw new Error('Expected the shared enrichment cancellation signal.'); + } + if (options?.filter?.includes("DatabaseName eq 'first'")) { + throw Object.assign(new Error('Storage access denied.'), { statusCode: 403 }); + } + return new Promise((_resolve, reject) => { + signal.addEventListener( + 'abort', + () => { + cancelled(); + reject(Object.assign(new Error('Storage request aborted.'), { name: 'AbortError' })); + }, + { once: true }, + ); + }); + }); + const finding = advisory('PaidContinuousBackup'); + const result = await enrichAdvisoryCosts({ + advisories: [finding], + cosmosClient, + monitorClient, + context: { ...context, abortSignal: caller.signal }, + accountId: + '/subscriptions/sub/resourceGroups/rg/providers/Microsoft.DocumentDB/databaseAccounts/account', + inventoryRows: ['first', 'second'].map((databaseId) => ({ + databaseId, + containerId: 'items', + throughputMode: 'dedicated', + partitionKeyPaths: [], + indexingMode: 'consistent', + excludedPathCount: 0, + userExcludedPathCount: 0, + compositeIndexCount: 0, + health: 'Healthy', + })), + now, + }); + expect(result).toEqual({ available: false, failures: [{ operation: 'backup', kind: 'accessDenied' }] }); + expect(list).toHaveBeenCalledTimes(2); + expect(list.mock.calls.every(([, options]) => options?.abortSignal?.aborted)).toBe(true); + expect(cancelled).toHaveBeenCalledOnce(); + expect(caller.signal.aborted).toBe(false); + expect(finding.costEstimate).toBeUndefined(); + expect(vi.getTimerCount()).toBe(0); + } finally { + caller.abort(); + vi.useRealTimers(); + } + }); + it.each([ { cost: 50, usage: 1, recoverableRu: 0.125, monthlyAmount: 45.62 }, { cost: 50, usage: 1, recoverableRu: 0.375, monthlyAmount: 136.88 }, diff --git a/src/panels/accountOverview/services/advisories/costEstimates.ts b/src/panels/accountOverview/services/advisories/costEstimates.ts index 94fd2bf3b..80f0ee9e6 100644 --- a/src/panels/accountOverview/services/advisories/costEstimates.ts +++ b/src/panels/accountOverview/services/advisories/costEstimates.ts @@ -37,8 +37,7 @@ export function recoverableCapacity(advisory: DerivedAdvisory): number | undefin return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : undefined; } -/** Enrichment follows supersession. Independent read failures retain every qualitative/capacity finding. */ -export async function enrichAdvisoryCosts(params: { +interface CostEnrichmentParams { advisories: DerivedAdvisory[]; cosmosClient: CosmosDBManagementClient; monitorClient: MonitorClient; @@ -47,7 +46,28 @@ export async function enrichAdvisoryCosts(params: { inventoryRows?: readonly InventoryContainerRow[]; inventoryFailureReason?: UnavailableReason; now: number; -}): Promise { +} + +/** One deadline includes all optional requests and SDK retry waits; cancellation never retries ahead of the server. */ +export async function enrichAdvisoryCosts(params: CostEnrichmentParams): Promise { + const deadline = new AbortController(); + const timer = setTimeout(() => deadline.abort(), 20_000); + try { + const abortSignal = params.context?.abortSignal + ? AbortSignal.any([params.context.abortSignal, deadline.signal]) + : deadline.signal; + return await collectAdvisoryCosts({ + ...params, + context: params.context && { ...params.context, abortSignal }, + }); + } finally { + deadline.abort(); + clearTimeout(timer); + } +} + +/** Enrichment follows supersession. Independent read failures retain every qualitative/capacity finding. */ +async function collectAdvisoryCosts(params: CostEnrichmentParams): Promise { const { advisories, cosmosClient, monitorClient, context, accountId, inventoryRows, now } = params; const capacity = advisories.flatMap((advisory) => { const ru = recoverableCapacity(advisory); diff --git a/src/panels/accountOverview/services/advisories/fetchers/backupCost.test.ts b/src/panels/accountOverview/services/advisories/fetchers/backupCost.test.ts index c455e9cdd..d936c9e72 100644 --- a/src/panels/accountOverview/services/advisories/fetchers/backupCost.test.ts +++ b/src/panels/accountOverview/services/advisories/fetchers/backupCost.test.ts @@ -7,6 +7,7 @@ import { CosmosDBManagementClient } from '@azure/arm-cosmosdb'; import { MonitorClient, type Metric, + type MetricsListOptionalParams, type Response as MetricsResponse, type TimeSeriesElement, } from '@azure/arm-monitor'; @@ -29,9 +30,9 @@ const rows = [ { databaseId: 'db', containerId: 'other' }, ]; -function storageSeries(container: string, total = 1_000_000_000): TimeSeriesElement { +function storageSeries(container: string, total = 1_000_000_000, database = 'db'): TimeSeriesElement { return { - metadatavalues: [...dims('db', container), { name: { value: 'Region' }, value: 'East US' }], + metadatavalues: [...dims(database, container), { name: { value: 'Region' }, value: 'East US' }], data: [{ timeStamp: timestamp, total }], }; } @@ -52,6 +53,29 @@ function metric(series: TimeSeriesElement[], overrides: Partial = {}): M }; } +function filteredStorage(series: TimeSeriesElement[], options?: MetricsListOptionalParams): MetricsResponse { + const dimensions = new Map(); + for (const [, dimension, literal] of (options?.filter ?? '').matchAll( + /(DatabaseName|CollectionName|Region) eq '((?:''|[^'])*)'/g, + )) { + const values = dimensions.get(dimension) ?? []; + values.push(literal.replace(/''/g, "'").toLowerCase()); + dimensions.set(dimension, values); + } + return metric( + series + .filter((entry) => + [...dimensions].every(([dimension, values]) => { + const value = entry.metadatavalues + ?.find((metadata) => metadata.name?.value === dimension) + ?.value?.toLowerCase(); + return values.includes('*') || (value !== undefined && values.includes(value)); + }), + ) + .slice(0, options?.top ?? 10), + ); +} + function retailItem(region: string, overrides: Record = {}) { return { armRegionName: region, @@ -91,9 +115,7 @@ function setup() { const monitor = new MonitorClient({ getToken: async () => null }, 'sub'); const list = vi .spyOn(monitor.metrics, 'list') - .mockImplementation(async (_account, options) => - metric([storageSeries(options?.filter?.includes("'other'") ? 'other' : 'items')]), - ); + .mockResolvedValue(metric(rows.map((row) => storageSeries(row.containerId)))); const fetchMock = vi.fn().mockImplementation(async () => new Response(JSON.stringify(page()))); vi.stubGlobal('fetch', fetchMock); const read = (overrides: Partial = {}, inventory = rows) => @@ -216,7 +238,7 @@ describe('paid continuous-backup retail scenario', () => { await expect(read()).rejects.toBe(failure); }); - it('counts one complete decimal-GB snapshot once, with exact current prices for every resolved region', async () => { + it('reads a complete dimension-split storage snapshot in one request, with current prices for every region', async () => { const { read, list, send, fetchMock } = setup(); const result = await read(); expect(result).toMatchObject({ @@ -234,13 +256,13 @@ describe('paid continuous-backup retail scenario', () => { }); expect(send).toHaveBeenCalledOnce(); expect(send.mock.calls[0][1].url).toContain('/subscriptions/sub/locations?api-version='); - expect(list).toHaveBeenCalledTimes(2); - expect(list).toHaveBeenCalledWith(accountId, { + expect(list).toHaveBeenCalledExactlyOnceWith(accountId, { metricnames: 'DataUsageV2', aggregation: 'Total', interval: 'PT5M', timespan: '2026-11-10T12:00:00.000Z/2026-11-11T12:00:00.000Z', - filter: "CollectionName eq 'items' and DatabaseName eq 'db' and Region eq 'East US'", + filter: "DatabaseName eq 'db' and (CollectionName eq 'items' or CollectionName eq 'other') and Region eq 'East US'", + top: 2, }); expect(fetchMock).toHaveBeenCalledExactlyOnceWith( expect.stringMatching(/^https:\/\/prices\.azure\.com\/api\/retail\/prices\?/), @@ -249,25 +271,124 @@ describe('paid continuous-backup retail scenario', () => { expect(fetchMock.mock.calls[0][1]).not.toHaveProperty('headers'); }); + it('matches lowercased Monitor dimensions to the original ARM inventory identities', async () => { + const { read, list } = setup(); + expect( + await read({}, [ + { databaseId: 'Db', containerId: 'Items' }, + { databaseId: 'DB', containerId: 'Other' }, + ]), + ).toMatchObject({ storageBytes: 2_000_000_000, logicalContainers: 2, monthlyAmount: 0.8 }); + expect(list).toHaveBeenCalledOnce(); + }); + + it('excludes a recently deleted container before it can consume a limited series slot', async () => { + const { read, list } = setup(); + const historical = [ + storageSeries('deleted', 99_000_000_000), + ...rows.map((row) => storageSeries(row.containerId)), + ]; + list.mockImplementation(async (_id, options) => filteredStorage(historical, options)); + expect(await read()).toMatchObject({ + storageBytes: 2_000_000_000, + logicalContainers: 2, + monthlyAmount: 0.8, + }); + expect(list).toHaveBeenCalledOnce(); + expect(list.mock.calls[0][1]?.filter).not.toContain("CollectionName eq '*'"); + }); + + it('keeps identically named containers in different databases separate', async () => { + const { read, list } = setup(); + const inventory = [ + { databaseId: 'db', containerId: 'items' }, + { databaseId: 'another-db', containerId: 'items' }, + ]; + list.mockImplementation(async (_id, options) => + filteredStorage( + inventory.map((row) => storageSeries(row.containerId, 1_000_000_000, row.databaseId)), + options, + ), + ); + expect(await read({}, inventory)).toMatchObject({ storageBytes: 2_000_000_000, logicalContainers: 2 }); + expect(list).toHaveBeenCalledTimes(2); + expect(list.mock.calls.map((call) => call[1]?.filter)).toEqual([ + "DatabaseName eq 'db' and (CollectionName eq 'items') and Region eq 'East US'", + "DatabaseName eq 'another-db' and (CollectionName eq 'items') and Region eq 'East US'", + ]); + }); + + it('does not request deleted cross-product identities from containers in different databases', async () => { + const { read, list } = setup(); + const inventory = [ + { databaseId: 'db', containerId: 'items' }, + { databaseId: 'other-db', containerId: 'other' }, + ]; + const series = [ + storageSeries('other', 99_000_000_000, 'db'), + storageSeries('items', 99_000_000_000, 'other-db'), + ...inventory.map((row) => storageSeries(row.containerId, 1_000_000_000, row.databaseId)), + ]; + list.mockImplementation(async (_id, options) => filteredStorage(series, options)); + expect(await read({}, inventory)).toMatchObject({ storageBytes: 2_000_000_000, logicalContainers: 2 }); + expect(list).toHaveBeenCalledTimes(2); + for (const [, options] of list.mock.calls) { + expect(options?.filter?.match(/DatabaseName eq /g)).toHaveLength(1); + expect(options?.top).toBe(1); + } + }); + + it('escapes database, container and reference-region literals without broadening the filter', async () => { + const { read, list, send } = setup(); + const inventory = [{ databaseId: "db'quoted", containerId: "items'quoted" }]; + const series = storageSeries("items'quoted", 1_000_000_000, "db'quoted"); + series.metadatavalues = [ + ...dims("db'quoted", "items'quoted"), + { name: { value: 'Region' }, value: "East 'US" }, + ]; + list.mockResolvedValue(metric([series])); + send.mockImplementation(async (_http, request) => ({ + request, + status: 200, + headers: createHttpHeaders(), + bodyAsText: JSON.stringify({ value: [{ name: 'eastus', displayName: "East 'US" }] }), + })); + expect(await read({ locationNames: ["East 'US"] }, inventory)).toMatchObject({ storageBytes: 1_000_000_000 }); + expect(list.mock.calls[0][1]?.filter).toBe( + "DatabaseName eq 'db''quoted' and (CollectionName eq 'items''quoted') and Region eq 'East ''US'", + ); + }); + + it.each([ + [ + { databaseId: 'db', containerId: 'items' }, + { databaseId: 'db', containerId: 'Items' }, + ], + [ + { databaseId: 'db', containerId: 'items' }, + { databaseId: 'Db', containerId: 'items' }, + ], + ])('rejects inventory identities that are ambiguous after Monitor case folding: %j', async (...inventory) => { + const { read, list, fetchMock } = setup(); + await expect(read({}, inventory)).rejects.toMatchObject({ kind: 'invalidData' }); + expect(list).not.toHaveBeenCalled(); + expect(fetchMock).not.toHaveBeenCalled(); + }); + it('chooses a common complete timestamp, not per-container latest values or sums of time buckets', async () => { const { read, list } = setup(); - list.mockImplementation(async (_id, options) => { - const container = options?.filter?.includes("'other'") ? 'other' : 'items'; - const series = storageSeries(container); - series.data?.push({ - timeStamp: new Date(now), - total: 99_000_000_000, - }); - return metric(container === 'items' ? [series] : [storageSeries(container)]); + const series = storageSeries('items'); + series.data?.push({ + timeStamp: new Date(now), + total: 99_000_000_000, }); + list.mockResolvedValue(metric([series, storageSeries('other')])); expect(await read()).toMatchObject({ monthlyAmount: 0.8, snapshotAt: timestamp.toISOString() }); }); it('retains observed zero storage but never replaces missing storage with zero', async () => { const { read, list } = setup(); - list.mockImplementation(async (_id, options) => - metric([storageSeries(options?.filter?.includes("'other'") ? 'other' : 'items', 0)]), - ); + list.mockResolvedValue(metric(rows.map((row) => storageSeries(row.containerId, 0)))); expect(await read()).toMatchObject({ monthlyAmount: 0, storageBytes: 0 }); list.mockResolvedValue(metric([])); await expect(read()).rejects.toMatchObject({ kind: 'noData' }); @@ -283,7 +404,19 @@ describe('paid continuous-backup retail scenario', () => { () => metric([{ ...storageSeries('items'), data: [{ timeStamp: new Date(NaN), total: 1 }] }]), ], ['wrong container', () => metric([storageSeries('unexpected')])], - ['duplicate identity', (series: TimeSeriesElement) => metric([series, series])], + ['duplicate identity', (series: TimeSeriesElement) => metric([series, series, storageSeries('other')])], + [ + 'case-folded duplicate identity', + (series: TimeSeriesElement) => + metric([ + series, + { + ...series, + metadatavalues: [...dims('DB', 'ITEMS'), { name: { value: 'Region' }, value: 'East US' }], + }, + storageSeries('other'), + ]), + ], [ 'wrong region', (series: TimeSeriesElement) => @@ -303,22 +436,28 @@ describe('paid continuous-backup retail scenario', () => { it.each([-6 * 3_600_000 - 1, 300_001])('rejects stale/future common snapshots at offset %s', async (offset) => { const { read, list } = setup(); - list.mockImplementation(async (_id, options) => { - const series = storageSeries(options?.filter?.includes("'other'") ? 'other' : 'items'); - series.data = [{ timeStamp: new Date(now + offset), total: 1 }]; - return metric([series]); - }); + list.mockResolvedValue( + metric( + rows.map((row) => ({ + ...storageSeries(row.containerId), + data: [{ timeStamp: new Date(now + offset), total: 1 }], + })), + ), + ); await expect(read()).rejects.toBeInstanceOf(Error); }); it('accepts the exact freshness and future-skew boundaries', async () => { const { read, list } = setup(); for (const offset of [-6 * 3_600_000, 300_000]) { - list.mockImplementation(async (_id, options) => { - const series = storageSeries(options?.filter?.includes("'other'") ? 'other' : 'items'); - series.data = [{ timeStamp: new Date(now + offset), total: 1 }]; - return metric([series]); - }); + list.mockResolvedValue( + metric( + rows.map((row) => ({ + ...storageSeries(row.containerId), + data: [{ timeStamp: new Date(now + offset), total: 1 }], + })), + ), + ); expect(await read()).toMatchObject({ snapshotAt: new Date(now + offset).toISOString() }); } }); @@ -369,32 +508,228 @@ describe('paid continuous-backup retail scenario', () => { expect(fetchMock).not.toHaveBeenCalled(); }); - it('reads all known containers with bounded concurrency, without wildcard discovery or series limits', async () => { + it('requests every inventory series explicitly beyond the default ten-series limit in one call', async () => { const { read, list } = setup(); const inventory = Array.from({ length: 13 }, (_, index) => ({ databaseId: 'db', containerId: `c${index}` })); - let inFlight = 0; - let peak = 0; - list.mockImplementation(async (_id, options) => { - inFlight++; - peak = Math.max(peak, inFlight); - await Promise.resolve(); - inFlight--; - const container = inventory.find((row) => options?.filter?.includes(`'${row.containerId}'`)); - if (!container) { - throw new Error('Metric query did not target a known container.'); - } - return metric([storageSeries(container.containerId)]); - }); + list.mockImplementation(async (_id, options) => + metric(inventory.slice(0, options?.top ?? 10).map((row) => storageSeries(row.containerId))), + ); expect(await read({}, inventory)).toMatchObject({ storageBytes: 13_000_000_000, logicalContainers: 13, monthlyAmount: 5.2, }); - expect(list).toHaveBeenCalledTimes(13); - expect(peak).toBe(5); - expect(list.mock.calls.every(([, options]) => !options?.filter?.includes("eq '*'"))).toBe(true); + expect(list).toHaveBeenCalledExactlyOnceWith( + accountId, + expect.objectContaining({ + top: 13, + filter: + "DatabaseName eq 'db' and (" + + inventory.map((row) => `CollectionName eq '${row.containerId}'`).join(' or ') + + ") and Region eq 'East US'", + }), + ); + }); + + it('bounds large inventories and requires a common timestamp across all batches', async () => { + const { read, list } = setup(); + const inventory = Array.from({ length: 45 }, (_, index) => ({ databaseId: 'db', containerId: `c${index}` })); + const series = inventory.map((row) => ({ + ...storageSeries(row.containerId), + data: [ + { timeStamp: timestamp, total: 1_000_000_000 }, + { timeStamp: new Date(now), total: row.containerId === 'c44' ? undefined : 99_000_000_000 }, + ], + })); + list.mockImplementation(async (_id, options) => filteredStorage(series, options)); + expect(await read({}, inventory)).toMatchObject({ + storageBytes: 45_000_000_000, + logicalContainers: 45, + snapshotAt: timestamp.toISOString(), + }); + expect(list.mock.calls.map((call) => call[1]?.top)).toEqual([20, 20, 5]); + for (const [, options] of list.mock.calls) { + expect(options?.filter?.match(/CollectionName eq /g)).toHaveLength(options?.top ?? 0); + expect(new URLSearchParams({ $filter: options?.filter ?? '' }).toString().length).toBeLessThanOrEqual(4096); + } }); + it('reads 25 databases in five concurrent waves instead of 25 serial round trips', async () => { + vi.useFakeTimers(); + try { + vi.setSystemTime(now); + const { read, list } = setup(); + const inventory = Array.from({ length: 25 }, (_, index) => ({ + databaseId: `db${index}`, + containerId: 'items', + })); + const series = inventory.map((row) => storageSeries(row.containerId, 1_000_000_000, row.databaseId)); + let active = 0; + let peak = 0; + const starts: number[] = []; + list.mockImplementation(async (_id, options) => { + active++; + peak = Math.max(peak, active); + starts.push(Date.now() - now); + await new Promise((resolve) => setTimeout(resolve, 10)); + active--; + return filteredStorage(series, options); + }); + const pending = read({}, inventory); + await vi.runAllTimersAsync(); + expect(await pending).toMatchObject({ + storageBytes: 25_000_000_000, + logicalContainers: 25, + snapshotAt: timestamp.toISOString(), + }); + expect(peak).toBe(5); + expect(active).toBe(0); + expect(starts).toEqual(Array.from({ length: 25 }, (_, index) => Math.floor(index / 5) * 10)); + } finally { + vi.useRealTimers(); + } + }); + + it('combines out-of-order waves only at a complete common timestamp', async () => { + vi.useFakeTimers(); + try { + const { read, list } = setup(); + const inventory = Array.from({ length: 7 }, (_, index) => ({ + databaseId: `db${index}`, + containerId: 'items', + })); + const series = inventory.map((row, index) => ({ + ...storageSeries(row.containerId, 1_000_000_000, row.databaseId), + data: [ + { timeStamp: timestamp, total: 1_000_000_000 }, + { timeStamp: new Date(now), total: index === 6 ? undefined : 99_000_000_000 }, + ], + })); + let started = 0; + const completed: number[] = []; + list.mockImplementation(async (_id, options) => { + const index = started++; + await new Promise((resolve) => setTimeout(resolve, (5 - (index % 5)) * 10)); + completed.push(index); + return filteredStorage(series, options); + }); + const pending = read({}, inventory); + await vi.runAllTimersAsync(); + expect(await pending).toMatchObject({ + storageBytes: 7_000_000_000, + logicalContainers: 7, + snapshotAt: timestamp.toISOString(), + }); + expect(completed).toEqual([4, 3, 2, 1, 0, 6, 5]); + } finally { + vi.useRealTimers(); + } + }); + + it('does not start another wave or query prices when a concurrent request fails', async () => { + vi.useFakeTimers(); + try { + const { read, list, fetchMock } = setup(); + const inventory = Array.from({ length: 12 }, (_, index) => ({ + databaseId: `db${index}`, + containerId: 'items', + })); + const series = inventory.map((row) => storageSeries(row.containerId, 1_000_000_000, row.databaseId)); + const failure = new Error('Storage request failed.'); + let started = 0; + let completed = 0; + list.mockImplementation(async (_id, options) => { + const index = started++; + await new Promise((resolve) => setTimeout(resolve, index === 0 ? 1 : 10)); + completed++; + if (index === 0) { + throw failure; + } + return filteredStorage(series, options); + }); + const pending = read({}, inventory).then( + () => undefined, + (error: unknown) => error, + ); + await vi.runAllTimersAsync(); + expect(await pending).toBe(failure); + expect(list).toHaveBeenCalledTimes(5); + expect(completed).toBe(5); + expect(fetchMock).not.toHaveBeenCalled(); + } finally { + vi.useRealTimers(); + } + }); + + it('bounds URL-encoded filters even when long escaped names fit in fewer than twenty terms', async () => { + const { read, list } = setup(); + const inventory = Array.from({ length: 13 }, (_, index) => ({ + databaseId: 'db', + containerId: `${"'".repeat(240)}${index}`, + })); + const series = inventory.map((row) => storageSeries(row.containerId)); + list.mockImplementation(async (_id, options) => filteredStorage(series, options)); + expect(await read({}, inventory)).toMatchObject({ storageBytes: 13_000_000_000, logicalContainers: 13 }); + expect(list.mock.calls.length).toBeGreaterThan(1); + for (const [, options] of list.mock.calls) { + expect(new URLSearchParams({ $filter: options?.filter ?? '' }).toString().length).toBeLessThanOrEqual(4096); + } + expect(list.mock.calls.reduce((sum, call) => sum + (call[1]?.top ?? 0), 0)).toBe(inventory.length); + }); + + it.each(['missing', 'disjoint', 'failure', 'wrong batch'] as const)( + 'never prices partial inventory when a later batch has %s evidence', + async (failure) => { + const { read, list, fetchMock } = setup(); + const inventory = Array.from({ length: 21 }, (_, index) => ({ + databaseId: 'db', + containerId: `c${index}`, + })); + const series = inventory.map((row) => storageSeries(row.containerId)); + list.mockImplementationOnce(async (_id, options) => filteredStorage(series, options)); + if (failure === 'failure') { + list.mockRejectedValueOnce(new Error('Metrics unavailable.')); + } else { + const last = storageSeries('c20'); + if (failure === 'disjoint') { + last.data = [{ timeStamp: new Date(now), total: 1_000_000_000 }]; + } + list.mockResolvedValueOnce( + metric(failure === 'missing' ? [] : failure === 'wrong batch' ? [series[0]] : [last]), + ); + } + await expect(read({}, inventory)).rejects.toBeInstanceOf(Error); + expect(list).toHaveBeenCalledTimes(2); + expect(fetchMock).not.toHaveBeenCalled(); + }, + ); + + it.each([ + { databaseId: '*', containerId: 'items' }, + { databaseId: 'db', containerId: '*' }, + { databaseId: 'db', containerId: "'".repeat(1000) }, + ])('does not issue wildcard or oversized inventory filters: %j', async (row) => { + const { read, list, fetchMock } = setup(); + await expect(read({}, [row])).rejects.toMatchObject({ kind: 'unsupported' }); + expect(list).not.toHaveBeenCalled(); + expect(fetchMock).not.toHaveBeenCalled(); + }); + + it.each([0, 1, 10])( + 'rejects a truncated snapshot of %i out of thirteen containers without pricing it', + async (count) => { + const { read, list, fetchMock } = setup(); + const inventory = Array.from({ length: 13 }, (_, index) => ({ + databaseId: 'db', + containerId: `c${index}`, + })); + list.mockResolvedValue(metric(inventory.slice(0, count).map((row) => storageSeries(row.containerId)))); + await expect(read({}, inventory)).rejects.toMatchObject({ kind: 'noData' }); + expect(list).toHaveBeenCalledOnce(); + expect(fetchMock).not.toHaveBeenCalled(); + }, + ); + it('fails closed without complete inventory and on duplicate container identities', async () => { const { read, cosmos, monitor } = setup(); await expect(read({}, [])).rejects.toMatchObject({ kind: 'noData' }); diff --git a/src/panels/accountOverview/services/advisories/fetchers/backupCost.ts b/src/panels/accountOverview/services/advisories/fetchers/backupCost.ts index 42ac2837f..2f98a2ab3 100644 --- a/src/panels/accountOverview/services/advisories/fetchers/backupCost.ts +++ b/src/panels/accountOverview/services/advisories/fetchers/backupCost.ts @@ -8,7 +8,7 @@ import { type MonitorClient } from '@azure/arm-monitor'; import { RestError } from '@azure/core-rest-pipeline'; import { z } from 'zod'; import { type InventoryContainerRow } from '../../inventory'; -import { containerFilter, DAY, escapeODataLiteral, HOUR, MINUTE, parseMonitorTimestamp } from '../../shared'; +import { DAY, escapeODataLiteral, HOUR, MINUTE, parseMonitorTimestamp } from '../../shared'; import { roundMonthlySavings } from '../core/helpers'; import { type AdvisoryCostEstimate, type BackupPrice } from '../core/types'; import { CostEvidenceError, readCostArmJson, type CostContext } from './costManagement'; @@ -17,6 +17,9 @@ const RETAIL_URL = 'https://prices.azure.com/api/retail/prices'; const PITR_PRODUCT = 'Azure Cosmos DB - PITR'; const PITR_METER = 'Cont 30D Bckp Continuous Backup'; const CURRENCY = 'USD'; +const MAX_STORAGE_SERIES_PER_REQUEST = 20; +const MAX_STORAGE_FILTER_LENGTH = 4096; +const STORAGE_REQUEST_BATCH_SIZE = 5; const locationsSchema = z.object({ nextLink: z.string().nullable().optional(), @@ -89,7 +92,11 @@ const priceSchema = z.object({ }); /** Unauthenticated reads only: no ARM pipeline/tokens and no redirects or pagination outside the fixed endpoint. */ -async function getBackupPrices(regions: readonly string[], now: number): Promise { +async function getBackupPrices( + regions: readonly string[], + now: number, + abortSignal?: AbortSignal, +): Promise { const initial = new URL(RETAIL_URL); initial.searchParams.set('$filter', `serviceName eq 'Azure Cosmos DB' and productName eq '${PITR_PRODUCT}'`); initial.searchParams.set('currencyCode', CURRENCY); @@ -110,7 +117,12 @@ async function getBackupPrices(regions: readonly string[], now: number): Promise throw new CostEvidenceError('invalidData'); } seen.add(url); - const response = await fetch(url, { redirect: 'error', signal: AbortSignal.timeout(15_000) }); + abortSignal?.throwIfAborted(); + const timeout = AbortSignal.timeout(15_000); + const response = await fetch(url, { + redirect: 'error', + signal: abortSignal ? AbortSignal.any([abortSignal, timeout]) : timeout, + }); if (!response.ok) { throw new RestError('Optional backup price request failed.', { statusCode: response.status }); } @@ -179,6 +191,7 @@ async function getStorageSnapshot( rows: readonly Pick[], referenceRegion: string, now: number, + abortSignal?: AbortSignal, ): Promise<{ timestamp: number; bytes: number }> { // Azure Monitor folds dimension values to lowercase, independently of ARM resource-name casing. const identity = (databaseId: string, containerId: string) => @@ -190,17 +203,55 @@ async function getStorageSnapshot( if (expected.size !== rows.length || rows.some((row) => !row.databaseId || !row.containerId)) { throw new CostEvidenceError('invalidData'); } + if (rows.some((row) => row.databaseId === '*' || row.containerId === '*')) { + throw new CostEvidenceError('unsupported'); + } + // Monitor permits OR only within one dimension; grouping by database avoids cross-product identities. + const databases = new Map(); + for (const row of rows) { + const key = row.databaseId.toLowerCase(); + const database = databases.get(key) ?? { databaseId: row.databaseId, containerIds: [] }; + database.containerIds.push(row.containerId); + databases.set(key, database); + } + const requests: { filter: string; expected: Set }[] = []; + for (const { databaseId, containerIds } of databases.values()) { + for (let start = 0; start < containerIds.length;) { + const batch: string[] = []; + let filter = ''; + while (start < containerIds.length && batch.length < MAX_STORAGE_SERIES_PER_REQUEST) { + const collections = [...batch, containerIds[start]] + .map((containerId) => `CollectionName eq '${escapeODataLiteral(containerId)}'`) + .join(' or '); + const nextFilter = + `DatabaseName eq '${escapeODataLiteral(databaseId)}' and (${collections}) ` + + `and Region eq '${escapeODataLiteral(referenceRegion)}'`; + if (new URLSearchParams({ $filter: nextFilter }).toString().length > MAX_STORAGE_FILTER_LENGTH) { + if (batch.length === 0) { + throw new CostEvidenceError('unsupported'); + } + break; + } + batch.push(containerIds[start++]); + filter = nextFilter; + } + requests.push({ filter, expected: new Set(batch.map((containerId) => identity(databaseId, containerId))) }); + } + } const snapshots = new Map>(); const duplicates = new Set(); - for (let offset = 0; offset < rows.length; offset += 5) { + for (let start = 0; start < requests.length; start += STORAGE_REQUEST_BATCH_SIZE) { + abortSignal?.throwIfAborted(); await Promise.all( - rows.slice(offset, offset + 5).map(async (row) => { + requests.slice(start, start + STORAGE_REQUEST_BATCH_SIZE).map(async (request) => { const response = await client.metrics.list(accountId, { metricnames: 'DataUsageV2', aggregation: 'Total', timespan: `${new Date(now - DAY).toISOString()}/${new Date(now).toISOString()}`, interval: 'PT5M', - filter: `${containerFilter(row.databaseId, row.containerId)} and Region eq '${escapeODataLiteral(referenceRegion)}'`, + filter: request.filter, + top: request.expected.size, + ...(abortSignal && { abortSignal }), }); if ( response.value?.length !== 1 || @@ -221,11 +272,12 @@ async function getStorageSnapshot( metadata.size !== 3 || !databaseId || !containerId || - identity(databaseId, containerId) !== identity(row.databaseId, row.containerId) || + !request.expected.has(identity(databaseId, containerId)) || metadata.get('region')?.toLowerCase() !== referenceRegion.toLowerCase() ) { throw new CostEvidenceError('invalidData'); } + const key = identity(databaseId, containerId); for (const point of series.data ?? []) { if (point.total === undefined || point.total === null) { continue; @@ -235,7 +287,6 @@ async function getStorageSnapshot( throw new CostEvidenceError('invalidData'); } const snapshot = snapshots.get(timestamp) ?? new Map(); - const key = identity(databaseId, containerId); if (snapshot.has(key)) { duplicates.add(timestamp); } @@ -271,6 +322,7 @@ export async function getBackupCostEstimate( rows: readonly Pick[] | undefined, now: number, ): Promise { + context.abortSignal?.throwIfAborted(); if (new URL(context.resourceManagerEndpoint).origin !== 'https://management.azure.com') { throw new CostEvidenceError('unsupported'); } @@ -279,10 +331,18 @@ export async function getBackupCostEstimate( } const regions = await resolveRegions(cosmosClient, context); const referenceRegion = regions[0].monitorName; - const snapshot = await getStorageSnapshot(monitorClient, accountId, rows, referenceRegion, now); + const snapshot = await getStorageSnapshot( + monitorClient, + accountId, + rows, + referenceRegion, + now, + context.abortSignal, + ); const prices = await getBackupPrices( regions.map((region) => region.retailName), now, + context.abortSignal, ); const monthlyAmount = (snapshot.bytes / 1_000_000_000) * prices.reduce((sum, price) => sum + price.perGbMonth, 0); if (!Number.isFinite(monthlyAmount) || monthlyAmount < 0) { diff --git a/src/panels/accountOverview/services/advisories/fetchers/costManagement.ts b/src/panels/accountOverview/services/advisories/fetchers/costManagement.ts index d74a1e8ae..24e7d5e3d 100644 --- a/src/panels/accountOverview/services/advisories/fetchers/costManagement.ts +++ b/src/panels/accountOverview/services/advisories/fetchers/costManagement.ts @@ -21,6 +21,7 @@ export interface CostContext { locationNames: readonly string[]; /** Raw ARM setting; absent is unknown, not a single-write account. */ multiRegionWritesEnabled?: boolean; + abortSignal?: AbortSignal; } export class CostEvidenceError extends Error { @@ -63,6 +64,7 @@ export async function readCostArmJson( path: string, body?: unknown, ): Promise { + context.abortSignal?.throwIfAborted(); const response = await client.pipeline.sendRequest( createDefaultHttpClient(), createPipelineRequest({ @@ -71,6 +73,7 @@ export async function readCostArmJson( headers: createHttpHeaders({ 'Content-Type': 'application/json' }), body: body === undefined ? undefined : JSON.stringify(body), timeout: 20_000, + abortSignal: context.abortSignal, }), ); if (response.status < 200 || response.status >= 300) { diff --git a/src/panels/accountOverview/services/advisories/fetchers/logs.test.ts b/src/panels/accountOverview/services/advisories/fetchers/logs.test.ts index d154131d5..1d3b1dcb8 100644 --- a/src/panels/accountOverview/services/advisories/fetchers/logs.test.ts +++ b/src/panels/accountOverview/services/advisories/fetchers/logs.test.ts @@ -21,6 +21,7 @@ import { fetchUncontrolledIngestion, logWindow, probeCdbLogs, + requestLogWindow, tableToRecords, type LogsQueryExecutor, type LogsTimespan, @@ -173,23 +174,76 @@ describe('classifyLogsError', () => { describe('probeCdbLogs', () => { it.each(['CDBDataPlaneRequests', 'CDBDataPlaneRequests5M', 'CDBQueryRuntimeStatistics'] as const)( - 'probes only the schema of %s using the normalized Cosmos ARM resource ID', + 'probes window-local presence in %s without returning log rows, using the normalized ARM ID', async (tableName) => { - const queryResource = vi.fn().mockResolvedValue(success(table(['x'], []))); + const queryResource = vi.fn().mockResolvedValue(success(table(['n'], [[1]]))); const onDiagnostic = vi.fn(); await expect( probeCdbLogs({ queryResource }, RESOURCE, SPAN, { table: tableName, onDiagnostic }), - ).resolves.toEqual({ available: true }); - expect(queryResource).toHaveBeenCalledExactlyOnceWith(RESOURCE.slice(1), `${tableName} | take 0`, SPAN); + ).resolves.toEqual({ available: true, hasData: true }); + expect(queryResource).toHaveBeenCalledExactlyOnceWith( + RESOURCE.slice(1), + `${tableName} | where TimeGenerated >= datetime(2024-01-01T00:00:00.000Z) ` + + 'and TimeGenerated < datetime(2024-01-02T00:00:00.000Z) | take 1 | summarize n = count()', + SPAN, + ); expect(onDiagnostic).not.toHaveBeenCalled(); }, ); - it('reports available when the probe query succeeds (even with zero rows)', async () => { + it('distinguishes a successful empty window from unavailable access', async () => { const executor: LogsQueryExecutor = { - queryResource: () => Promise.resolve(success(table(['x'], []))), + queryResource: () => Promise.resolve(success(table(['n'], [[0]]))), }; - await expect(probeCdbLogs(executor, RESOURCE, SPAN)).resolves.toEqual({ available: true }); + await expect(probeCdbLogs(executor, RESOURCE, SPAN)).resolves.toEqual({ available: true, hasData: false }); + }); + + it.each(['CDBDataPlaneRequests', 'CDBDataPlaneRequests5M', 'CDBQueryRuntimeStatistics'] as const)( + 'uses the same half-open analysis window as downstream queries for %s', + async (tableName) => { + const now = Date.parse('2026-11-11T09:17:23.456Z'); + const queryResource = vi.fn().mockResolvedValue(success(table(['n'], [[0]]))); + await probeCdbLogs({ queryResource }, RESOURCE, logWindow(now), { table: tableName }); + const expected = tableName === 'CDBDataPlaneRequests5M' ? requestLogWindow(tableName, now) : logWindow(now); + expect(queryResource).toHaveBeenCalledExactlyOnceWith( + RESOURCE.slice(1), + `${tableName} | where TimeGenerated >= datetime(${expected.startTime.toISOString()}) ` + + `and TimeGenerated < datetime(${expected.endTime.toISOString()}) | take 1 | summarize n = count()`, + expected, + ); + }, + ); + + it.each([ + table(['n'], []), + table(['n'], [[null]]), + table(['n'], [['0']]), + table(['n'], [[Number.NaN]]), + table(['n'], [[2]]), + table(['n'], [[-1]]), + table(['n'], [[0], [1]]), + table(['other'], [[0]]), + ])('rejects malformed presence evidence rather than treating it as an empty window: %j', async (resultTable) => { + const onDiagnostic = vi.fn(); + await expect( + probeCdbLogs({ queryResource: async () => success(resultTable) }, RESOURCE, SPAN, { onDiagnostic }), + ).resolves.toEqual({ available: false, reason: 'noData' }); + expect(onDiagnostic).toHaveBeenCalledOnce(); + }); + + it.each([ + { startTime: new Date(Number.NaN), endTime: SPAN.endTime }, + { startTime: SPAN.endTime, endTime: SPAN.startTime }, + { startTime: SPAN.startTime, endTime: SPAN.startTime }, + ])('reports an invalid observation window without issuing a query: %j', async (window) => { + const queryResource = vi.fn(); + const onDiagnostic = vi.fn(); + await expect(probeCdbLogs({ queryResource }, RESOURCE, window, { onDiagnostic })).resolves.toEqual({ + available: false, + reason: 'noData', + }); + expect(queryResource).not.toHaveBeenCalled(); + expect(onDiagnostic).toHaveBeenCalledOnce(); }); it('classifies a missing-table failure as logAnalyticsDisabled', async () => { @@ -240,7 +294,11 @@ describe('probeCdbLogs', () => { available: false, reason: 'noData', }); - expect(queryResource).toHaveBeenCalledExactlyOnceWith(RESOURCE.slice(1), 'CDBDataPlaneRequests | take 0', SPAN); + expect(queryResource).toHaveBeenCalledExactlyOnceWith( + RESOURCE.slice(1), + expect.stringMatching(/^CDBDataPlaneRequests .* \| take 1 \| summarize n = count\(\)$/), + SPAN, + ); expect(onDiagnostic).toHaveBeenCalledExactlyOnceWith( expect.objectContaining({ operation: 'probeDataPlane', diff --git a/src/panels/accountOverview/services/advisories/fetchers/logs.ts b/src/panels/accountOverview/services/advisories/fetchers/logs.ts index 3e5828971..33a62da7c 100644 --- a/src/panels/accountOverview/services/advisories/fetchers/logs.ts +++ b/src/panels/accountOverview/services/advisories/fetchers/logs.ts @@ -61,7 +61,7 @@ export function requestLogWindow(source: DataPlaneSource, now = Date.now()): Log ); } -function requestLogEvidence(source: DataPlaneSource, timespan: LogsTimespan): RequestLogEvidence { +export function requestLogEvidence(source: DataPlaneSource, timespan: LogsTimespan): RequestLogEvidence { return { sourceTable: source, sourceResolution: source === 'CDBDataPlaneRequests5M' ? 'PT5M' : 'perRequest', @@ -266,9 +266,8 @@ export function classifyLogsError(error: unknown): UnavailableReason { } /** - * Probes one required resource-specific table without retrieving log rows. Success means the table query completed, - * not that every backing workspace is accessible or that the time window contains data. Each table gates its own - * detectors; missing-table failures retain the legacy `logAnalyticsDisabled` reason without asserting logs are disabled. + * Probes table access and window-local data presence without returning log rows. A successful empty probe is not a + * missing table or proof of complete collection. Each table gates only its dependent detector/enrichment queries. */ export async function probeCdbLogs( executor: LogsQueryExecutor, @@ -278,24 +277,51 @@ export async function probeCdbLogs( onDiagnostic?: LogsDiagnosticReporter; table?: DataPlaneSource | 'CDBQueryRuntimeStatistics'; }, -): Promise<{ available: boolean; reason?: UnavailableReason }> { +): Promise<{ available: true; hasData: boolean } | { available: false; reason: UnavailableReason }> { const table = options?.table ?? 'CDBDataPlaneRequests'; + const operation = + table === 'CDBDataPlaneRequests' + ? 'probeDataPlane' + : table === 'CDBDataPlaneRequests5M' + ? 'probeDataPlane5M' + : 'probeQueryRuntime'; + const window = + table === 'CDBDataPlaneRequests5M' + ? { + startTime: new Date( + Math.floor(timespan.startTime.getTime() / AGGREGATE_BUCKET_MS) * AGGREGATE_BUCKET_MS, + ), + endTime: new Date(Math.floor(timespan.endTime.getTime() / AGGREGATE_BUCKET_MS) * AGGREGATE_BUCKET_MS), + } + : timespan; try { - await executeLogsQuery( + if ( + !Number.isFinite(window.startTime.getTime()) || + !Number.isFinite(window.endTime.getTime()) || + window.startTime >= window.endTime + ) { + throw new Error('Log table presence probe requires a valid, nonempty observation window.'); + } + const rows = await executeLogsQuery( executor, resourceId, - `${table} | take 0`, - timespan, - table === 'CDBDataPlaneRequests' - ? 'probeDataPlane' - : table === 'CDBDataPlaneRequests5M' - ? 'probeDataPlane5M' - : 'probeQueryRuntime', + `${table} | where TimeGenerated >= datetime(${window.startTime.toISOString()}) ` + + `and TimeGenerated < datetime(${window.endTime.toISOString()}) | take 1 | summarize n = count()`, + window, + operation, [], options?.onDiagnostic, + (values) => { + if (values.length !== 1 || (values[0].n !== 0 && values[0].n !== 1)) { + throw new Error('Log table presence probe returned an invalid row count.'); + } + }, ); - return { available: true }; + return { available: true, hasData: rows[0].n === 1 }; } catch (error) { + if (!(error instanceof LogsFetchError)) { + reportDiagnostic(createLogsDiagnostic(error, operation), options?.onDiagnostic); + } return { available: false, reason: classifyLogsError(error) }; } } @@ -306,19 +332,19 @@ export async function selectDataPlaneSource( resourceId: string, timespan: LogsTimespan, onDiagnostic?: LogsDiagnosticReporter, -): Promise { +): Promise<{ source: DataPlaneSource; hasData: boolean } | undefined> { const diagnostics: LogsDiagnostic[] = []; const aggregate = await probeCdbLogs(executor, resourceId, timespan, { table: 'CDBDataPlaneRequests5M', onDiagnostic: (diagnostic) => diagnostics.push(diagnostic), }); if (aggregate.available) { - return 'CDBDataPlaneRequests5M'; + return { source: 'CDBDataPlaneRequests5M', hasData: aggregate.hasData }; } if (diagnostics[0]?.kind === 'missingTable') { const raw = await probeCdbLogs(executor, resourceId, timespan, { onDiagnostic }); if (raw.available) { - return 'CDBDataPlaneRequests'; + return { source: 'CDBDataPlaneRequests', hasData: raw.hasData }; } } for (const diagnostic of diagnostics) { @@ -538,6 +564,7 @@ export async function fetchUncontrolledIngestion( 'ingestionBurst', [databaseId, containerId, db, coll], onDiagnostic, + // Kusto serializes avg() over no write buckets as "NaN", while max() is null. (rows) => { for (const row of rows) { if (row.peak === null && (Number.isNaN(row.avg) || row.avg === 'NaN')) { diff --git a/src/panels/accountOverview/services/advisories/fetchers/logsResourceRoute.test.ts b/src/panels/accountOverview/services/advisories/fetchers/logsResourceRoute.test.ts index 9dd22fb81..19c3deac5 100644 --- a/src/panels/accountOverview/services/advisories/fetchers/logsResourceRoute.test.ts +++ b/src/panels/accountOverview/services/advisories/fetchers/logsResourceRoute.test.ts @@ -30,14 +30,19 @@ describe('Log Analytics SDK resource route', () => { status: 200, headers: createHttpHeaders(), bodyAsText: JSON.stringify({ - tables: [{ name: 'PrimaryResult', columns: [], rows: [] }], + tables: [ + { name: 'PrimaryResult', columns: [{ name: 'n', type: 'long' }], rows: [[0]] }, + ], }), }; }, }, }, ); - await expect(probeCdbLogs(client, resource, logWindow())).resolves.toEqual({ available: true }); + await expect(probeCdbLogs(client, resource, logWindow())).resolves.toEqual({ + available: true, + hasData: false, + }); expect(requests).toHaveLength(1); const url = new URL(requests[0].url); expect(url.origin).toBe('https://api.loganalytics.io'); diff --git a/src/panels/accountOverview/services/advisories/fetchers/requestLogs.test.ts b/src/panels/accountOverview/services/advisories/fetchers/requestLogs.test.ts index 684d5688b..2810bf0ab 100644 --- a/src/panels/accountOverview/services/advisories/fetchers/requestLogs.test.ts +++ b/src/panels/accountOverview/services/advisories/fetchers/requestLogs.test.ts @@ -39,35 +39,40 @@ function missingTable(table: string) { } describe('request-log source selection', () => { - it('prefers one aggregate source without querying raw data or requiring nonempty telemetry', async () => { - const queryResource = vi.fn().mockResolvedValue(success()); + it.each([0, 1])('selects the aggregate source with presence=%i without querying raw data', async (n) => { + const queryResource = vi.fn().mockResolvedValue(success(['n'], [[n]])); const report = vi.fn(); - await expect(selectDataPlaneSource({ queryResource }, RESOURCE, SPAN, report)).resolves.toBe( - 'CDBDataPlaneRequests5M', - ); + await expect(selectDataPlaneSource({ queryResource }, RESOURCE, SPAN, report)).resolves.toEqual({ + source: 'CDBDataPlaneRequests5M', + hasData: n > 0, + }); expect(queryResource).toHaveBeenCalledExactlyOnceWith( RESOURCE.slice(1), - 'CDBDataPlaneRequests5M | take 0', + expect.stringMatching(/^CDBDataPlaneRequests5M .* \| take 1 \| summarize n = count\(\)$/), SPAN, ); expect(report).not.toHaveBeenCalled(); }); - it('falls back to raw only for an explicitly missing aggregate table, without false partial coverage', async () => { - const queryResource = vi - .fn() - .mockRejectedValueOnce(missingTable('CDBDataPlaneRequests5M')) - .mockResolvedValueOnce(success()); - const report = vi.fn(); - await expect(selectDataPlaneSource({ queryResource }, RESOURCE, SPAN, report)).resolves.toBe( - 'CDBDataPlaneRequests', - ); - expect(queryResource.mock.calls.map((call) => call[1])).toEqual([ - 'CDBDataPlaneRequests5M | take 0', - 'CDBDataPlaneRequests | take 0', - ]); - expect(report).not.toHaveBeenCalled(); - }); + it.each([0, 1])( + 'falls back only for a missing aggregate table, with raw presence=%i and no false failure', + async (n) => { + const queryResource = vi + .fn() + .mockRejectedValueOnce(missingTable('CDBDataPlaneRequests5M')) + .mockResolvedValueOnce(success(['n'], [[n]])); + const report = vi.fn(); + await expect(selectDataPlaneSource({ queryResource }, RESOURCE, SPAN, report)).resolves.toEqual({ + source: 'CDBDataPlaneRequests', + hasData: n > 0, + }); + expect(queryResource.mock.calls.map((call) => call[1].split(' ')[0])).toEqual([ + 'CDBDataPlaneRequests5M', + 'CDBDataPlaneRequests', + ]); + expect(report).not.toHaveBeenCalled(); + }, + ); it.each([ { statusCode: 403, code: 'Forbidden' }, @@ -270,6 +275,53 @@ describe('weighted five-minute request logs', () => { }, ); + it.each([0, 20])('accepts Kusto empty-write averages with %s total requests', async (requests) => { + const report = vi.fn(); + const executor: LogsQueryExecutor = { + queryResource: async (_id, query) => + query.includes('totalRu =') + ? success( + ['totalRu', 'writeRu', 'reqs', 'throttles', 'invalidRows'], + [[requests * 5, 0, requests, 0, 0]], + ) + : query.includes('peak =') + ? success(['peak', 'avg', 'invalidRows'], [[null, 'NaN', 0]]) + : success(['UserAgent', 'n', 'invalidRows']), + }; + await expect( + fetchUncontrolledIngestion(executor, RESOURCE, 'db', 'items', SPAN, report, 'CDBDataPlaneRequests5M'), + ).resolves.toMatchObject({ + totalRequests: requests, + writeRu: 0, + burstFactor: 0, + dominantUserAgent: undefined, + }); + expect(report).not.toHaveBeenCalled(); + }); + + it.each([ + [100, 'NaN', 0], + [100, Number.NaN, 0], + [null, 'Infinity', 0], + [null, 100, 0], + [null, 'NaN', 1], + [null, 'NaN', undefined], + ])('still rejects invalid burst aggregates: %j', async (...row) => { + const report = vi.fn(); + const executor: LogsQueryExecutor = { + queryResource: async (_id, query) => + query.includes('totalRu =') + ? success(['totalRu', 'writeRu', 'reqs', 'throttles', 'invalidRows'], [[100, 0, 20, 0, 0]]) + : query.includes('peak =') + ? success(['peak', 'avg', 'invalidRows'], [row]) + : success(), + }; + await expect( + fetchUncontrolledIngestion(executor, RESOURCE, 'db', 'items', SPAN, report, 'CDBDataPlaneRequests5M'), + ).rejects.toMatchObject({ name: 'LogsFetchError', reason: 'noData' }); + expect(report).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ operation: 'ingestionBurst' })); + }); + it.each(['startTime', 'endTime'] as const)('rejects partial %s buckets before querying', async (boundary) => { const queryResource = vi.fn(); const window = { ...SPAN, [boundary]: new Date(SPAN[boundary].getTime() + 1) }; diff --git a/src/utils/azureClients.test.ts b/src/utils/azureClients.test.ts index d9321b58c..f9aefead8 100644 --- a/src/utils/azureClients.test.ts +++ b/src/utils/azureClients.test.ts @@ -10,10 +10,14 @@ import { createPipelineRequest, type PipelineRequest, type PipelineResponse, + type RawHttpHeaders, } from '@azure/core-rest-pipeline'; import { createAzureSubscriptionClient, type AzExtSubscriptionClientType } from '@microsoft/vscode-azext-azureutils'; import { type IActionContext, type ISubscriptionContext } from '@microsoft/vscode-azext-utils'; import { armTestSubscription, createArmTestContext } from '../cosmosdb/armTestUtils'; +import { type DerivedAdvisory } from '../panels/accountOverview/services/advisories/core/types'; +import { enrichAdvisoryCosts } from '../panels/accountOverview/services/advisories/costEstimates'; +import { readCostArmJson } from '../panels/accountOverview/services/advisories/fetchers/costManagement'; import { COSMOSDB_ARM_API_VERSION, createCosmosDBManagementClient, @@ -25,7 +29,9 @@ vi.mock('@microsoft/vscode-azext-utils', () => ({ createSubscriptionContext: (subscription: typeof armTestSubscription) => ({ subscriptionId: subscription.subscriptionId, environment: subscription.environment, - credentials: { getToken: () => Promise.resolve(null) }, + credentials: { + getToken: () => Promise.resolve({ token: 'test-token', expiresOnTimestamp: Date.now() + 3_600_000 }), + }, }), })); vi.mock('@microsoft/vscode-azext-azureutils', () => ({ @@ -43,6 +49,304 @@ describe('modular ARM client factories', () => { vi.clearAllMocks(); }); + describe('Cost Management retry headers', () => { + const costPath = '/subscriptions/sub-1/providers/Microsoft.CostManagement/query?api-version=2023-11-01'; + const qpu = 'x-ms-ratelimit-microsoft.costmanagement-qpu-retry-after'; + const entity = 'x-ms-ratelimit-microsoft.costmanagement-entity-retry-after'; + + beforeEach(() => { + vi.clearAllMocks(); + vi.useFakeTimers(); + vi.setSystemTime(new Date('2026-11-12T10:00:00Z')); + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + async function probe(headers: RawHttpHeaders, path = costPath) { + const client = await createCosmosDBManagementClient(createArmTestContext(), armTestSubscription); + const request = createPipelineRequest({ + url: `https://management.azure.com${path}`, + method: 'POST', + body: '{}', + }); + const sentAt: number[] = []; + const status = vi.fn<() => number>().mockReturnValue(200).mockReturnValueOnce(429); + const sendRequest = vi.fn(async (forwarded: PipelineRequest): Promise => { + sentAt.push(Date.now()); + return { request: forwarded, status: status(), headers: createHttpHeaders(headers), bodyAsText: '{}' }; + }); + return { + client, + request, + status, + sendRequest, + sentAt, + start: () => client.pipeline.sendRequest({ sendRequest }, request), + }; + } + + async function measureRetryDelay(headers: RawHttpHeaders, delay: number) { + const run = await probe(headers); + const response = run.start(); + await vi.advanceTimersByTimeAsync(0); + expect(run.sendRequest).toHaveBeenCalledOnce(); + await vi.advanceTimersByTimeAsync(delay - 1); + expect(run.sendRequest).toHaveBeenCalledOnce(); + await vi.advanceTimersByTimeAsync(1); + expect((await response).status).toBe(200); + expect(run.sendRequest).toHaveBeenCalledTimes(2); + expect(run.request.url).toBe(`https://management.azure.com${costPath}`); + expect(run.request.method).toBe('POST'); + expect(run.request.body).toBe('{}'); + expect(run.request.headers.has('retry-after-ms')).toBe(false); + return run.sentAt[1] - run.sentAt[0]; + } + + it.each(['qpu', 'entity', 'tenant', 'clienttype'])( + 'honors the %s delay in seconds through SDK retries', + async (scope) => { + expect( + await measureRetryDelay( + { [`x-ms-ratelimit-microsoft.costmanagement-${scope}-retry-after`]: '2' }, + 2000, + ), + ).toBe(2000); + }, + ); + + it('matches header names case-insensitively', async () => { + expect( + await measureRetryDelay({ 'X-Ms-Ratelimit-Microsoft.CostManagement-Qpu-Retry-After': '2' }, 2000), + ).toBe(2000); + }); + + it('uses the longest valid Cost Management delay without an invalid header masking it', async () => { + expect( + await measureRetryDelay( + { + [qpu]: '1', + [entity]: '3', + 'x-ms-ratelimit-microsoft.costmanagement-tenant-retry-after': 'invalid', + }, + 3000, + ), + ).toBe(3000); + }); + + it.each(['retry-after-ms', 'x-ms-retry-after-ms', 'Retry-After'])( + 'preserves standard %s retries without a Cost Management header', + async (header) => { + const delay = header === 'Retry-After' ? 2000 : 2; + expect(await measureRetryDelay({ [header]: '2' }, delay)).toBe(delay); + }, + ); + + it.each(['retry-after-ms', 'x-ms-retry-after-ms', 'Retry-After'])( + 'does not shorten a longer standard %s delay', + async (header) => { + expect( + await measureRetryDelay({ [qpu]: '1', [header]: header === 'Retry-After' ? '3' : '3000' }, 3000), + ).toBe(3000); + }, + ); + + it('honors a longer standard HTTP-date delay', async () => { + expect( + await measureRetryDelay({ [qpu]: '1', 'Retry-After': new Date(Date.now() + 3000).toUTCString() }, 3000), + ).toBe(3000); + }); + + it('does not interpret a numeric Retry-After as a calendar year', async () => { + vi.setSystemTime(new Date('2026-12-31T23:59:59Z')); + expect(await measureRetryDelay({ [qpu]: '1', 'Retry-After': '2027' }, 2_027_000)).toBe(2_027_000); + }); + + it('does not let a shorter standard header bypass a longer Cost Management quota', async () => { + expect( + await measureRetryDelay( + { [qpu]: '3', 'retry-after-ms': '1', 'x-ms-retry-after-ms': '2', 'Retry-After': '1' }, + 3000, + ), + ).toBe(3000); + }); + + it('uses a valid Cost Management delay despite an invalid standard header', async () => { + expect(await measureRetryDelay({ [qpu]: '2', 'retry-after-ms': 'invalid' }, 2000)).toBe(2000); + }); + + it('rounds fractional milliseconds up rather than retrying early', async () => { + expect(await measureRetryDelay({ [qpu]: '0.0015' }, 2)).toBe(2); + }); + + it('supports the exact Node.js timer limit without overflow', async () => { + expect(await measureRetryDelay({ [qpu]: '2147483.647' }, 2_147_483_647)).toBe(2_147_483_647); + }); + + it.each(['', ' ', 'invalid', 'NaN', 'Infinity', '-1', '1e309', '2147483.648'])( + 'does not invent a retry delay for an invalid Cost Management header: %j', + async (value) => { + const run = await probe({ [qpu]: value }); + const response = run.start(); + await vi.runAllTimersAsync(); + expect((await response).status).toBe(429); + expect(run.sendRequest).toHaveBeenCalledOnce(); + }, + ); + + it.each([ + '/subscriptions/sub-1/providers/Microsoft.DocumentDB/databaseAccounts/account', + '/subscriptions/sub-1/providers/Microsoft.Insights/metrics', + '/subscriptions/sub-1/providers/Microsoft.CostManagementOther/query', + '/subscriptions/sub-1/locations?other=/providers/Microsoft.CostManagement/query', + ])('does not apply Cost Management headers to unrelated requests: %s', async (path) => { + const run = await probe({ [qpu]: '0' }, path); + const response = run.start(); + await vi.runAllTimersAsync(); + expect((await response).status).toBe(429); + expect(run.sendRequest).toHaveBeenCalledOnce(); + }); + + it.each([200, 400, 403, 404])( + 'does not retry or rewrite HTTP %i with a Cost Management delay', + async (status) => { + const run = await probe({ [qpu]: '2' }); + run.status.mockReset().mockReturnValue(status); + const response = run.start(); + await vi.runAllTimersAsync(); + const result = await response; + expect(result.status).toBe(status); + expect(result.headers.has('retry-after-ms')).toBe(false); + expect(run.sendRequest).toHaveBeenCalledOnce(); + }, + ); + + it('keeps the existing default limit of three retries, not a nested retry budget', async () => { + const run = await probe({ [qpu]: '0' }); + run.status.mockReset().mockReturnValue(429); + const response = run.start(); + await vi.runAllTimersAsync(); + expect((await response).status).toBe(429); + expect(run.sendRequest).toHaveBeenCalledTimes(4); + }); + + it('preserves an explicitly disabled SDK retry budget', async () => { + vi.mocked(createAzureSubscriptionClient).mockImplementationOnce((context, Client) => { + if (!Array.isArray(context) || !('credentials' in context[1])) { + throw new Error('Expected a subscription-scoped client context.'); + } + const subscription = context[1]; + return new Client(subscription.credentials, { + endpoint: subscription.environment.resourceManagerEndpointUrl, + retryOptions: { maxRetries: 0 }, + }); + }); + const run = await probe({ [qpu]: '0' }); + const response = run.start(); + await vi.runAllTimersAsync(); + expect((await response).status).toBe(429); + expect(run.sendRequest).toHaveBeenCalledOnce(); + }); + + it('retains SDK cancellation while waiting for the server delay', async () => { + const run = await probe({ [qpu]: '10' }); + const controller = new AbortController(); + run.request.abortSignal = controller.signal; + const response = run.start(); + await vi.advanceTimersByTimeAsync(1000); + expect(run.sendRequest).toHaveBeenCalledOnce(); + controller.abort(); + await expect(response).rejects.toMatchObject({ name: 'AbortError' }); + await vi.advanceTimersByTimeAsync(10_000); + expect(run.sendRequest).toHaveBeenCalledOnce(); + }); + + it('returns qualitative findings at the overall cost deadline without retrying an hours-long quota delay', async () => { + const run = await probe({ [qpu]: '3600' }); + const send = run.client.pipeline.sendRequest.bind(run.client.pipeline); + vi.spyOn(run.client.pipeline, 'sendRequest').mockImplementation((_http, request) => + send({ sendRequest: run.sendRequest }, request), + ); + const advisories: DerivedAdvisory[] = ['IdleContainer', 'PaidContinuousBackup'].map( + (rule) => { + const evidence: NonNullable = { recoverableRu: 1000 }; + if (rule === 'IdleContainer') { + evidence.throughputMode = 'manual'; + } + return { + id: rule, + rule: rule === 'IdleContainer' ? 'IdleContainer' : 'PaidContinuousBackup', + severity: 'Low', + title: rule, + rationale: 'Original rationale', + suggestedAction: 'Original action', + thresholdReference: 'Original threshold', + evidence, + }; + }, + ); + const before = structuredClone(advisories); + const monitor = new MonitorClient({ getToken: async () => null }, 'sub-1'); + const list = vi.spyOn(monitor.metrics, 'list'); + const result = enrichAdvisoryCosts({ + advisories, + cosmosClient: run.client, + monitorClient: monitor, + context: { + subscriptionId: 'sub-1', + resourceManagerEndpoint: 'https://management.azure.com', + locationNames: ['East US'], + }, + accountId: 'account', + inventoryRows: [], + now: Date.now(), + }); + await vi.advanceTimersByTimeAsync(19_999); + expect(run.sendRequest).toHaveBeenCalledOnce(); + await vi.advanceTimersByTimeAsync(1); + expect(await result).toEqual({ + available: false, + failures: [ + { operation: 'throughput', kind: 'timeout' }, + { operation: 'backup', kind: 'timeout' }, + ], + }); + expect(advisories).toEqual(before); + expect(list).not.toHaveBeenCalled(); + await vi.advanceTimersByTimeAsync(3_600_000); + expect(run.sendRequest).toHaveBeenCalledOnce(); + }); + + it('retries through the production Cost Management reader without changing its request or result', async () => { + const run = await probe({ [qpu]: '1' }); + const send = run.client.pipeline.sendRequest.bind(run.client.pipeline); + const pipelineSend = vi + .spyOn(run.client.pipeline, 'sendRequest') + .mockImplementation((_http, request) => send({ sendRequest: run.sendRequest }, request)); + try { + const result = readCostArmJson( + run.client, + { + subscriptionId: 'sub-1', + resourceManagerEndpoint: 'https://management.azure.com/', + locationNames: [], + }, + costPath, + { type: 'AmortizedCost' }, + ); + await vi.runAllTimersAsync(); + expect(await result).toEqual({}); + expect(run.sendRequest).toHaveBeenCalledTimes(2); + expect(run.sentAt[1] - run.sentAt[0]).toBe(1000); + expect(pipelineSend).toHaveBeenCalledOnce(); + expect(run.sendRequest.mock.calls[0][0].body).toBe('{"type":"AmortizedCost"}'); + } finally { + pipelineSend.mockRestore(); + } + }); + }); + it.each([ ['public', 'https://management.azure.com/'], ['sovereign', 'https://management.usgovcloudapi.net/'], @@ -86,6 +390,26 @@ describe('modular ARM client factories', () => { expect(monitorRequest.mock.calls[0][1].url).toContain(`${endpoint}subscriptions/sub-1/`); }); + it('retries Cost Management QPU throttling through the existing SDK retry policy', async () => { + const client = await createCosmosDBManagementClient(createArmTestContext(), armTestSubscription); + const sendRequest = vi.fn(async (request: PipelineRequest): Promise => ({ + request, + status: sendRequest.mock.calls.length === 1 ? 429 : 200, + headers: createHttpHeaders({ 'x-ms-ratelimit-microsoft.costmanagement-qpu-retry-after': '0' }), + bodyAsText: '{}', + })); + const response = await client.pipeline.sendRequest( + { sendRequest }, + createPipelineRequest({ + url: 'https://management.azure.com/subscriptions/sub-1/providers/Microsoft.CostManagement/query?api-version=2023-11-01', + method: 'POST', + body: '{}', + }), + ); + expect(sendRequest).toHaveBeenCalledTimes(2); + expect(response.status).toBe(200); + }); + it.each([ ['/subscriptions/sub/resourceGroups/rg/providers/Microsoft.DocumentDB/databaseAccounts/account', false, true], ['/subscriptions/sub/providers/Microsoft.DocumentDB/locations/westus/operationResults/operation', false, true], diff --git a/src/utils/azureClients.ts b/src/utils/azureClients.ts index 317cbfaed..e99496fc8 100644 --- a/src/utils/azureClients.ts +++ b/src/utils/azureClients.ts @@ -49,7 +49,7 @@ const DOCUMENT_DB_PROVIDER_PATH = '/providers/microsoft.documentdb/'; */ export const PRESERVE_API_VERSION_HEADER = 'x-vscode-cosmosdb-preserve-api-version'; -function pinCosmosDBApiVersion(client: CosmosDBManagementClient): CosmosDBManagementClient { +function configureCosmosDBPipeline(client: CosmosDBManagementClient): CosmosDBManagementClient { client.pipeline.addPolicy({ name: 'PinCosmosDBApiVersionPolicy', async sendRequest(request, next) { @@ -69,6 +69,49 @@ function pinCosmosDBApiVersion(client: CosmosDBManagementClient): CosmosDBManage return next(request); }, }); + // Normalize responses inside the existing retry policy; its attempt limits and cancellation stay unchanged. + client.pipeline.addPolicy( + { + name: 'CostManagementRetryAfterPolicy', + async sendRequest(request, next) { + const response = await next(request); + if ( + response.status === 429 && + new URL(request.url).pathname.toLowerCase().includes('/providers/microsoft.costmanagement/') + ) { + const numericDelay = (header: string, multiplier: number) => { + const value = response.headers.get(header)?.trim(); + return value ? Number(value) * multiplier : Number.NaN; + }; + // Node.js timers overflow beyond this bound and otherwise turn long delays into immediate retries. + const validDelay = (delay: number) => + Number.isFinite(delay) && delay >= 0 && delay <= 2_147_483_647; + const costDelays = ['qpu', 'entity', 'tenant', 'clienttype'] + .map((scope) => + numericDelay(`x-ms-ratelimit-microsoft.costmanagement-${scope}-retry-after`, 1000), + ) + .filter(validDelay); + if (costDelays.length > 0) { + const retryAfter = numericDelay('retry-after', 1000); + const standardDelays = [ + numericDelay('retry-after-ms', 1), + numericDelay('x-ms-retry-after-ms', 1), + retryAfter, + Number.isNaN(retryAfter) + ? Math.max(0, Date.parse(response.headers.get('retry-after') ?? '') - Date.now()) + : Number.NaN, + ].filter(validDelay); + response.headers.set( + 'retry-after-ms', + String(Math.ceil(Math.max(...costDelays, ...standardDelays))), + ); + } + } + return response; + }, + }, + { afterPhase: 'Retry' }, + ); return client; } @@ -89,7 +132,7 @@ export async function createCosmosDBManagementClient( super(credential, subscription.subscriptionId, options); } } - return pinCosmosDBApiVersion(createAzureSubscriptionClient([context, subContext], SubscriptionClient)); + return configureCosmosDBPipeline(createAzureSubscriptionClient([context, subContext], SubscriptionClient)); } export async function createFeatureClient(