3

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?

huysmania
  • 1,054
  • 5
  • 11
ThatCreole
  • 495
  • 1
  • 7
  • 17
  • arw you suing azure function runtime v1 or v2 ? – Thomas Jun 15 '18 at 13:40
  • @Thomas I'm using v1. – ThatCreole Jun 15 '18 at 13:45
  • I dont think there is extension point for this using v1 but I may be wrong. otherwise, on app insight if you do a search and filter only on request, when you click on a particular request, all the traces should be grouped together. – Thomas Jun 15 '18 at 13:52

2 Answers2

2

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

Marek Woźniak
  • 1,766
  • 16
  • 34
ahmelsayed
  • 7,125
  • 3
  • 28
  • 40
0

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.

ThatCreole
  • 495
  • 1
  • 7
  • 17