View Post Request Body in Application Insights

View POST request body in Application Insights

You can simply implement your own Telemetry Initializer:

For example, below an implementation that extracts the payload and adds it as a custom dimension of the request telemetry:

public class RequestBodyInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry != null && (requestTelemetry.HttpMethod == HttpMethod.Post.ToString() || requestTelemetry.HttpMethod == HttpMethod.Put.ToString()))
{
using (var reader = new StreamReader(HttpContext.Current.Request.InputStream))
{
string requestBody = reader.ReadToEnd();
requestTelemetry.Properties.Add("body", requestBody);
}
}
}
}

Then add it to the configuration either by configuration file or via code:

TelemetryConfiguration.Active.TelemetryInitializers.Add(new RequestBodyInitializer());

Then query it in Analytics:

requests | limit 1 | project customDimensions.body

View Request Body in Application Insights

I can't do code changes in existing api. any option without code change would help me

Unfortunately, it is not supported by Application Insights.

I also find the feedback, you could vote it.

It now supports custom Telemetry Initializer as I have shown to you.

View POST request body in Application Insights (JAVA example)

TelemetryModules track various data w.r.t HTTP request and send the same to AI servers as RequestTelemetry. In order to track any custom HTTP parameters one has to create a new TelemetryModule by Implementing WebTelemetryModule and TelemetryModule interface that comes part of app insight sdk.

Here is the sample implementation.

Sample Image

WebTelemetry modules are the ones that has access to the HttpRequest and HttpResponse objects in the Request lifecycle. Basically AI collects the request telemetry by registering a Servlet fitler and onBeginRequest gets called before the actual request is processed and onEndRequest gets called after the request is processed.

Now register the module in ApplicationInsights.xml file

Sample Image

<TelemetryModules>
<Add type="com.ai.demo.CustomHttpTelemetryModule"/>
<Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebRequestTrackingTelemetryModule"/>
<Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebSessionTrackingTelemetryModule"/>
<Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebUserTrackingTelemetryModule"/>
</TelemetryModules>

Please note that, i have registered my TelemetryModule before all the default telemetries, because if you register your telemetry after WebRequestTelemetryModule then anything that you set in OnEndRequest will not be passed upon because WebRequestTelemetryModule makes trackRequest in its onEndRequest method. Any changes made post the trackRequest() call will not be reflected in AI portal.

TelemetryModules are executed in the order they are defined in the ApplicationInsights.xml file.

Integrating App Insights is pretty straight forward and the relevant documentation about the same can be found here.

https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-get-started

Hope it helps.

Track HTTP body content on Application Insights

This actually requires a bit more than inspecting the properties of your Response object. You will have to use a Response Filter in order to capture the body before it's done.

The gist below has two files. One is the CaptureStream.cs file that implements a Stream abastract class and just passes along the information. Along the way we append data in a StringBuilder.

In the other is just an example of a Global.asax.cs that overrides the Application_BeginRequest method and the Application_LogRequest method.

You can choose any method in the ASP.NET Application Lifecycle that you think is the right location. I chose these two because it was the first two I remember using in other projects.

https://gist.github.com/debugthings/058f8c0634accfbdcce2c8c5b818d514

Angular Request & response body into application insights

The C# examples just implement some middleware to handle requests/responses. This is easily achieved in angular using HttpInterceptors. I recommend reading up on how to implement these interceptors to handle the requests and responses as you see fit, as well as registering the HttpInterceptor in you app.module.ts providers array. https://angular.io/guide/http#intercepting-requests-and-responses



Related Topics



Leave a reply



Submit