Skip to content

Commit 6a830c1

Browse files
committed
Merge refresh counter in authenticate
1 parent 070aa85 commit 6a830c1

File tree

3 files changed

+13
-33
lines changed

3 files changed

+13
-33
lines changed

src/Identity/Core/src/SignInManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ public virtual async Task RefreshSignInAsync(TUser user)
200200
try
201201
{
202202
var (success, isPersistent) = await RefreshSignInCoreAsync(user);
203-
_metrics?.RefreshSignIn(typeof(TUser).FullName!, AuthenticationScheme, success, isPersistent);
203+
var signInResult = success ? SignInResult.Success : SignInResult.Failed;
204+
_metrics?.AuthenticateSignIn(typeof(TUser).FullName!, AuthenticationScheme, signInResult, SignInType.Refresh, isPersistent);
204205
}
205206
catch (Exception ex)
206207
{
207-
_metrics?.RefreshSignIn(typeof(TUser).FullName!, AuthenticationScheme, success: null, isPersistent: null, ex);
208+
_metrics?.AuthenticateSignIn(typeof(TUser).FullName!, AuthenticationScheme, result: null, SignInType.Refresh, isPersistent: null, ex);
208209
throw;
209210
}
210211
}

src/Identity/Core/src/SignInManagerMetrics.cs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ internal sealed class SignInManagerMetrics : IDisposable
1313
public const string AuthenticateCounterName = "aspnetcore.identity.sign_in.authenticate";
1414
public const string RememberTwoFactorCounterName = "aspnetcore.identity.sign_in.remember_two_factor";
1515
public const string ForgetTwoFactorCounterName = "aspnetcore.identity.sign_in.forget_two_factor";
16-
public const string RefreshCounterName = "aspnetcore.identity.sign_in.refresh";
1716
public const string CheckPasswordCounterName = "aspnetcore.identity.sign_in.check_password";
1817
public const string SignInUserPrincipalCounterName = "aspnetcore.identity.sign_in.sign_in_principal";
1918
public const string SignOutUserPrincipalCounterName = "aspnetcore.identity.sign_in.sign_out_principal";
@@ -22,7 +21,6 @@ internal sealed class SignInManagerMetrics : IDisposable
2221
private readonly Counter<long> _authenticateCounter;
2322
private readonly Counter<long> _rememberTwoFactorClientCounter;
2423
private readonly Counter<long> _forgetTwoFactorCounter;
25-
private readonly Counter<long> _refreshCounter;
2624
private readonly Counter<long> _checkPasswordCounter;
2725
private readonly Counter<long> _signInUserPrincipalCounter;
2826
private readonly Counter<long> _signOutUserPrincipalCounter;
@@ -34,7 +32,6 @@ public SignInManagerMetrics(IMeterFactory meterFactory)
3432
_authenticateCounter = _meter.CreateCounter<long>(AuthenticateCounterName, "count", "The number of authenticate attempts. The authenticate counter is incremented by sign in methods such as PasswordSignInAsync and TwoFactorSignInAsync.");
3533
_rememberTwoFactorClientCounter = _meter.CreateCounter<long>(RememberTwoFactorCounterName, "count", "The number of two factor clients remembered.");
3634
_forgetTwoFactorCounter = _meter.CreateCounter<long>(ForgetTwoFactorCounterName, "count", "The number of two factor clients forgotten.");
37-
_refreshCounter = _meter.CreateCounter<long>(RefreshCounterName, "count", "The number of refresh sign-in attempts.");
3835
_checkPasswordCounter = _meter.CreateCounter<long>(CheckPasswordCounterName, "count", "The number of check password attempts. Checks that the account is in a state that can log in and that the password is valid using the UserManager.CheckPasswordAsync method.");
3936
_signInUserPrincipalCounter = _meter.CreateCounter<long>(SignInUserPrincipalCounterName, "count", "The number of user principals signed in.");
4037
_signOutUserPrincipalCounter = _meter.CreateCounter<long>(SignOutUserPrincipalCounterName, "count", "The number of user principals signed out.");
@@ -57,7 +54,7 @@ internal void CheckPasswordSignIn(string userType, SignInResult? result, Excepti
5754
_checkPasswordCounter.Add(1, tags);
5855
}
5956

60-
internal void AuthenticateSignIn(string userType, string authenticationScheme, SignInResult? result, SignInType signInType, bool isPersistent, Exception? exception = null)
57+
internal void AuthenticateSignIn(string userType, string authenticationScheme, SignInResult? result, SignInType signInType, bool? isPersistent, Exception? exception = null)
6158
{
6259
if (!_authenticateCounter.Enabled)
6360
{
@@ -69,8 +66,11 @@ internal void AuthenticateSignIn(string userType, string authenticationScheme, S
6966
{ "aspnetcore.identity.user_type", userType },
7067
{ "aspnetcore.identity.authentication_scheme", authenticationScheme },
7168
{ "aspnetcore.identity.sign_in.type", GetSignInType(signInType) },
72-
{ "aspnetcore.identity.sign_in.is_persistent", isPersistent },
7369
};
70+
if (isPersistent != null)
71+
{
72+
tags.Add("aspnetcore.identity.sign_in.is_persistent", isPersistent.Value);
73+
}
7474
AddSignInResult(ref tags, result);
7575
AddExceptionTags(ref tags, exception);
7676

@@ -145,28 +145,6 @@ internal void ForgetTwoFactorClient(string userType, string authenticationScheme
145145
_forgetTwoFactorCounter.Add(1, tags);
146146
}
147147

148-
internal void RefreshSignIn(string userType, string authenticationScheme, bool? success, bool? isPersistent, Exception? exception = null)
149-
{
150-
if (!_refreshCounter.Enabled)
151-
{
152-
return;
153-
}
154-
155-
var tags = new TagList
156-
{
157-
{ "aspnetcore.identity.user_type", userType },
158-
{ "aspnetcore.identity.authentication_scheme", authenticationScheme },
159-
{ "aspnetcore.identity.sign_in.result", success.GetValueOrDefault() ? "success" : "failure" }
160-
};
161-
if (isPersistent != null)
162-
{
163-
tags.Add("aspnetcore.identity.sign_in.is_persistent", isPersistent.Value);
164-
}
165-
AddExceptionTags(ref tags, exception);
166-
167-
_refreshCounter.Add(1, tags);
168-
}
169-
170148
public void Dispose()
171149
{
172150
_meter.Dispose();
@@ -217,6 +195,7 @@ private static string GetSignInResult(SignInResult result)
217195

218196
internal enum SignInType
219197
{
198+
Refresh,
220199
Password,
221200
TwoFactorRecoveryCode,
222201
TwoFactorAuthenticator,

src/Identity/test/Identity.Test/SignInManagerTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ public async Task CanResignIn(bool isPersistent, bool externalLogin)
763763
{
764764
// Setup
765765
var testMeterFactory = new TestMeterFactory();
766-
using var refreshSignIn = new MetricCollector<long>(testMeterFactory, "Microsoft.AspNetCore.Identity", SignInManagerMetrics.RefreshCounterName);
766+
using var authenticate = new MetricCollector<long>(testMeterFactory, "Microsoft.AspNetCore.Identity", SignInManagerMetrics.AuthenticateCounterName);
767767

768768
var user = new PocoUser { UserName = "Foo" };
769769
var context = new DefaultHttpContext();
@@ -803,7 +803,7 @@ public async Task CanResignIn(bool isPersistent, bool externalLogin)
803803
auth.Verify();
804804
signInManager.Verify();
805805

806-
Assert.Collection(refreshSignIn.GetMeasurementSnapshot(),
806+
Assert.Collection(authenticate.GetMeasurementSnapshot(),
807807
m => MetricsHelpers.AssertContainsTags(m.Tags,
808808
[
809809
KeyValuePair.Create<string, object>("aspnetcore.identity.user_type", "Microsoft.AspNetCore.Identity.Test.PocoUser"),
@@ -817,7 +817,7 @@ public async Task CanResignIn(bool isPersistent, bool externalLogin)
817817
public async Task ResignInNoOpsAndLogsErrorIfNotAuthenticated()
818818
{
819819
var testMeterFactory = new TestMeterFactory();
820-
using var refresh = new MetricCollector<long>(testMeterFactory, "Microsoft.AspNetCore.Identity", SignInManagerMetrics.RefreshCounterName);
820+
using var authenticate = new MetricCollector<long>(testMeterFactory, "Microsoft.AspNetCore.Identity", SignInManagerMetrics.AuthenticateCounterName);
821821
using var signInUserPrincipal = new MetricCollector<long>(testMeterFactory, "Microsoft.AspNetCore.Identity", SignInManagerMetrics.SignInUserPrincipalCounterName);
822822

823823
var user = new PocoUser { UserName = "Foo" };
@@ -840,7 +840,7 @@ public async Task ResignInNoOpsAndLogsErrorIfNotAuthenticated()
840840
signInManager.Verify(s => s.SignInWithClaimsAsync(It.IsAny<PocoUser>(), It.IsAny<AuthenticationProperties>(), It.IsAny<IEnumerable<Claim>>()),
841841
Times.Never());
842842

843-
Assert.Collection(refresh.GetMeasurementSnapshot(),
843+
Assert.Collection(authenticate.GetMeasurementSnapshot(),
844844
m => MetricsHelpers.AssertContainsTags(m.Tags,
845845
[
846846
KeyValuePair.Create<string, object>("aspnetcore.identity.user_type", "Microsoft.AspNetCore.Identity.Test.PocoUser"),

0 commit comments

Comments
 (0)