Fix Cost Management 429 retry logic and reduce query weight#2
Open
bychkov wants to merge 1 commit into
Open
Conversation
- The retry/backoff for HTTP 429 was dead code: Invoke-AzRestMethod returns 429 as a normal response (no exception thrown), so the catch block never fired and the script always failed immediately on the first 429 with no retry. - Move throttling detection inside the retry loop, checking StatusCode directly so 429 and 5xx responses are retried correctly. - Add Get-RetryAfterSeconds helper that reads the typed RetryAfter property on HttpResponseHeaders (Delta / Date) and falls back to raw Retry-After / x-ms-retry-after-ms header enumeration, so the script waits exactly as long as Azure requests instead of guessing. - Switch Cost Management query granularity from Daily (~365 rows/yr) to Monthly (12 rows/yr). The script already aggregated daily rows to months in PowerShell; letting the API do it is strictly lighter and reduces consumption of the small per-subscription rate budget. - Keep per-calendar-year chunking: the Query API hard-caps each request to a 1-year time range, so multi-year ranges still need one request per year.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Invoke-AzRestMethodreturns 429 as a normal response object (no exception thrown), so thecatchblock retry path never ran. The script always failed immediately on the first throttle. Fixed by detecting 429/5xx fromStatusCodeinside the loop.Retry-Afternow honored — AddedGet-RetryAfterSecondsthat reads the typedRetryAfterproperty onHttpResponseHeaders(Delta/Date) and falls back to rawRetry-After/x-ms-retry-after-msheader enumeration, so the script waits exactly as long as Azure requests instead of guessing.Daily(~365 rows/yr, aggregated in script) toMonthly(12 rows/yr, aggregated by Azure). Reduces per-call weight against the small per-subscription rate budget.Test plan
pwsh -File .\Get-AzureMonthlyCosts.ps1 -YearRange 2025 -SubscriptionId <id>— should return 12 monthly rows-YearRange 2025:2026— should return rows for both years (2 API calls)Retrying in N seconds...and eventually succeed rather than failing immediately