7

In a Azure Function we make a number of request using the .NET HttpClient class, the Azure Servicebus SDK and the Azure Storage SDK - everything is beautifully logged via the build in Application Insight logging, showing dependencies and all!

Now we however like to add an implementation of ITelemetryInitializer to add a few dimensions to the EventTelemetry being written by the SDKs mentioned above.

We start by creating a class implementing the ITelemetryInitializer interface.

public class ReferrerTelemetryInitializer : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry is EventTelemetry)
        {
            // Add some code
        }
    }
}

I then create a class implementing the IWebJobsStartup interface to DI inject my initializer.

[assembly: WebJobsStartup(typeof(Startup))]

namespace LoggingTest
{
    public class Startup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddSingleton<ITelemetryInitializer, ReferrerTelemetryInitializer>();
        }
    }
}

Note: I only got the Startup class to fire in .NET Core 2.0 (and not in 2.1) - this seems to be an open an known issue though?

BUT, the Initialize method in the initializer class does however never fires (in core 2.0, after I've seen that the Startup class has registered the DI ReferrerTelemetryInitializer implementation)?

What are we missing?

Riri
  • 11,501
  • 14
  • 63
  • 88

2 Answers2

1

You are not missing anything and it should just work. There is a known issue that breaks WebJobStartup in functions.

EventTelemtery is never reported by the SDK automatically (they report Requests, Dependencies, Exceptions, Traces and Metrics). Can you check if initialize is called with other types of telemetry on net core 2.0?

  • 1
    Thanks @Liudmila! Unfortunately initialize is never called no matter what type we're talking about ... I guess I just wait for an update then? Any idea of when this might be fixed? Or do I just have to watch the status of the [issue](https://github.com/Azure/azure-functions-host/issues/3731)? – Riri Jan 30 '19 at 08:32
  • If Startup.Configure was hit, Initializer should work. Would you mind creating a repro app and sending it to me (lmolkova at microsoft dot com) or creating an issue with repro steps in https://github.com/azure/azure-functions-host and mentioning me (lmolkova)? – Liudmila Molkova Jan 31 '19 at 19:00
  • Created a issue with test repo https://github.com/Azure/azure-functions-host/issues/4064 @liudmila-molkova – Riri Feb 08 '19 at 09:01
1

Azure Function v3

For those that work on this in 2021 in Azure Function v3 this does work now.

The TelemetryClient is configured by the Azure Function host/runtime. It will pick up the registered ITelemetryInitializer via dependency injection from the service collection.

Project file

<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.27" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />

Startup

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddSingleton<ITelemetryInitializer, TheCustomTelemetryInitializer>();            
    }
}

And important for local development!:

Then you need to add some value to your APPINSIGHTS_INSTRUMENTATIONKEY in local.settings.json. This is where it differs from regular console apps, where even if the instrumentation key is empty it will work locally. In function apps apparently you need to add some value there. It can be anything, I've done this and it worked fine:

Source

"APPINSIGHTS_INSTRUMENTATIONKEY": "you-need-to-have-anything-at-all-here-so-it-will-work-locally"
Edward Olamisan
  • 800
  • 1
  • 18
  • 28