azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

New-AZCostManagementDetailReport does not return when report completed

Open TrickyTic opened this issue 3 years ago • 12 comments

Description

New-AZCostManagementDetailReport never returns but continues to poll every 30 seconds after the report is completed.

Issue script & Debug output

PS E:\Temp> New-AZCostManagementDetailReport -Scope "subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX" -Metric 'ActualCost' -BillingPeriod 202301

DEBUG: [CmdletBeginProcessing]: Starting command
DEBUG: CmdletBeginProcessing:
DEBUG: CmdletProcessRecordStart:
DEBUG: CmdletGetPipeline:
DEBUG: CmdletBeforeAPICall:
DEBUG: URLCreated: /subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/providers/Microsoft.CostManagement/generateCostDetailsReport?api-version=2022-05-01
DEBUG: RequestCreated: /subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/providers/Microsoft.CostManagement/generateCostDetailsReport?api-version=2022-05-01
DEBUG: HeaderParametersAdded:
DEBUG: BodyContentSet:
DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
POST

Absolute Uri:
https://management.azure.com/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/providers/Microsoft.CostManagement/generateCostDetailsReport?api-version=2022-05-01

Headers:
x-ms-unique-id                : 1
x-ms-client-request-id        : eaa37d1e-0d45-400e-bb5c-5794c42984d6
CommandName                   : New-AZCostManagementDetailReport
FullCommandName               : New-AzCostManagementDetailReport_CreateExpanded
ParameterSetName              : __AllParameterSets
User-Agent                    : AzurePowershell/v0.0.0,PSVersion/v5.1.17763.3770,Az.CostManagement/0.3.0

Body:
{
  "metric": "ActualCost",
  "billingPeriod": "202301"
}

DEBUG: BeforeCall:
DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
Accepted

Headers:
Pragma                        : no-cache
Retry-After                   : 20
session-id                    : ce508b06-8b3b-43ac-9f80-5518d192b5ec
x-ms-request-id               : d8c78083-a2d6-4d32-84e9-88b73830cb86
OData-EntityId                : 7b00d46e-dc02-4464-829f-f9ac72e9606f
x-ms-correlation-request-id   : 44463ed0-a40c-4628-bf85-6a06a98d93bf
x-ms-client-request-id        : eaa37d1e-0d45-400e-bb5c-5794c42984d6
x-ms-ratelimit-microsoft.costmanagement-scope-request-remaining-per-day: 47
x-ms-ratelimit-microsoft.costmanagement-scope-request-remaining-per-hour: 9
x-ms-ratelimit-microsoft.costmanagement-tenant-request-remaining-per-min: 10
x-ms-ratelimit-microsoft.costmanagement-scope-request-remaining-per-min: 2
x-ms-ratelimit-remaining-subscription-reads: 11924
x-ms-routing-request-id       : CENTRALUS:20230214T151936Z:44463ed0-a40c-4628-bf85-6a06a98d93bf
Strict-Transport-Security     : max-age=31536000; includeSubDomains
X-Content-Type-Options        : nosniff
Cache-Control                 : no-cache
Date                          : Tue, 14 Feb 2023 15:19:36 GMT
Location                      :
https://management.azure.com/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/providers/Microsoft.CostManagement/costDetailsOperationResults/7b00d46e-dc02-4464-829f-f9ac72e9606f?api-version=2022-05-01
X-Powered-By                  : ASP.NET

Body:


DEBUG: ResponseCreated:
DEBUG: DelayBeforePolling: Delaying 20 seconds before polling.
DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
GET

Absolute Uri:
https://management.azure.com/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/providers/Microsoft.CostManagement/costDetailsOperationResults/7b00d46e-dc02-4464-829f-f9ac72e9606f?api-version=2022-05-01

Headers:
x-ms-unique-id                : 2
x-ms-client-request-id        : eaa37d1e-0d45-400e-bb5c-5794c42984d6
CommandName                   : New-AZCostManagementDetailReport
FullCommandName               : New-AzCostManagementDetailReport_CreateExpanded
ParameterSetName              : __AllParameterSets
User-Agent                    : AzurePowershell/v0.0.0,PSVersion/v5.1.17763.3770,Az.CostManagement/0.3.0

Body:


DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
OK

Headers:
Pragma                        : no-cache
Vary                          : Accept-Encoding
session-id                    : a64ad04b-91f1-4169-b42f-4f056acbc95c
x-ms-request-id               : 62e992cb-318f-418d-ade9-145b53728013
x-ms-correlation-request-id   : cc6d8a69-bfea-49a3-9831-d301403d7d5e
x-ms-client-request-id        : eaa37d1e-0d45-400e-bb5c-5794c42984d6
x-ms-ratelimit-remaining-subscription-reads: 11923
x-ms-routing-request-id       : CENTRALUS:20230214T151957Z:cc6d8a69-bfea-49a3-9831-d301403d7d5e
Strict-Transport-Security     : max-age=31536000; includeSubDomains
X-Content-Type-Options        : nosniff
Cache-Control                 : no-cache
Date                          : Tue, 14 Feb 2023 15:19:56 GMT
X-Powered-By                  : ASP.NET

Body:
{
  "id": "/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/providers/Microsoft.CostManagement/costDetailsOperationResults/7b00d46e-dc02-4464-829f-f9ac72e9606f",
  "name": "7b00d46e-dc02-4464-829f-f9ac72e9606f",
  "status": "Completed",
  "manifest": {
    "manifestVersion": "2022-05-01",
    "dataFormat": "Csv",
    "byteCount": 92008511,
    "blobCount": 1,
    "compressData": false,
    "requestContext": {
      "requestScope": "/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/",
      "requestBody": {
        "metric": "ActualCost",
        "invoiceId": null,
        "billingPeriod": "202301"
      }
    },
    "blobs": [
      {
        "blobLink":
"https://ccmreportstoragenortheu3.blob.core.windows.net/armreports/20230214/7b00d46e-dc02-4464-829f-f9ac72e9606f?sv=2016-05-31&sr=b&sig=XXXXXXXXXXvxm9z2%2BConrSSwq8GnwQpN53P0vxUVTdDmU%3D&spr=https&st=2023-02-14T15%3A14%3
A57Z&se=2023-02-15T03%3A19%3A57Z&sp=r",
        "byteCount": 92008511
      }
    ]
  },
  "validTill": "2023-02-15T03:19:57.4923235Z"
}

DEBUG: Polling:
DEBUG: DelayBeforePolling: Delaying 30 seconds before polling.
DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
GET

Absolute Uri:
https://management.azure.com/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/providers/Microsoft.CostManagement/costDetailsOperationResults/7b00d46e-dc02-4464-829f-f9ac72e9606f?api-version=2022-05-01

Headers:
x-ms-unique-id                : 3
x-ms-client-request-id        : eaa37d1e-0d45-400e-bb5c-5794c42984d6
CommandName                   : New-AZCostManagementDetailReport
FullCommandName               : New-AzCostManagementDetailReport_CreateExpanded
ParameterSetName              : __AllParameterSets
User-Agent                    : AzurePowershell/v0.0.0,PSVersion/v5.1.17763.3770,Az.CostManagement/0.3.0

Body:


DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
OK

Headers:
Pragma                        : no-cache
Vary                          : Accept-Encoding
session-id                    : 0ca17d1f-b322-4baf-aace-90773fa2e3d0
x-ms-request-id               : 3deab2e8-7f1b-4f32-9bb3-48b18f60da98
x-ms-correlation-request-id   : ca664c49-c1b1-4938-96f8-137de60ed933
x-ms-client-request-id        : eaa37d1e-0d45-400e-bb5c-5794c42984d6
x-ms-ratelimit-remaining-subscription-reads: 11922
x-ms-routing-request-id       : CENTRALUS:20230214T152028Z:ca664c49-c1b1-4938-96f8-137de60ed933
Strict-Transport-Security     : max-age=31536000; includeSubDomains
X-Content-Type-Options        : nosniff
Cache-Control                 : no-cache
Date                          : Tue, 14 Feb 2023 15:20:27 GMT
X-Powered-By                  : ASP.NET

Body:
{
  "id": "/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/providers/Microsoft.CostManagement/costDetailsOperationResults/7b00d46e-dc02-4464-829f-f9ac72e9606f",
  "name": "7b00d46e-dc02-4464-829f-f9ac72e9606f",
  "status": "Completed",
  "manifest": {
    "manifestVersion": "2022-05-01",
    "dataFormat": "Csv",
    "byteCount": 92008511,
    "blobCount": 1,
    "compressData": false,
    "requestContext": {
      "requestScope": "/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/",
      "requestBody": {
        "metric": "ActualCost",
        "invoiceId": null,
        "billingPeriod": "202301"
      }
    },
    "blobs": [
      {
        "blobLink":
"https://ccmreportstoragenortheu3.blob.core.windows.net/armreports/20230214/7b00d46e-dc02-4464-829f-f9ac72e9606f?sv=2016-05-31&sr=b&sig=XXXXXXXXXXrc1V6zN1tsV9cDcbEeialitxGD%2B%2B0nI2w%3D&spr=https&st=2023-02-14T15%3A15%3
A28Z&se=2023-02-15T03%3A20%3A28Z&sp=r",
        "byteCount": 92008511
      }
    ]
  },
  "validTill": "2023-02-15T03:20:28.1051443Z"
}

DEBUG: Polling:
DEBUG: DelayBeforePolling: Delaying 30 seconds before polling.
DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
GET

Absolute Uri:
https://management.azure.com/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/providers/Microsoft.CostManagement/costDetailsOperationResults/7b00d46e-dc02-4464-829f-f9ac72e9606f?api-version=2022-05-01

Headers:
x-ms-unique-id                : 4
x-ms-client-request-id        : eaa37d1e-0d45-400e-bb5c-5794c42984d6
CommandName                   : New-AZCostManagementDetailReport
FullCommandName               : New-AzCostManagementDetailReport_CreateExpanded
ParameterSetName              : __AllParameterSets
User-Agent                    : AzurePowershell/v0.0.0,PSVersion/v5.1.17763.3770,Az.CostManagement/0.3.0

Body:


DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
OK

Headers:
Pragma                        : no-cache
Vary                          : Accept-Encoding
session-id                    : fec4c6c7-9a99-4db4-8350-03b0f7ff0fdf
x-ms-request-id               : dd061e79-fd7c-4474-9c2b-d020a63b370b
x-ms-correlation-request-id   : bd998863-cdec-411f-a98b-bbf1e7827f0c
x-ms-client-request-id        : eaa37d1e-0d45-400e-bb5c-5794c42984d6
x-ms-ratelimit-remaining-subscription-reads: 11921
x-ms-routing-request-id       : CENTRALUS:20230214T152058Z:bd998863-cdec-411f-a98b-bbf1e7827f0c
Strict-Transport-Security     : max-age=31536000; includeSubDomains
X-Content-Type-Options        : nosniff
Cache-Control                 : no-cache
Date                          : Tue, 14 Feb 2023 15:20:58 GMT
X-Powered-By                  : ASP.NET

Body:
{
  "id": "/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/providers/Microsoft.CostManagement/costDetailsOperationResults/7b00d46e-dc02-4464-829f-f9ac72e9606f",
  "name": "7b00d46e-dc02-4464-829f-f9ac72e9606f",
  "status": "Completed",
  "manifest": {
    "manifestVersion": "2022-05-01",
    "dataFormat": "Csv",
    "byteCount": 92008511,
    "blobCount": 1,
    "compressData": false,
    "requestContext": {
      "requestScope": "/subscriptions/9af2fe58-e215-4e90-8595-XXXXXXXXXXXX/",
      "requestBody": {
        "metric": "ActualCost",
        "invoiceId": null,
        "billingPeriod": "202301"
      }
    },
    "blobs": [
      {
        "blobLink":
"https://ccmreportstoragenortheu3.blob.core.windows.net/armreports/20230214/7b00d46e-dc02-4464-829f-f9ac72e9606f?sv=2016-05-31&sr=b&sig=XXXXXXXXXXWLnnBDOzkYibnhU2R7wgcLgUyDi6B7epk%3D&spr=https&st=2023-02-14T15%3A15%3A58Z
&se=2023-02-15T03%3A20%3A58Z&sp=r",
        "byteCount": 92008511
      }
    ]
  },
  "validTill": "2023-02-15T03:20:58.8150825Z"
}

DEBUG: Polling:
DEBUG: DelayBeforePolling: Delaying 30 seconds before polling.

Environment data

PS E:\Temp> $PSVERSIONTABLE

Name                           Value
----                           -----
PSVersion                      5.1.17763.3770
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.3770
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module versions

PS E:\Temp> Get-Module Az*

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.11.2     Az.Accounts                         {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault...}
Script     0.3.0      Az.CostManagement                   {Get-AzCostManagementExport, Get-AzCostManagementExportExecutionHistory, Invoke-AzCostManagementExecuteExport, Invoke-AzCostManagementQuery...}

Error output

PS E:\Temp> Resolve-AzError
DEBUG: 10:29:17 - ResolveError begin processing with ParameterSet 'AnyErrorParameterSet'.
DEBUG: 10:29:17 - using account id '[email protected]'...
DEBUG: 10:29:17 - [ConfigManager] Got nothing from [DisplayBreakingChangeWarning], Module = [], Cmdlet = []. Returning default value [True].
WARNING: Upcoming breaking changes in the cmdlet 'Resolve-AzError' :
The `Resolve-Error` alias will be removed in a future release.  Please change any scripts that use this alias to use `Resolve-AzError` instead.
Note : Go to https://aka.ms/azps-changewarnings for steps to suppress this breaking change warning, and other information on breaking changes in Azure PowerShell.


DEBUG: AzureQoSEvent:  Module: Az.Accounts:2.11.2; CommandName: Resolve-AzError; PSVersion: 5.1.17763.3770; IsSuccess: True; Duration: 00:00:00.0188817
DEBUG: 10:29:17 - [ConfigManager] Got [True] from [EnableDataCollection], Module = [], Cmdlet = [].
DEBUG: 10:29:17 - ResolveError end processing.

TrickyTic avatar Feb 14 '23 15:02 TrickyTic

The root cause is API returned

"status": "Completed",

But "Completed" is not a legitimate terminal state, so we just kept polling. @dolauli to comment how do we proceed.

isra-fel avatar Feb 15 '23 10:02 isra-fel

@TiagoCrewGitHubIssues hi, please take a look at this issue. We believed the Get costDetailsOperationResults API returned an non-legitimate terminal state. It should have been "Succeeded" instead of "Completed". Hence causing the PowerShell client to keep polling.

isra-fel avatar Feb 16 '23 02:02 isra-fel

Having the same issue here - how did you get the debug output showing the HTTP response and body?

GeorgeMansoMSFT avatar Jun 09 '23 22:06 GeorgeMansoMSFT

@GeorgeMansoMSFT $DebugPreference = 'Continue' enables debug log.

isra-fel avatar Jun 12 '23 01:06 isra-fel

@isra-fel this is still broken in v 0.3.1 . I have made my request and it is stuck in a forever polling loop but status has been set to complete in the response body

Kenneth-Abrams avatar Apr 09 '24 17:04 Kenneth-Abrams

@isra-fel is there any workaround or fix for the Cmdlet available? Since the Azure Enterprise Reporting APIs have been retired, this Cmdlet would be helpful for migrating to the Cost Management APIs.

rfc821 avatar May 26 '24 08:05 rfc821

Simply add -NoWait to the end. For example. New-AzCostManagementDetailReport -Scope "/subscriptions/xxxxxxxxxxxxxxxxxx" -Metric 'ActualCost' -TimePeriodStart "2024-04-01" -TimePeriodEnd "2024-05-01" -NoWait

VictorMeyerIdea11 avatar May 27 '24 02:05 VictorMeyerIdea11

I've tried that but maybe I missed something: When requesting this report using the -NoWait, is there a Cmdlet to download the report afterwards?

rfc821 avatar May 27 '24 07:05 rfc821

Here you go mate. Cheap and cheerful. Expand it with async API handling, error checking and handling multiple blobs but this will get you where you need to go.

This will: -

  1. Create a new details request.
  2. Retrieve the results of that request.
  3. Download the report as a csv.
  4. Reimport it into an array so you can consume the report as needed.

$NewReport = New-AzCostManagementDetailReport -Scope "/subscriptions/xxxxxxxxxxxxxxx" -Metric 'ActualCost' -TimePeriodStart "2024-04-01" -TimePeriodEnd "2024-05-01" -NoWait $CompletedReport = Invoke-RestMethod -Uri $NewReport.Target -Headers @{ "Authorization" = "Bearer $((Get-AzAccessToken).Token)" } Invoke-WebRequest -URI $CompletedReport.manifest.blobs[0].blobLink -OutFile C:\Temp\DetailReport.csv $ReportDetails = Import-Csv C:\Temp\DetailReport.csv $ReportDetails.Count

VictorMeyerIdea11 avatar May 27 '24 22:05 VictorMeyerIdea11

Thanks @VictorMeyerIdea11

rfc821 avatar Jun 01 '24 07:06 rfc821

This issue persists in the latest Az.CostManagement 0.3.4. Could someone in the development team please confirm if it is being tracked?

https://www.powershellgallery.com/packages/Az.CostManagement/0.3.4

yaegashi avatar Feb 26 '25 04:02 yaegashi

Additionally, there is no way to know the retry back-off seconds (Retry-After header) when the API hits the rate limit and returns the status code 429 (Too Many Requests). The cmdlet simply returns $null and outputs the response body as shown below, then stops execution (according to -ErrorAction) regardless of whether -NoWait is specified or not.

> $a = New-AzCostManagementDetailReport -Scope $Scope -BillingPeriod $BillingPerios
New-AzCostManagementDetailReport_CreateExpanded: Too many requests. Please retry after 5 minute(s) and 57 second(s)

yaegashi avatar Feb 26 '25 10:02 yaegashi