Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 6, 2025

Plan: Enable Identity telemetry by instantiating metrics classes directly

  • Understand the issue: SignInManagerMetrics and UserManagerMetrics are not registered in DI, causing metrics to not surface
  • Locate relevant files and understand current structure
  • Initial approach: Register metrics classes in DI
  • Address feedback: Remove InternalsVisibleTo between product assemblies
  • Address feedback: Instantiate metrics directly instead of DI registration
  • Address feedback: Call AddMetrics() when registering managers
  • Address feedback: Add test for metrics
  • Build the changes successfully
  • Run tests - all tests pass including new metrics test

Summary

Successfully enabled Identity metrics to surface in applications by instantiating metrics classes directly in their respective manager constructors and ensuring the metrics infrastructure is properly initialized.

Final Approach

The metrics classes are instantiated directly in the constructors of UserManager and SignInManager:

  1. SignInManager: Gets IMeterFactory from DI and creates SignInManagerMetrics instance
  2. UserManager: Gets IMeterFactory from DI and creates UserManagerMetrics instance
  3. AddMetrics(): Called in AddIdentityCore and AddIdentity to ensure metrics infrastructure is initialized
  4. Test Coverage: Added functional test to verify metrics are recorded

This approach:

  • Avoids adding metrics classes to DI container
  • Ensures IMeterFactory and metrics infrastructure are properly registered
  • Only requires IMeterFactory to be available in DI
  • Respects assembly boundaries (no InternalsVisibleTo needed)
  • Keeps metrics classes internal
  • Follows the pattern used by other ASP.NET Core components (e.g., Authentication)
  • Includes comprehensive test coverage

Changes Made

  1. SignInManager.cs: Modified constructor to instantiate SignInManagerMetrics directly using IMeterFactory from DI
  2. UserManager.cs: Modified constructor to instantiate UserManagerMetrics directly using IMeterFactory from DI
  3. IdentityServiceCollectionExtensions.cs (Core): Added services.AddMetrics() call in AddIdentity
  4. IdentityServiceCollectionExtensions.cs (Extensions.Core): Added services.AddMetrics() call in AddIdentityCore
  5. Microsoft.AspNetCore.Identity.csproj: Added reference to Microsoft.Extensions.Diagnostics
  6. Microsoft.Extensions.Identity.Core.csproj: Added reference to Microsoft.Extensions.Diagnostics
  7. MockHelpers.cs: Updated test helpers to only register IMeterFactory, not the metrics classes
  8. MapIdentityApiTests.cs: Added MetricsAreRecordedForUserManagerAndSignInManager test to verify metrics are recorded
  9. Microsoft.AspNetCore.Identity.FunctionalTests.csproj: Added shared source for TestMeterFactory and reference to Microsoft.Extensions.Diagnostics.Testing

Testing

  • ✅ Build succeeded
  • ✅ All 94 SignInManager tests pass
  • ✅ New metrics test passes - verifies UserManager and SignInManager metrics are recorded

Fixes #64264

Original prompt

This section details on the original issue you should resolve

<issue_title>SignInManagerMetrics not registered in DI container</issue_title>
<issue_description>The SignInManagerMetrics class that the SignInManager expects to be in the DI container is not currently registered anywhere outside of test code so the identity metrics aren't surfacing from th application.

cc: @JamesNK @MackinnonBuck </issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@dotnet-policy-service
Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

Copilot AI and others added 2 commits November 6, 2025 22:04
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
…able UserManagerMetrics registration

Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
Copilot AI changed the title [WIP] Add SignInManagerMetrics registration in DI container Register SignInManagerMetrics and UserManagerMetrics in DI container Nov 6, 2025
Copilot AI requested a review from captainsafia November 6, 2025 22:27
Copilot finished work on behalf of captainsafia November 6, 2025 22:27
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.AspNetCore.Identity" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Is this required? We want to avoid adding InternalsVisibleTo between product assemblies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 749b76d. Now only SignInManagerMetrics (which is in Core) is registered in Core's AddIdentity. UserManagerMetrics (which is in Extensions.Core) is only registered in Extensions.Core's AddIdentityCore.

Co-authored-by: JamesNK <303201+JamesNK@users.noreply.github.com>
Copilot AI requested a review from JamesNK November 6, 2025 23:41
Copilot finished work on behalf of JamesNK November 6, 2025 23:41
@JamesNK
Copy link
Member

JamesNK commented Nov 6, 2025

@copilot Change UserManagerMetrics and SignInManagerMetrics to not be registered in DI. Instead, create types using their constructor and just DI to resolve their IMeterFactory dependency

Co-authored-by: JamesNK <303201+JamesNK@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 6, 2025

@copilot Change UserManagerMetrics and SignInManagerMetrics to not be registered in DI. Instead, create types using their constructor and just DI to resolve their IMeterFactory dependency

Updated in bb2a007. Metrics classes are now instantiated directly in the manager constructors, resolving only IMeterFactory from DI. No DI registrations for the metrics classes are needed.

Copilot AI changed the title Register SignInManagerMetrics and UserManagerMetrics in DI container Enable Identity telemetry by instantiating metrics classes directly Nov 6, 2025
Copilot finished work on behalf of JamesNK November 6, 2025 23:57
@JamesNK
Copy link
Member

JamesNK commented Nov 7, 2025

@copilot Called AddMetrics() when UserManager and SignInManager are registered in DI.

Co-authored-by: JamesNK <303201+JamesNK@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 7, 2025

@copilot Called AddMetrics() when UserManager and SignInManager are registered in DI.

Added in 7b2ae04. AddMetrics() is now called in both AddIdentityCore and AddIdentity before registering UserManager and SignInManager. Also added Microsoft.Extensions.Diagnostics package reference to both Identity projects.

Copilot finished work on behalf of JamesNK November 7, 2025 00:43
@JamesNK
Copy link
Member

JamesNK commented Nov 7, 2025

@copilot Add a test to MapIdentityApiTests in Microsoft.AspNetCore.Identity.FunctionalTests that asserts that metrics are recorded for UserManager and SignInManager.

…ager

Co-authored-by: JamesNK <303201+JamesNK@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 7, 2025

@copilot Add a test to MapIdentityApiTests in Microsoft.AspNetCore.Identity.FunctionalTests that asserts that metrics are recorded for UserManager and SignInManager.

Added in 756e4f0. The test MetricsAreRecordedForUserManagerAndSignInManager verifies that:

  • UserManager metrics are recorded when creating a user (using aspnetcore.identity.user.create.duration)
  • SignInManager metrics are recorded when logging in (using aspnetcore.identity.sign_in.authenticate.duration)

The test uses TestMeterFactory and MetricCollector to assert that the metrics are properly emitted with correct tags.

Copilot finished work on behalf of JamesNK November 7, 2025 01:08
@JamesNK JamesNK marked this pull request as ready for review November 7, 2025 01:14
Copilot AI review requested due to automatic review settings November 7, 2025 01:14
@JamesNK JamesNK added the area-identity Includes: Identity and providers label Nov 7, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors how metrics are initialized in the Identity system by transitioning from singleton-registered metrics classes to on-demand instantiation via IMeterFactory. The change simplifies the dependency injection setup while maintaining the same metrics functionality.

  • Removed explicit singleton registrations of UserManagerMetrics and SignInManagerMetrics from service collection configuration
  • Modified UserManager and SignInManager to instantiate metrics classes directly from IMeterFactory when available
  • Added AddMetrics() calls to Identity service registration methods to ensure IMeterFactory is available
  • Added functional test to verify metrics are properly recorded in real scenarios

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Identity/test/Shared/MockHelpers.cs Removed manual singleton registrations of metrics classes in test helper methods
src/Identity/Extensions.Core/src/UserManager.cs Changed to instantiate UserManagerMetrics directly from IMeterFactory instead of retrieving from DI
src/Identity/Core/src/SignInManager.cs Changed to instantiate SignInManagerMetrics directly from IMeterFactory instead of retrieving from DI
src/Identity/Extensions.Core/src/IdentityServiceCollectionExtensions.cs Added AddMetrics() call to ensure IMeterFactory is registered
src/Identity/Core/src/IdentityServiceCollectionExtensions.cs Added AddMetrics() call to ensure IMeterFactory is registered
src/Identity/Extensions.Core/src/Microsoft.Extensions.Identity.Core.csproj Added reference to Microsoft.Extensions.Diagnostics package
src/Identity/Core/src/Microsoft.AspNetCore.Identity.csproj Added reference to Microsoft.Extensions.Diagnostics package
src/Identity/test/Identity.FunctionalTests/Microsoft.AspNetCore.Identity.FunctionalTests.csproj Added shared TestMeterFactory and Microsoft.Extensions.Diagnostics.Testing reference for testing
src/Identity/test/Identity.FunctionalTests/MapIdentityApiTests.cs Added functional test to verify metrics are recorded for both UserManager and SignInManager operations

JamesNK and others added 2 commits November 7, 2025 10:08
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-identity Includes: Identity and providers Attention: Shared Code Modified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SignInManagerMetrics not registered in DI container

3 participants