-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Description
Calling AddMetrics initializes the subscription manager:
runtime/src/libraries/Microsoft.Extensions.Diagnostics/src/Metrics/MetricsServiceExtensions.cs
Lines 33 to 35 in 64243bb
| // Make sure the subscription manager is started when the host starts. | |
| // The host will trigger options validation. | |
| services.AddOptions<NoOpOptions>().Configure<MetricsSubscriptionManager>((_, manager) => manager.Initialize()).ValidateOnStart(); |
However, the way this is done isn't resilient to multiple calls to AddMetrics by app or library code.
Listeners are initialized multiple times:
runtime/src/libraries/Microsoft.Extensions.Diagnostics/src/Metrics/MetricsSubscriptionManager.cs
Lines 30 to 36 in 64243bb
| public void Initialize() | |
| { | |
| foreach (var listener in _listeners) | |
| { | |
| listener.Initialize(); | |
| } | |
| } |
Reproduction Steps
services.AddMetrics();
services.AddMetrics();Expected behavior
Multiple IConfigurateOptions<MetricsServiceExtensions+NoOpOptions> aren't registered in DI. MetricsSubscriptionManager is initialized once.
Actual behavior
Multiple IConfigurateOptions<MetricsServiceExtensions+NoOpOptions> is registered in DI. Each will instance will call initialize on MetricsSubscriptionManager.
I noticed this when the AddMvc_Twice_DoesNotAddDuplicates test in aspnetcore started throwing because there were duplicate config options.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response