Skip to content

AddMetrics can initialize MetricsSubscriptionManager multiple times #90779

@JamesNK

Description

@JamesNK

Description

Calling AddMetrics initializes the subscription manager:

// 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:

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions