type | title | excerpt | tags | share | date | |
---|---|---|---|---|---|---|
post |
Tip 301 - How to log request/response payload in Application Insights for APIs frontend by API Management |
Learn how to log request/response payload in Application Insights for APIs frontend by API Management |
|
true |
2021-01-08 09:00:00 -0800 |
::: tip
🔥 Download the FREE Azure Developer Guide eBook here.
💡 Learn more : How to integrate Azure API Management with Application Insights.
💡 Learn more : Performance implications and log sampling.
:::
This post was brought to you by Abishek Narayan.
When fronting your APIs with an Azure API Management Gateway there is often a need to debug/troubleshoot issues by analysing the request or response payload. In such a scenario we use Application Insights to log and inspect the request/response payloads. To avoid performance issues at API Management level, the configuration to inspect payloads is not enabled by default.
If you want to follow along, you'll need the following:
- An Azure subscription (If you don't have an Azure subscription, create a free account before you begin)
- Postman to trigger the sample API
- A provisioned API Management resource
- An API (with a POST operation), deployed to Azure (optional) and frontend by API Management
- An Application insights resource integrated with API Management
- For analyzing logs
- Basic understanding of Log Analytics
- Basic understanding of querying Application Insight tables (we will use the 'response' table)
- Go to the Azure portal and navigate to the API Management resource
- Under Settings >> select API
- Select the sample API from the right pane
- Click the 'Settings' tab
- Scroll down to Diagnostics Logs
- Under Application Insights tab, tick the checkbox Enable
- Select the AppInsights resource under Destination dropdown
- Default sampling 100%
- Verbosity Information
- Correlation protocol W3C
- Under Additional settings click Advanced Options
- Tick to enable Frontend Request and Frontend Response
- Use these settings for Frontend Request and Frontend Response
- Under Headers to log add the value X-Forwarded-For
- Under Number of payload bytes to log (up to 8192) specify a value upto 8192 bytes
- Hit save
- Navigate to the application insights resource and open the 'Live Metrics' feature under Investigate.
- Once the metrics have been initialized, open Postman and trigger the POST operation for the API with a sample payload. You should see a spike in the 'request rate' graph
- Wait for a few minutes, under Application Insights>>Monitoring>>Logs run the following query
requests
| where url == "http://apimanagementurl/apiname/resource"
| where timestamp >= ago(1h)
| order by timestamp desc
- In the results grid, expand the result row, expand the customDimensions node and then expand the Request-Body node.
Thanks to Todd Foust for sharing the header value.