ITelemetryInitializer not being called in Azure function V4

Viewed 211

I have an V4 Azure function running .net6 as a isolated process. I want to set the Cloud role name in Application insights so I created class that inherits ITelemetryInitializer.

public class MyOwnTelemetryInitializer : ITelemetryInitializer
{ 

    public void Initialize(ITelemetry telemetry)
    {
        telemetry.Context.Cloud.RoleName = "MyAppName";
    }
}

Then I inject it in the main function

public static void Main()
    {
        var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureServices(s =>
            {
                s.AddSingleton<ITelemetryInitializer, MyOwnTelemetryInitializer >();
                
                
            })
            .Build();

        host.Run();
    }

I also set the "APPINSIGHTS_INSTRUMENTATIONKEY" in the locl.settings.json

The logging to application insights is working, but the Initialize() in MyOwnTelemetryInitializer never get call. What am I missing?

1 Answers

I confirm your issue and I have experienced exact same thing yesterday, Initialize-method is never called. Same goes for ITelemetryModule and its Initialize-method.

Initially I wanted just access to RequestTelemetry but in v4 isolated you do not get HttpContext per http request so I tried fetching it via FunctionContext with no luck, like this:

[Function("CreateItemX")]
public async Task<HttpResponseData> CreateItemX(
  [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req, FunctionContext context)
{
   RequestTelemetry requestTelemetry = context.Features.Get<RequestTelemetry>();
   if (requestTelemetry != null)
      requestTelemetry.Properties.Add("RequestBody", "{mybody}");
      
      //the rest
}

Related