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?
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.
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.
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.
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.
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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With