Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
427 stars 182 forks source link

Custom implementation of ITelemetryInitializer doesn't receive ITelemetry of type RequestTelemetry in Isolated Azure Func(v4) #1603

Open brewstertimothybrewster opened 1 year ago

brewstertimothybrewster commented 1 year ago

I have a Azure isolated function v4 and want to enrich the telemetry before it's sent to Application Insight. Therefore i have implemented the "ITelemetryInitializer" with a custom class. However when I execute a http request to the function the "Initialize" method is called with a "ITelemetry" object. But the problem is that none of these "ITelemetry" items are of type "RequestTelemetry". Only "ITelemetry" items of type "Microsoft.ApplicationInsights.DataContracts.MetricTelemetry" and "Microsoft.ApplicationInsights.DataContracts.DependencyTelemetry" are found.

image

My project is using these packages image

I would like to know if enrichment of telemetry is possible with Azure isolated function version 4. I have this working with another Asp.Net project btw.

rtgs-danila-golovniov commented 1 year ago

+1 we also want to enrich request telemetry with some correlation data, but are unable to do that from the worker process. We ended up with a custom middleware that logs a duplicate RequestTelemetry, which obviously at scale will cost a lot. So fixing this would be such a winner.

angel199321 commented 1 year ago

+1 we also want to enrich request telemetry with some correlation data, but are unable to do that from the worker process. We ended up with a custom middleware that logs a duplicate RequestTelemetry, which obviously at scale will cost a lot. So fixing this would be such a winner.

Hi, Can you share the middleware codes, I trying to add custom correlation data to request telemetry too. Thank you.

bmacalindong commented 1 year ago

Just wanted to know what's the plan for this or are there are any alternatives we can use apart from tracking it via middleware?

briandunnington commented 1 year ago

would also like to hear an update about this. even if we cant get the real RequestTelemetry in the worker, it would be nice if the host "knew" about HTTP triggers and logged them as success/failures like people would expect. previously we had a custom ITelemetryInitializer just to do this:

image

at least with that, 4xx and 5xx responses show up as failed requests in AppInsights. otherwise, everything just gets logged as a success

cc @fabiocav @brettsam

saalocin commented 1 year ago

+1 experiencing exactly the same issue as described by OP. The types received by ITelemetryInitializer is limited by these 2 types: "Microsoft.ApplicationInsights.DataContracts.MetricTelemetry" and "Microsoft.ApplicationInsights.DataContracts.DependencyTelemetry"

briandunnington commented 1 year ago

Since the RequestTelemetry is handled entirely by the host process, and all of your function app code runs in the worker process, there is no straight-forward way to get access to the RequestTelemetry. You can in fact do it though, but it is not trivial. Full details of how to do it and a working example here: https://github.com/briandunnington/AzureFunctionsIsolatedHttpTelemetry (includes link to a published Nuget package if you only want to set the Success based on HTTP response codes)

luthus commented 1 month ago

@fabiocav @mattchenderson RequestTelemetry definitely seems like something that should be setup in the worker rather than the host. With the In Process model we add a whole bunch of additional custom dimensions to the RequestTelemetry objects. This will likely block our migration over to the Isolated model without a solution.

The only other solution I can think of is to completely remove the host's request telemetry data via the host.json and use middleware to setup the RequestTelemetry manually. We did something similar for Azure Functions V1 originally and I really can't believe we're back to considering this years down the line.