Search code examples
azureazure-billing-api

Azure : Resource usage API issue


I tried to pull the Azure resource usage data for billing metrics. I followed the steps as mentioned in the blog to get Usage data of resources.

https://msdn.microsoft.com/en-us/library/azure/mt219001.aspx

Even If I set "start and endtime" parameter in the URL, its not take effect. It returns entire output [ from resource created/added time ].

For example : https://management.azure.com/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Commerce/UsageAggregates?api-version=2015-06-01-preview&reportedStartTime=2017-03-03T00%3a00%3a00%2b00%3a00&reportedEndTime=2017-03-04T00%3a00%3a00%2b00%3a00&aggregationGranularity=Hourly&showDetails=true"

As per the above URL, it should return the data between "2017-03-03 to 2017-03-04". But It shows the data from 2nd March [ 2017-03-02]. don't know why this return entire output and time filter section is not working.

Note : Endtime parameter value takes effect, mean it shows the output upto what mentioned in the endtime. But it doesn't consider the start time.

Anyone have a suggestion on this.


Solution

  • So there are a few things to consider:

    • There is usage date/time and then there is reported date/time. Former tells you the date/time when the resources were used while the latter tells you the date/time when this information was received by the billing sub-system. There will be some delay in when the resources used versus when they are reported. From this link:

    Set {dateTimeOffset-value} for reportedStartTime and reportedEndTime to valid dateTime values. Please note that this dateTimeOffset value represents the timestamp at which the resource usage was recorded within the Azure billing system. As Azure is a distributed system, spanning across 19 datacenters around the world, there is bound to be a delay between the resource usage time (when the resource was actually consumed) and the resource usage reported time (when the usage event reached the billing system) and callers need a predictable way to get all usage events for a subscription for a given time period.

    • The query only lets you search for reported date/time and there is no provision for usage date/time. However the data returned back to you contains usage date/time and not the reported date/time.

    Long story short, because of the delay in propagating the usage information to the billing sub-system, the behavior you're seeing is correct. In my experience, it takes about 24 hours for all the usage information to show up in the billing sub-system.

    The way we handle this scenario in our application is we fetch the data for a longer duration and then pick up only the data we're interested in seeing. So for example, if I need to see the data for 1st of March then we query the data for reported date/time from 1st March to say 4th March (i.e. today's date) and then discard any data where usage date is not 1st of March.

    If we don't find any data (which is quite possible and is happening in your case as well), we simply tell the users that usage information is not yet available.