Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Application Insight TelemetryInitializer using Azure Function v2

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?

like image 662
Riri Avatar asked Jan 29 '19 08:01

Riri


People also ask

How does Azure function connect to application Insights?

In your function app, select Configuration under Settings, and then select Application settings. If you see a setting named APPINSIGHTS_INSTRUMENTATIONKEY , Application Insights integration is enabled for your function app running in Azure.

Which application Insights data type should you use to capture the custom order telemetry?

Use the Application Insights core telemetry API to send custom events and metrics and your own versions of standard telemetry. This API is the same API that the standard Application Insights data collectors use.

What is telemetry in Azure application Insights?

Application Insights sends telemetry from your web application to the Azure portal so that you can analyze the performance and usage of your application. The telemetry model is standardized, so it's possible to create platform and language-independent monitoring.

How do I add apps to Insights telemetry?

Open your project in Visual Studio. Go to Project > Add Application Insights Telemetry. Choose Azure Application Insights, then select Next. Choose your subscription and Application Insights instance (or create a new instance with Create new), then select Next.


2 Answers

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?

like image 102
Liudmila Molkova Avatar answered Oct 27 '22 14:10

Liudmila Molkova


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"
like image 44
Edward Olamisan Avatar answered Oct 27 '22 15:10

Edward Olamisan