Arc Jumpstart Newsletter: April 2025 Edition
May 7, 2025Disable Local Authentication in Azure bot
May 7, 2025Scenario:
In case of encountering error in API management, request tracing is an invaluable feature that serves as a debugger. It allows for tracking the flow of requests as they pass through various policy logic, providing detailed insights into the complete API Management (APIM) processing. Here is a link if you would like to read more on how to enable request tracing in API management.
However, it is the most common way to debug your API, let’s assume a real-life scenario where you encounter a sporadic error or unexpected response while processing the live APIM calls and need to drill down the issue. In such cases, attaching a debugger or running request traces can be challenging, especially when the issue is intermittent or requires checking the specific code logic. This often necessitates a trial-and-error approach to reproduce the scenario and obtain the traces. To address these challenges, the APIM Trace policy can be utilized. This policy enables the addition of custom traces to Application Insights and/or to resource logs.
This blog can help users to use the APIM Trace policy which can be leveraged to sort out such kind of issues. The following snippet demonstrates how to use traces with Application Insights.
Pre-Requirements:
Before we start, we could make sure that we have existing Azure API Management service and App Insight setup for logging these requests.
Steps:
Once you are done with your APIM setup make sure your diagnostics setting is correctly setup and it is logging your API calls, you can verify the same by going to App Insight instance and running the queries for “requests” table.
- Now let’s consider a scenario where we may need to check on the value of remaining calls which will be set based on certain input parameters (or any other logic on your policy code). To do the same go to your API’s>Design> “Your API”.
- I did setup the verbosity as Verbose here to log the APIM request to App insight and we will be using the trace policy code to add custom trace information to the request tracing output as shown below.
- As an example, we will be capturing the traces for the selected API only. The below example is for illustration purpose only here we are trying to log remaining calls under trace variable “callremain“. We will be leveraging rate limit policy to check number of calls left and adding a custom trace information. Here is a policy code for the same
@(String.Format(“{0} | {1}”, “ip address”, context.Request.IpAddress))
<metadata name="callremain" value="@{
var calls = context.Variables.GetValueOrDefault(“remainingCallsPerIP”);
return calls.ToString();
}” />
- Once you save the above policy code run the API calls using header x-env the rate limit policy only allows 5 calls per 30 seconds here and will log the traces with message and call remain variable.
- Next Go to App insight(your app insight instance) >Monitoring>Logs>traces
- You can filter traces based on your Kusto like API name or custom dimension however here for the simplicity for our sample call filter is based on message contains “ip address”. Please note it is the message metadata we added in our trace policy and call remain variable which you can track based on your business logic.
traces
| where message contains “ip address”
- Keep calling the API, for example using VS Code or any other tool to make continuous calls to API and capture the variable “callremain“. Here the trace policy helps to identify how many calls left in the rate limit policy by logging custom messages and metadata to the trace output.
- This will provide a way to look deep into your code logic or if you would like to track any variables in your APIM code. You can add more metadata in the policy as per your debug requirements. Here is another quick example to save your request and change the severity as informational in the trace log.
Conclusion: The above steps outlined will give you an idea on how to use this policy to add custom messages and metadata to the trace output, which can help in debugging and troubleshooting API’s by providing detailed information about request processing steps. This includes inbound requests, backend interactions, and outbound responses. I hope this blog provided you with information to better grasp what happens when an API is called, and how trace policy can help you in troubleshooting and debugging your APIM calls.