A few weeks ago Toon from Your Azure Coach wrote a cool blog showing how you can use the emit policy in APIM which gave me an idea. Toons original post is here:

https://yourazurecoach.com/2021/12/16/emit-custom-metrics-from-azure-api-management/

I wanted to explore if I can use this to help me track the rate limiting on the Power Platform Dataverse when I am integrating with it through APIM. Imagine I am using a scenario like in the below picture:

We know that for CRM, throttling and rate limiting are one of the non functional constraints we have. Well each time we make a request to the Dataverse API we get back a couple of headers showing how many requests and the time window before we would encounter rate limiting.

What id like to do is to use the emit policy in the APIM policy to push out these values to the custom metrics which APIM pushes to App Insights. To do this I add the below into my outbound policy.

<outbound>
        <base />
        <emit-metric name="Dataverse-RateLimit-TimeRemaining" value="@{
            var currentRequests = context.Response.Headers.GetValueOrDefault("x-ms-ratelimit-time-remaining-xrm-requests","0");
            return System.Convert.ToDouble(currentRequests);
        }" namespace="APIM-Dataverse-Ratelimiting" />

        <emit-metric name="Dataverse-RateLimit-RemainingRequests" value="@{
            var currentRequests = context.Response.Headers.GetValueOrDefault("x-ms-ratelimit-burst-remaining-xrm-requests","0");
            return System.Convert.ToDouble(currentRequests);
        }" namespace="APIM-Dataverse-Ratelimiting" />
    </outbound>

This will mean I will get a metric for each of the values.

One thing to consider here is that I can add the policy on the API so that it will be reused on all operations.

I should now be able to get graphs showing the correlation of remaining requests and time for my proxy interface for the Dataverse API which will help me track and troubleshoot performance issues.

 

Buy Me A Coffee