Azure function with resource graph queries getting RateLimiting error

Aramvalartha Subramanian 0 Reputation points
2024-01-08T12:17:05.5066667+00:00

When running some resource graph queries inside a Function App I get RateLimiting error:

error in resourceGraphFunction (RateLimiting) Please provide below info when asking for support: timestamp = 2024-01-02T16:10:29.7151093Z, correlationId = bf7ffdce-2c00-49f9-8171-0a682d3e6966. Code: RateLimiting Message: Please provide below info when asking for support: timestamp = 2024-01-02T16:10:29.7151093Z, correlationId = bf7ffdce-2c00-49f9-8171-0a682d3e6966. Exception Details: (RateLimiting) Client application has been throttled and should not attempt to repeat the request until an amount of time has elapsed. Please see Overview of Azure Resource Graph - Azure Resource Graph  for help. Code: RateLimiting Message: Client application has been throttled and should not attempt to repeat the request until an amount of time has elapsed. Please see Overview of Azure Resource Graph - Azure Resource Graph  for help.

This error is not very frequent and not easily reproducible, I'm using the following python code to reproduce the issue.

Code:

import azure.functions as func
import azure.mgmt.resourcegraph as arg
from azure.identity import DefaultAzureCredential
import datetime

def custom_res(pipeline_response, deserialized, *kwargs):
    resource = deserialized
    quota_remaining = None
    quota_resets_after = None
    try:
        headers = pipeline_response.http_response.internal_response.headers
        quota_remaining = headers._store['x-ms-user-quota-remaining']
        quota_resets_after = headers._store['x-ms-user-quota-resets-after']
        status_code = pipeline_response.http_response.status_code
    except AttributeError:
        pass
    setattr(resource, 'x-ms-user-quota-remaining', quota_remaining)
    setattr(resource, 'x-ms-user-quota-resets-after', quota_resets_after)
    setattr(resource, 'status_code', status_code)
    return resource


def getPagedResources(sub_id, strQuery):
    subsList = []
    if len(sub_id) > 0:
        subsList.append(sub_id)

    arg_result_arr = []
    skip_num = 0
    result_limit = 1000
    while(True):
        argQueryOptions = arg.models.QueryRequestOptions(result_format="objectArray", top=result_limit, skip=skip_num)
        argQuery = arg.models.QueryRequest(subscriptions=subsList, query=strQuery, options=argQueryOptions)

        # Run query
        argResults = argClient.resources(argQuery, cls=custom_res)
        log.info(f"time: {datetime.datetime.now()}, remaining: {getattr(argResults, 'x-ms-user-quota-remaining', None)[1]}, status_code: {argResults.status_code}, reset_time: {getattr(argResults, 'x-ms-user-quota-resets-after', None)[1]}, skip_num: {skip_num}")
        if not argResults.data:
            break
        if not arg_result_arr:
            arg_result_arr = argResults.data
        else:
            arg_result_arr = arg_result_arr + argResults.data
        skip_num = skip_num + result_limit
    return arg_result_arr

def main(req: func.HttpRequest) -> func.HttpResponse:
    qry = "resources"
    try:
        for i in range(20):
            res = getPagedResources('123-456-789-34343',qry)
    except Exception as err:
        log.error(f"error: {err}")

Output:

Even if the user-quota-remaining

is 0, I don't get the RateLimit error that I sometimes get.

Is there any way to reproduce the issue or any fix for it ?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,994 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sourabh Gupta 800 Reputation points Microsoft Vendor
    2024-01-14T10:22:38.2833333+00:00

    Hi @Aramvalartha Subramanian Thanks for reaching out.

    This is a throttling error : It means whenever there is too much traffic on this endpoint from a single end point. Azure throws a throttling error.

    Resource Graph throttles queries at the user level. The service response contains the following HTTP headers:

    • x-ms-user-quota-remaining (int): The remaining resource quota for the user. This value maps to query count.
    • x-ms-user-quota-resets-after (hh:mm:ss): The time duration until a user's quota consumption is reset

    As an immediate solution what you can do is just write down a retry mechanism based upon the time returned in following header of the response.

    x-ms-user-quota-resets-after
    

    Since throttling is related to real time consumption of resources, There is no sure shot way to reproduce this.

    Reason why Throttling is being implemented.

    As a free service, queries to Resource Graph are throttled to provide the best experience and response time for all customers.

    If your organization wants to use the Resource Graph API for large-scale and frequent queries, use portal Feedback from the Resource Graph portal page. Provide your business case and select the Microsoft can email you about your feedback checkbox in order for the team to contact you.

    For more information on throttling, you can refer to the following link. Guidance on throttling of Azure resource If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment"

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.