microsoft / ApplicationInsights-dotnet

ApplicationInsights-dotnet
MIT License
565 stars 287 forks source link

RequestTelemetry missing in HttpContext.Features #2842

Open mcgiany opened 9 months ago

mcgiany commented 9 months ago

.NET 7 Nuget version 2.21.0 Windows

Hi, we are facing weird issue in our .NET7 ASP NET Project, that Features in HTTP Context doesnt contain RequestTelemetry. HttpContext.Features.Get<RequestTelemetry>() returns NULL.

The problem is, we cannot reproduce it on developers machine, it happens only in our test machine. (Same build same code). Is there anything, that could cause this behaviour? Like any IIS setting or evironment variable, etc.

I cannot add example project, because on local machine it works.

CharlesSchimmel commented 5 months ago

I'm seeing this as well. Works when ran locally but not when deployed an Azure app service. .NET 8, ASP.NET API wherein I have a middleware that attempts to add values to the RequestTelemetry.

CharlesSchimmel commented 5 months ago

My Azure App Service is using Application Insights autoinstrumentation extension (v2.8.45.1) with interop enabled (XDT_MicrosoftApplicationInsights_PreemptSdk=1), and I wonder if that is related. Perhaps when running locally, there's no autoinstrumented instance of AI running to pre-empt the SDK, so custom telemetry still works.

It's unclear to me what SDK interop actually does; the documentation says "Loads the extension side by side with the SDK and uses it to send telemetry. (Disables the Application Insights SDK.)" which to me is ambiguous if things like writing RequestTelemetry in middleware would still work.

Something else I've found is that if I enumerate and log out httpContext.Features.AsEnumerable() in the middleware, RequestTelemetry appears as a registered feature, but any attempt to retrieve it returns null.

Update: Setting XDT_MicrosoftApplicationInsights_PreemptSdk=0 seems to have resolved it.