ThatCreole
ThatCreole

Reputation: 525

Custom TelemetryInitializers in AzureFunctions

Short: Is there any way to add a custom ITelemetryInitializer to an AzureFunction application?

Long: After successfully integrating our AzureFunction application with ApplicationInsights and thoroughly instrumenting our code, it became evident really quickly that we'd need to correlate the various Trace and Request telemetry together. To do this we wanted to reuse a custom property we write to all trace logs called a SessionId.

A quick search yielded this SO post and this Docs article. The problem is that these articles assume I have access to some startup event or to the ApplicationInsights.config file on the server. I could be wrong but I don't believe I have access to either one of these.

So my question is, how do I do this with AzureFunctions?

Upvotes: 3

Views: 1696

Answers (2)

ahmelsayed
ahmelsayed

Reputation: 7382

No, it's not possible to customize that. There is work in progress to allow that, but it's not there yet.

You can see more details on these Github issues

application insights integration - ITelemetryInitializer doesn't have any effects #1416

Unable to access TelemetryConfiguration in DefaultTelemetryClientFactory (App Insights) #1556

EDIT:

It's possible in azure function v2. There was a question on stackoverflow with problem here:

Custom Application Insight TelemetryInitializer using Azure Function v2

The problem was solved and from version Microsoft.Net.Sdk.Functions 1.0.25 all works fine, more here:

https://github.com/Azure/azure-functions-host/issues/3731#issuecomment-465252591

Upvotes: 2

ThatCreole
ThatCreole

Reputation: 525

One abhorrent solution I came up with is to reflect into the ILogger that gets passed to the function to get at the ILogger array it hides inside. Then find the ApplicationInsightsLogger ILogger and reflect harder to pull out the TelemetryClient it uses. Once you get this you can merely set the SessionId property on the client's Context.

This works "great" however not only do I now have reflection in my code I had to downgrade the version of ApplicationInsights package I was using to get the type casting to stick.

Looking forward to better support in the future.

Upvotes: 0

Related Questions