How to integrate Azure API Management with Azure Application Insights
You can easily integrate Azure Application Insights with Azure API Management. Azure Application Insights is an extensible service for web developers building and managing apps on multiple platforms. In this guide, you will:
- Walk through every step of the Application Insights integration into API Management.
- Learn strategies for reducing performance impact on your API Management service instance.
You need an Azure API Management instance. Create one first.
Create an Application Insights instance
To use Application Insights, create an instance of the Application Insights service. To create an instance using the Azure portal, see Workspace-based Application Insights resources.
The Application Insights resource can be in a different subscription or even a different tenant than the API Management resource.
Create a connection between Application Insights and API Management
If your Application Insights resource is in a different tenant, then you will have to create the logger using the REST API
- Navigate to your Azure API Management service instance in the Azure portal.
- Select Application Insights from the menu on the left.
- Select + Add.
- Select the Application Insights instance you created earlier and provide a short description.
- To enable availability monitoring of your API Management instance in Application Insights, select the Add availability monitor checkbox.
- This setting regularly validates whether the API Management gateway endpoint is responding.
- Results appear in the Availability pane of the Application Insights instance.
- Select Create.
- Check that the new Application Insights logger now appears in the list.
Behind the scenes, a Logger entity is created in your API Management instance, containing the instrumentation key of the Application Insights instance.
If you need to update the instrumentation key configured in the Application Insights logger, select the logger's row in the list (not the name of the logger). Enter the instrumentation key, and select Save.
Enable Application Insights logging for your API
Navigate to your Azure API Management service instance in the Azure portal.
Select APIs from the menu on the left.
Click on your API, in this case Demo Conference API. If configured, select a version.
Go to the Settings tab from the top bar.
Scroll down to the Diagnostics Logs section.
Check the Enable box.
Select your attached logger in the Destination dropdown.
Input 100 as Sampling (%) and select the Always log errors checkbox.
Leave the rest of the settings as is. For details about the settings, see Diagnostic logs settings reference.
Overriding the default Number of payload bytes to log value 0 may significantly decrease the performance of your APIs.
Behind the scenes, a Diagnostic entity named
applicationinsightsis created at the API level.
Requests are successful once API Management sends the entire response to the client.
Loggers for a single API or all APIs
You can specify loggers on different levels:
- Single API logger
- A logger for all APIs
- By default, the single API logger (more granular level) will override the one for all APIs.
- If the loggers configured at the two levels are different, and you need both loggers to receive telemetry (multiplexing), please contact Microsoft Support.
What data is added to Application Insights
Application Insights receives:
|Request||For every incoming request:
|Dependency||For every request forwarded to a backend service:
|Exception||For every failed request:
|Trace||If you configure a trace policy.
Emit custom metrics
You can emit custom metrics by configuring the
To make Application Insights pre-aggregated metrics available in API Management, you'll need to manually enable custom metrics in the service.
- Use the
emit-metricpolicy with the Create or Update API.
"metrics":trueto the payload, along with any other properties.
See Application Insights limits for information about the maximum size and number of metrics and events per Application Insights instance.
Performance implications and log sampling
Logging all events may have serious performance implications, depending on incoming requests rate.
Based on internal load tests, enabling the logging feature caused a 40%-50% reduction in throughput when request rate exceeded 1,000 requests per second. Application Insights is designed to assess application performances using statistical analysis. It's not:
- Intended to be an audit system.
- Suited for logging each individual request for high-volume APIs.
You can manipulate the number of logged requests by adjusting the Sampling setting. A value of 100% means all requests are logged, while 0% reflects no logging.
Sampling helps to reduce telemetry volume, effectively preventing significant performance degradation while still carrying the benefits of logging.
To improve performance issues, skip:
- Request and responses headers.
- Body logging.
- Learn more about Azure Application Insights.
- Consider logging with Azure Event Hubs.
- Learn about visualizing data from Application Insights using Azure Managed Grafana
Submit and view feedback for