Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
APPLIES TO: All API Management tiers
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 Application Insights integration into API Management.
- Learn strategies for reducing performance impact on your API Management service instance.
Note
In an API Management workspace, a workspace owner can independently integrate Application Insights and enable Application Insights logging for the workspace's APIs. The general guidance to integrate a workspace with Application Insights is similar to the guidance for an API Management instance; however, configuration is scoped to the workspace only. Currently, you must integrate Application Insights in a workspace by configuring a connection string (recommended) or an instrumentation key.
Warning
When using our self-hosted gateway, we don't guarantee all telemetry is pushed to Azure Application Insights since it relies on Application Insights' in-memory buffering.
Prerequisites
Create an Azure API Management instance.
Create an instance of the Application Insights service. To create an instance by using the Azure portal, see Workspace-based Application Insights resources.
Note
The Application Insights resource can be in a different subscription or even a different tenant than the API Management resource.
If you plan to configure managed identity credentials to use with Application Insights, complete the following steps:
Enable a system-assigned or user-assigned managed identity for API Management.
- If you enable a user-assigned managed identity, take note of the identity's Client ID.
Assign the identity the Monitoring Metrics Publisher role, scoped to the Application Insights resource. To assign the role, use the Azure portal or other Azure tools.
Scenario overview
The following steps provide a high-level overview of this scenario.
Create a connection between Application Insights and API Management.
You can create a connection between Application Insights and your API Management by using the Azure portal, the REST API, or related Azure tools. API Management configures a logger resource for the connection.
Important
Currently, in the portal, API Management only supports connections to Application Insights using an Application Insights instrumentation key. For enhanced security, use an Application Insights connection string with an API Management managed identity. To configure connection string with managed identity credentials, use the REST API or related tools as shown in a later section of this article. To learn more, see Connection strings in Application Insights.
Note
If your Application Insights resource is in a different tenant, create the logger by using the REST API or related tools as shown in a later section of this article.
Enable Application Insights logging for your API or APIs.
In this article, you enable Application Insights logging for your API by using the Azure portal. API Management configures a diagnostic resource for the API.
Create a connection by using the Azure portal
Follow these steps to use the Azure portal to create a connection between Application Insights and API Management.
Note
Where possible, Microsoft recommends using connection string with managed identity credentials for enhanced security. To configure these credentials, use the REST API or related tools as shown in a later section of this article.
Go to your Azure API Management service instance in the Azure portal.
Under Monitoring in the sidebar menu, select Application Insights.
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 checks 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.
Note
Behind the scenes, a logger entity is created in your API Management instance, containing the instrumentation key of the Application Insights instance.
Tip
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.
Create a connection by using the REST API, Bicep, or ARM template
Follow these steps to use the REST API, Bicep, or ARM template to create an Application Insights logger for your API Management instance. You can configure a logger that uses a connection string with managed identity credentials (recommended), or a logger that uses only a connection string.
Logger with connection string and managed identity credentials (recommended)
See the prerequisites for using an API Management managed identity.
The Application Insights connection string appears in the Overview section of your Application Insights resource.
Connection string with system-assigned managed identity
Use the API Management Logger - Create or Update REST API with the following request body.
{
"properties": {
"loggerType": "applicationInsights",
"description": "Application Insights logger with system-assigned managed identity",
"credentials": {
"connectionString":"InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://xxxx.applicationinsights.azure.com/;...",
"identityClientId":"SystemAssigned"
}
}
}
Connection string with user-assigned managed identity
Use the API Management Logger - Create or Update REST API with the following request body.
{
"properties": {
"loggerType": "applicationInsights",
"description": "Application Insights logger with user-assigned managed identity",
"credentials": {
"connectionString":"InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://xxxx.applicationinsights.azure.com/;...",
"identityClientId":"<client-ID>"
}
}
}
Logger with connection string credentials only
The Application Insights connection string appears in the Overview section of your Application Insights resource.
Use the API Management Logger - Create or Update REST API with the following request body.
If you're configuring the logger for a workspace, use the Workspace Logger - Create or Update REST API.
{
"properties": {
"loggerType": "applicationInsights",
"description": "Application Insights logger with connection string",
"credentials": {
"connectionString":"InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://xxxx.applicationinsights.azure.com/;..."
}
}
}
Enable Application Insights logging for your API
Use the following steps to enable Application Insights logging for an API. You can also enable Application Insights logging for all APIs.
Go to your Azure API Management service instance in the Azure portal.
Select APIs > APIs from the sidebar menu.
Select an API, such as Swagger Petstore. If configured, select a version.
Tip
To enable logging for all APIs, select All APIs.
Select the Settings tab on the top bar.
Scroll down to the Diagnostics Logs section, and check the Enable box.
Select your attached logger in the Destination dropdown.
Enter
100as 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.
Warning
Overriding the default Number of payload bytes to log value
0might significantly decrease the performance of your APIs.Select Save.
Behind the scenes, a Diagnostic entity named
applicationinsightsis created at the API level.
Note
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 at different levels:
- Single API logger
- A logger for all APIs
If you specify both:
- By default, the single API logger (more granular level) overrides the one for all APIs.
- If the loggers configured at the two levels are different, and you need both loggers to receive telemetry (multiplexing), contact Microsoft Support. Multiplexing isn't supported if you're using the same logger (Application Insights destination) at the "All APIs" level and the single API level. For multiplexing to work correctly, you must configure different loggers at the "All APIs" and individual API level and request assistance from Microsoft support to enable multiplexing for your service.
What data is added to Application Insights
Application Insights receives:
| Telemetry item | Description |
|---|---|
| Request | For every incoming request:
|
| Dependency | Automatic external dependency telemetry from supported policy execution, including requests forwarded to a backend service. |
| Exception | For every failed request:
|
| Trace | If you configure a trace policy. The severity setting in the trace policy must be equal to or greater than the verbosity setting in the Application Insights logging. |
External dependency telemetry for supported policies
After you configure and enable Application Insights logging, API Management automatically collects external dependency telemetry for the following policies. No additional policy or telemetry configuration is required:
authentication-managed-identityauthentication-tokenazure-openai-semantic-cache-lookupcosmosdb-request-handlerforward-requestget-authorization-contexthttp-data-sourceinvoke-dapr-bindingllm-content-safetyllm-semantic-cache-lookupsend-requestsend-one-way-requestsend-service-bus-messagesql-data-sourcevalidate-jwt
Application Insights records these external calls as correlated child operations. For example, you can see retry attempts, OpenID Connect metadata retrieval by validate-jwt, a Service Bus send, or backend forwarding as related operations. To verify the telemetry in the Azure portal, open End-to-end transaction details in your Application Insights resource and review the related operations for a request.
Currently, dependency tracking is supported only for the policies in the preceding list.
Note
For information about the maximum size and number of metrics and events per Application Insights instance, see Application Insights limits.
Performance implications and log sampling
Warning
Logging all events might 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
Troubleshooting
Addressing the issue of telemetry data flow from API Management to Application Insights:
- Investigate whether a linked Azure Monitor Private Link Scope (AMPLS) resource exists within the virtual network where the API Management resource is connected. AMPLS resources have a global scope across subscriptions and are responsible for managing data query and ingestion for all Azure Monitor resources. It's possible that the AMPLS was configured with a Private-Only access mode specifically for data ingestion. In such instances, include the Application Insights resource and its associated Log Analytics resource in the AMPLS. Once this addition is made, the API Management data will be successfully ingested into the Application Insights resource, resolving the telemetry data transmission issue.