Azure API Management allows for easy integration with Azure Application Insights - an extensible service for web developers building and managing apps on multiple platforms. This guide walks through every step of such an integration and describes strategies for reducing performance impact on your Azure API Management instance.
Before you can use Azure Application Insights, you first need to create an instance of the service. The naming convention for Application Insights is:
Open the Azure portal and navigate to Application Insights.
Click + Create, then fill in the form. You may need to create a new Log Analytics Workspace if you don’t already have one.
Click Review + Create, followed by Create.
- Navigate to your APIM instance in the Azure portal.
- Select Application Insights from the menu on the left.
Click + Add.
- Select the previously created Application Insights instance and provide a short description.
- Click Create.
You have just created an Azure Application Insights logger with an instrumentation key. It should now appear in the list.
Behind the scenes, a Logger entity is created in your Azure API Management instance, containing the Connection String of the Application Insights instance.
- Select APIs from the menu on the left.
- Click on the Colors API.
- Go to the Settings tab from the top bar.
- Scroll down to the Diagnostics Logs section.
- On the Application Insights tab check the Enable box.
- In the Destination dropdown select the logger you just added in the Application Insights blade.
- Set sampling to 100 to capture all events.
Check the Always log errors checkbox.
Overriding the default value 0 in the Number of payload bytes to log field may significantly decrease the performance of your APIs.
Behind the scenes, a Diagnostic entity named ‘applicationinsights’ is created at the API level.
Azure Application Insights receives:
- Request telemetry item for every incoming request (frontend request, frontend response),
- Dependency telemetry item for every request forwarded to a backend service (backend request, backend response),
- Exception telemetry item, for every failed request.
A failed request is a request which:
- failed because of a closed client connection, or
- triggered an on-error section of the API policies, or
- has a response HTTP status code matching 4xx or 5xx.
Any request you make to the Colors API in Azure API Management will be subject to being received by Application Insights (recall the 100% sampling). To generate a large amount of requests quickly, you can invoke the API via the Colors website. As we are presently caching the output for 15 seconds, you may get a lot of requests with the same color. Please feel free to disable the output caching in the Colors API if you would like to see more variety.
- Go back to the Application Insights blade and click on the Application Insights instance.
- In the Application Insights instance, you should be able to see logs and metrics after a few seconds.
Open the Transaction search to see details on a transactional level:
Logging all events may have serious performance implications, depending on incoming requests rate, payload size, etc.
Based on internal load tests, enabling this feature caused a 40%-50% reduction in throughput when request rate exceeded 1,000 requests per second. Azure Application Insights is designed to use statistical analysis for assessing application performances. It is not intended to be an audit system and is not suited for logging each individual request for high-volume APIs.
You can manipulate the number of requests being logged by adjusting the Sampling setting (see the steps above). 100% means all requests are logged, while 0% reflects no logging at all. Sampling helps to reduce the volume of telemetry, effectively preventing from significant performance degradation, while still carrying the benefits of logging. Sampling is an effective tool in diagnosing often general operational issues. For example, sampling can identify connectivity or integration issues as these would often occur in high quantity, not singular instances. A sampling rate of 50% is as effective in diagnosing such issues as 100% is.
Skipping logging of headers and body of requests and responses will also have positive impact on alleviating performance issues.