-
Notifications
You must be signed in to change notification settings - Fork 4k
Invoke-AzCostManagementQuery
often does not return results on first try. Throttling?
#27734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Seems I get throttled. Az should handle that, right? Seems it does not in this case. {
"error": {
"code": "429",
"message": "Too many requests. Please retry."
}
} |
Invoke-AzCostManagementQuery
often does not return results on first tryInvoke-AzCostManagementQuery
often does not return results on first try. Throttling?
Made a workaround using the API directly with If header # Invoke cost query with failproofing and throttling handling
do {
$Results = Invoke-AzRestMethod -Method 'Post' -WhatIf:$false -Path (
'/subscriptions/{0}/providers/Microsoft.CostManagement/query?api-version=2019-11-01' -f $SubscriptionId
) -Payload (
ConvertTo-Json -Compress -Depth 4 -InputObject @{
'timePeriod' = @{
'from' = [string] $From.ToString('o')
'to' = [string] $To.ToString('o')
}
'dataset' = @{
'aggregation' = @{
'totalCost' = @{
'name' = [string] 'Cost'
'function' = [string] 'SUM'
}
}
}
'type' = [string] 'ActualCost'
'timeframe' = [string] 'Custom'
}
)
if ($Results.'StatusCode' -eq 429) {
Start-Sleep -Seconds ($Results.'Headers'.'x-ms-ratelimit-microsoft.costmanagement-entity-retry-after' ?? 5)
}
elseif ($Results.'StatusCode' -ne 200) {
Throw ('Request returned status code {0} (means "{1}")' -f $Results.'StatusCode', [System.Net.HttpStatusCode]($Results.'StatusCode'))
}
}
until ($Results.StatusCode -eq 200)
# Return results
(ConvertFrom-Json -InputObject $Results.'Content').'properties' |
cc @dolauli |
We will retry only when |
So the resource provider does not follow Azure RM API guidelines? Maybe throw an error or warning on 429 without the expected header? Instead of just returning nothing. |
Description
I experience that
Invoke-AzCostManagementQuery
often does not return results on first try. Seems the Rest API endpoint it uses is unstable. If I retry once or sometimes even twice, the success rate becomes better.Can Az do something about this? Add some retry logic or something?
Issue script & Debug output
Environment data
Module versions
Error output
The text was updated successfully, but these errors were encountered: