'How to add customDimensions and set operation_parentId for Azure function log

I created a http trigger V1 azure function on net framework 4.8, and used ILogger for logging. The code is like this. enter image description here

I checked the Application Insight and queried for traces table. This table contains columns named customDimensions and operation_ParentId. May I ask is there anyway to add custom property in customDimensions column, or set a new Guid value for operation_ParentId? I know that I can use TelemetryClient sdk to create a custom telemetry client for logging. Just curious if there is any easy way which doesn't need to create a new telemetry client, because azure function offers bulit-in integration with application insight.

Also, since azure function runtimes automatically tracks requests, is there any way to change the operation_ParentId and customDimensions for requests table as well? Thanks a lot!



Solution 1:[1]

To get both the headers and App Insights to get the custom operation Id, two things must be overridden.

The first is an Activity that wraps the HttpClient, which is responsible for controlling the correlation headers and the other is App Insights' dependency tracing.

Although you can disable Actions completely in your HttpClients, you can just remove the one in the client by setting Activity.Current = null to limit side effects.

var operationId = "CR" + Guid.NewGuid().ToString();
var url = "https://www.microsoft.com";
using (var client = new HttpClient())
{
    using (var requestMessage =
        new HttpRequestMessage(HttpMethod.Get, url))
    {
        //Makes the headers configurable
        Activity.Current = null;

        //set correlation header manually
        requestMessage.Headers.Add("Request-Id", operationId);
        await client.SendAsync(requestMessage);
    }
}

The next step is to remove the App Insights default tracking for this request. Again, you can disable dependency tracking completely, or you can filter out the default telemetry for this request. Processors are registered inside the Startup class just like initializers.

services.AddApplicationInsightsTelemetryProcessor<CustomFilter>();

public class CustomFilter : ITelemetryProcessor
{
    private ITelemetryProcessor Next { get; set; }

    // next will point to the next TelemetryProcessor in the chain.
    public CustomFilter(ITelemetryProcessor next)
    {
        this.Next = next;
    }

    public void Process(ITelemetry item)
    {
        // To filter out an item, return without calling the next processor.
        if (!OKtoSend(item)) { return; }

        this.Next.Process(item);
    }

    // Example: replace with your own criteria.
    private bool OKtoSend(ITelemetry item)
    {
        var dependency = item as DependencyTelemetry;

        if (dependency == null) return true;

        if (dependency.Type == "Http"
            && dependency.Data.Contains("microsoft.com")
            //This key is just there to help identify the custom tracking
            && !dependency.Context.GlobalProperties.ContainsKey("keep"))
        {
            return false;
        }
        return true;
    }
}

Finally, you must inject a telemetry client and call TelemetryClient.TrackDependency() in the method that makes the remote call.

var operationId = "CR" + Guid.NewGuid().ToString();

//setup telemetry client
telemetry.Context.Operation.Id = operationId;
if (!telemetry.Context.GlobalProperties.ContainsKey("keep"))
{
    telemetry.Context.GlobalProperties.Add("keep", "true");
}
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
//continue setting up context if needed

var url = "https:microsoft.com";
using (var client = new HttpClient())
{
    //Makes the headers configurable
    Activity.Current = null;

    using (var requestMessage =
        new HttpRequestMessage(HttpMethod.Get, url))
    {
        //Makes the headers configurable
        Activity.Current = null;

        //set header manually
        requestMessage.Headers.Add("Request-Id", operationId);
        await client.SendAsync(requestMessage);
    }
}

//send custom telemetry 
telemetry.TrackDependency("Http", url, "myCall", startTime, timer.Elapsed, true);

Refer here more information.

Note: The above is possible by disabling the built-in dependency tracking and App Insights and handling it on your own. But the better approach is let .NET Core & App Insights do the tracking.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DelliganeshS-MT