Skip to content

Fix issue where AppInsights SDK doesn't load/configure correctly due to keyed services being registered in DI#594

Merged
martinothamar merged 4 commits into
mainfrom
fix/replace-keyed-registration-with-normal
Apr 16, 2024
Merged

Fix issue where AppInsights SDK doesn't load/configure correctly due to keyed services being registered in DI#594
martinothamar merged 4 commits into
mainfrom
fix/replace-keyed-registration-with-normal

Conversation

@martinothamar

@martinothamar martinothamar commented Apr 16, 2024

Copy link
Copy Markdown
Contributor

Description

A bug report came in, where logs/telemetry wasn't being shipped to AppInsights.
Upon inspecting the IServiceCollection registrations after calling AddApplicationInsightsTelemetry, we discovered that everything until (and including) these lines of registrations succeeded:
https://github.com/microsoft/ApplicationInsights-dotnet/blob/4626e3632323b741a2bd56d3d529c283cf7626c9/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs#L134

However the TelemetryClient itself was never registered in DI. In the code linked above, if there is an exception, it is (relatively silently) swallowed and registered on the event source for AppInsights.

We traced the event source in a local test app:

dotnet trace collect --providers Microsoft-ApplicationInsights-AspNetCore -- ./bin/Debug/net8.0/Altinn.App

Which could be loaded into PerfView

image

We see this error:

HasStack="True" ThreadID="2,558,902" ProcessorNumber="0" errorMessage="System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services. at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.ThrowKeyedDescriptor() 
at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.get_ImplementationFactory() 
at Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.<>c__28`2.<AddSingletonIfNotExists>b__28_0(ServiceDescriptor o) 
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate) 
at Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.AddSingletonIfNotExists[TService,TImplementation](IServiceCollection services) 
at Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.AddCommonTelemetryModules(IServiceCollection services) at Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.AddApplicationInsightsTelemetry(IServiceCollection services)" appDomainName="Altinn.App" 

Which means that AppInsights thinks that our DI container (Scrutor) doesn't support keyed services. I think that's right? Since there is an open issue on that: khellang/Scrutor#209

This PR replaces keyed services by just injecting multiple services, and introducing a second marker interface to distinguish between them instead.

Other options:

  • Use a different DI container (for example the built in one), which supports keyed services right now?
  • ?

Related Issue(s)

  • N/A

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)
  • All tests run green

Documentation

  • User documentation is updated with a separate linked PR in altinn-studio-docs. (if applicable)

@sonarqubecloud

Copy link
Copy Markdown

@martinothamar

Copy link
Copy Markdown
Contributor Author

We can probably also just register AppInsights earlier in the DI container, since the issue is provoked when AppInsights scans the registry.. https://github.com/microsoft/ApplicationInsights-dotnet/blob/4626e3632323b741a2bd56d3d529c283cf7626c9/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs#L297
But that would feel less solid

@martinothamar martinothamar added bugfix Label Pull requests with bugfix. Used when generation releasenotes kind/bug Something isn't working labels Apr 16, 2024
@martinothamar martinothamar changed the title Fix issue where AppInsights SDK doesn't load/configure correctly Fix issue where AppInsights SDK doesn't load/configure correctly due to keyed services being registered in DI Apr 16, 2024
@martinothamar martinothamar merged commit dc62cf8 into main Apr 16, 2024
@martinothamar martinothamar deleted the fix/replace-keyed-registration-with-normal branch April 16, 2024 13:01
@martinothamar martinothamar mentioned this pull request May 6, 2024
5 tasks
ivarne added a commit that referenced this pull request Mar 28, 2025
Notable changes
* Changed from Microsoft.CodeAnalysis.Workspaces to Microsoft.CodeAnalysis.CSharp, in Inernal.Analyzers because of a compiller warning
see: https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md
* Updated SDK reference in global.json
* Updated Applicationinsights package and removed the test from #594
* Updated to released version of HybridCache and remove #pragma waring disable for preview
* Used [CallerFilePath] intead of assemly location to find test data folder.
@ivarne ivarne mentioned this pull request Mar 28, 2025
5 tasks
ivarne added a commit that referenced this pull request Mar 30, 2025
Notable changes
* Changed from Microsoft.CodeAnalysis.Workspaces to Microsoft.CodeAnalysis.CSharp, in Inernal.Analyzers because of a compiller warning
see: https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md
* Updated SDK reference in global.json
* Updated Applicationinsights package and removed the test from #594
* Updated to released version of HybridCache and remove #pragma waring disable for preview
* Used [CallerFilePath] intead of assemly location to find test data folder.
ivarne added a commit that referenced this pull request Mar 30, 2025
* Update nuget depencencies

Notable changes
* Changed from Microsoft.CodeAnalysis.Workspaces to Microsoft.CodeAnalysis.CSharp, in Inernal.Analyzers because of a compiller warning
see: https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md
* Updated SDK reference in global.json
* Updated Applicationinsights package and removed the test from #594
* Updated to released version of HybridCache and remove #pragma waring disable for preview
* Used [CallerFilePath] intead of assemly location to find test data folder.

* Revert to Altinn.Platform.Storage.Interface v 4.0.6
ivarne added a commit that referenced this pull request Mar 30, 2025
* Update nuget depencencies

Notable changes
* Changed from Microsoft.CodeAnalysis.Workspaces to Microsoft.CodeAnalysis.CSharp, in Inernal.Analyzers because of a compiller warning
see: https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md
* Updated SDK reference in global.json
* Updated Applicationinsights package and removed the test from #594
* Updated to released version of HybridCache and remove #pragma waring disable for preview
* Used [CallerFilePath] intead of assemly location to find test data folder.
ivarne added a commit that referenced this pull request Apr 4, 2025
* Update nuget depencencies

Notable changes
* Changed from Microsoft.CodeAnalysis.Workspaces to Microsoft.CodeAnalysis.CSharp, in Inernal.Analyzers because of a compiller warning
see: https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md
* Updated SDK reference in global.json
* Updated Applicationinsights package and removed the test from #594
* Updated to released version of HybridCache and remove #pragma waring disable for preview
* Used [CallerFilePath] intead of assemly location to find test data folder.
olamathiesenBlueTree pushed a commit to olamathiesenBlueTree/fork-altinn-app-lib-dotnet that referenced this pull request Apr 8, 2025
* Update nuget depencencies

Notable changes
* Changed from Microsoft.CodeAnalysis.Workspaces to Microsoft.CodeAnalysis.CSharp, in Inernal.Analyzers because of a compiller warning
see: https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md
* Updated SDK reference in global.json
* Updated Applicationinsights package and removed the test from Altinn#594
* Updated to released version of HybridCache and remove #pragma waring disable for preview
* Used [CallerFilePath] intead of assemly location to find test data folder.

* Revert to Altinn.Platform.Storage.Interface v 4.0.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Label Pull requests with bugfix. Used when generation releasenotes kind/bug Something isn't working

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants