microsoft / ApplicationInsights-dotnet

ApplicationInsights-dotnet
MIT License
565 stars 287 forks source link

ArgumentNullException in OperationNameTelemetryInitializer #1550

Closed SergeyKanzhelev closed 2 years ago

SergeyKanzhelev commented 7 years ago

Customer reported:

Problem Desc:  on 2017-02-21T17:16:16.109Z we encountered a runtime exception in the Application Insights integration module.

System.ArgumentNullException: Value cannot be null.
Parameter name: provider
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.OperationNameTelemetryInitializer.OnBeforeAction(ActionDescriptor actionDescriptor, HttpContext httpContext, RouteData routeData)
   at Proxy_Method_From_<>f__AnonymousType0`3_To_Void OnBeforeAction(Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor, Microsoft.AspNetCore.Http.HttpContext, Microsoft.AspNetCore.Routing.RouteData)(Object , Object , IProxyFactory )
   at Microsoft.Extensions.DiagnosticAdapter.DiagnosticSourceAdapter.Write(String diagnosticName, Object parameters)
   at Microsoft.Extensions.DiagnosticAdapter.DiagnosticSourceAdapter.System.IObserver<System.Collections.Generic.KeyValuePair<System.String,System.Object>>.OnNext(KeyValuePair`2 value)
   at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
   at Microsoft.AspNetCore.Mvc.Internal.MvcCoreDiagnosticSourceExtensions.BeforeAction(DiagnosticSource diagnosticSource, ActionDescriptor actionDescriptor, HttpContext httpContext, RouteData routeData)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.ApplicationInsights.AspNetCore.ExceptionTrackingMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()

Application is an aspnet core application built in .Net461 full framework. Application insights has been applied through Microsoft.ApplicationInsights.AspNetCore 1.0.2 – Now upgraded to 2.0.0 We have enabled it from the app service “Application Insights” setting as well. The application is published though “Continous Delivery”. It happened only once. The application communicates to Redis “as a service”, sql “as a service”, Azure tables. We have configured application logging to send data to azure storage.

SergeyKanzhelev commented 7 years ago

Looking at code I think the most probable root cause is that IHttpContextAccessor is null. I binged and saw couple links with the same problem.

@pakrym what may be the cause? If this is expected behavior - we'd need to remove the check and fail more gracefully.

SergeyKanzhelev commented 7 years ago

Hm, it seems I jumped into conclusion too fast. It probably has http accessor since it fails in Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider), not in telemetry initializer itself.

SergeyKanzhelev commented 7 years ago

Failed here

So httpContext.RequestServices was null in this code:

var telemetry = httpContext.RequestServices.GetService<RequestTelemetry>(); 

In version 2.X the call was replaced to:

var telemetry = httpContext.Features.Get<RequestTelemetry>(); 

Now the question is what might be a condition when httpContext.RequestServices became null and whether the same condition may apply to httpContext.Features

pakrym commented 7 years ago

The only reason I know httpContext.RequestServices to be set to null is when request is completed and HttpContext disposed. I think it might happen to httpContext.Features too, but request code should be done executing at this time.

SergeyKanzhelev commented 7 years ago

@pakrym can dispose happen for some reason while in Microsoft.AspNetCore.Mvc.Internal.MvcCoreDiagnosticSourceExtensions.BeforeAction? Looking at stack I do not have any explanation how it can happen, but clearly customer experienced this. Any ideas?

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 300 days with no activity. Remove stale label or comment or this will be closed in 7 days.