2
votes

I want to be able to add custom properties to a request telemetry. I am able to do this with code such as:

public void LogRequest(IDictionary<string,string> properties) 
{
    var client  = new TelemetryClient();
    var request = new RequestTelemetry();

    foreach(var prop in properties) 
    {
        request.Properties.Add(prop );
    }
    client.TrackRequest(request);
}

This code works in the sense that it creates a request telemetry with all of the custom properties I wanted, however the application insights SDK is also creating a duplicate request telemetry (without my custom properties). So it's sending its own request telemetry and the one that I created.

While trying to do some research I found this: https://github.com/Azure/Azure-Functions/wiki/App-Insights-Early-Preview

Custom telemetry

You can bring the .NET App Insights SDK in and create your own TelemetryClient. There isn’t any conflicts, but there is some advice:

Don’t create TrackRequest or use the StartOperation methods if you don’t want duplicate requests – we do this automatically.

So my question is, is there anyway to send in my own custom request telemetry without the sdk automatically creating a duplicate message?

Also I would like to avoid using TrackEvent. Most of the information I need is already in the request object so I would prefer to use TrackRequest.

This is what I have in my application insights config in the track request section:

<TelemetryModules>
    <Add Type="Microsoft.ApplicationInsights.Web.RequestTrackingTelemetryModule, Microsoft.AI.Web">
        <Handlers>
            <Add>System.Web.Handlers.TransferRequestHandler</Add>
            <Add>Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.RequestDataHttpHandler</Add>
            <Add>System.Web.StaticFileHandler</Add>
            <Add>System.Web.Handlers.AssemblyResourceLoader</Add>
            <Add>System.Web.Optimization.BundleHandler</Add>
            <Add>System.Web.Script.Services.ScriptHandlerFactory</Add>
            <Add>System.Web.Handlers.TraceHandler</Add>
            <Add>System.Web.Services.Discovery.DiscoveryRequestHandler</Add>
            <Add>System.Web.HttpDebugHandler</Add>
        </Handlers>
    </Add>
</TelemetryModules>
1
Maybe try a custom telemetry initializer: docs.microsoft.com/en-us/azure/application-insights/…Peter Bons
Nice, I think that is what I'm looking for. I'll give it a try. Thank you for the link.Soto

1 Answers

1
votes

The reason is that AI SDK automatically track requests for you, and therefore you get dups (the one w/o your properties is the one created automatically).

As PeterBons suggested, using Telemetry Initializer you can add the properties to the auto-generated request.