-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add AuthN/AuthZ metrics #59557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add AuthN/AuthZ metrics #59557
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e50ed9c
Add authentication metrics
MackinnonBuck d6309e2
Add authorization metrics
MackinnonBuck b08e436
Add missing reference
MackinnonBuck 6c69508
PR feedback for authentication metrics
MackinnonBuck 1cec21f
PR feedback for authorization metrics
MackinnonBuck f5350c3
PR feedback
MackinnonBuck 5679142
API Review feedback
MackinnonBuck 4b0e925
Update tests
MackinnonBuck e3ed7b5
PR feedback
MackinnonBuck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
248 changes: 248 additions & 0 deletions
248
src/Http/Authentication.Core/src/AuthenticationMetrics.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Diagnostics.Metrics; | ||
using System.Runtime.CompilerServices; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Authentication; | ||
|
||
internal sealed class AuthenticationMetrics | ||
{ | ||
public const string MeterName = "Microsoft.AspNetCore.Authentication"; | ||
|
||
private readonly Meter _meter; | ||
private readonly Histogram<double> _authenticatedRequestDuration; | ||
private readonly Counter<long> _challengeCount; | ||
private readonly Counter<long> _forbidCount; | ||
private readonly Counter<long> _signInCount; | ||
private readonly Counter<long> _signOutCount; | ||
|
||
public AuthenticationMetrics(IMeterFactory meterFactory) | ||
{ | ||
_meter = meterFactory.Create(MeterName); | ||
|
||
_authenticatedRequestDuration = _meter.CreateHistogram<double>( | ||
"aspnetcore.authentication.authenticate.duration", | ||
unit: "s", | ||
description: "The authentication duration for a request.", | ||
advice: new() { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries }); | ||
|
||
_challengeCount = _meter.CreateCounter<long>( | ||
"aspnetcore.authentication.challenges", | ||
unit: "{request}", | ||
description: "The total number of times a scheme is challenged."); | ||
|
||
_forbidCount = _meter.CreateCounter<long>( | ||
"aspnetcore.authentication.forbids", | ||
unit: "{request}", | ||
description: "The total number of times an authenticated user attempts to access a resource they are not permitted to access."); | ||
|
||
_signInCount = _meter.CreateCounter<long>( | ||
"aspnetcore.authentication.sign_ins", | ||
unit: "{request}", | ||
description: "The total number of times a principal is signed in."); | ||
|
||
_signOutCount = _meter.CreateCounter<long>( | ||
"aspnetcore.authentication.sign_outs", | ||
unit: "{request}", | ||
description: "The total number of times a scheme is signed out."); | ||
} | ||
|
||
public void AuthenticatedRequestSucceeded(string? scheme, AuthenticateResult result, long startTimestamp, long currentTimestamp) | ||
MackinnonBuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (_authenticatedRequestDuration.Enabled) | ||
{ | ||
AuthenticatedRequestSucceededCore(scheme, result, startTimestamp, currentTimestamp); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void AuthenticatedRequestSucceededCore(string? scheme, AuthenticateResult result, long startTimestamp, long currentTimestamp) | ||
{ | ||
var tags = new TagList(); | ||
TryAddSchemeTag(ref tags, scheme); | ||
|
||
var resultTagValue = result switch | ||
{ | ||
{ None: true } => "none", | ||
{ Succeeded: true } => "success", | ||
{ Failure: not null } => "failure", | ||
_ => "_OTHER", // _OTHER is commonly used fallback for an extra or unexpected value. Shouldn't reach here. | ||
halter73 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
tags.Add("aspnetcore.authentication.result", resultTagValue); | ||
|
||
var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); | ||
_authenticatedRequestDuration.Record(duration.TotalSeconds, tags); | ||
} | ||
|
||
public void AuthenticatedRequestFailed(string? scheme, Exception exception, long startTimestamp, long currentTimestamp) | ||
{ | ||
if (_authenticatedRequestDuration.Enabled) | ||
{ | ||
AuthenticatedRequestFailedCore(scheme, exception, startTimestamp, currentTimestamp); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void AuthenticatedRequestFailedCore(string? scheme, Exception exception, long startTimestamp, long currentTimestamp) | ||
{ | ||
var tags = new TagList(); | ||
TryAddSchemeTag(ref tags, scheme); | ||
AddErrorTag(ref tags, exception); | ||
|
||
var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); | ||
_authenticatedRequestDuration.Record(duration.TotalSeconds, tags); | ||
} | ||
|
||
public void ChallengeSucceeded(string? scheme) | ||
{ | ||
if (_challengeCount.Enabled) | ||
{ | ||
ChallengeSucceededCore(scheme); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void ChallengeSucceededCore(string? scheme) | ||
{ | ||
var tags = new TagList(); | ||
TryAddSchemeTag(ref tags, scheme); | ||
|
||
_challengeCount.Add(1, tags); | ||
} | ||
|
||
public void ChallengeFailed(string? scheme, Exception exception) | ||
{ | ||
if (_challengeCount.Enabled) | ||
{ | ||
ChallengeFailedCore(scheme, exception); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void ChallengeFailedCore(string? scheme, Exception exception) | ||
{ | ||
var tags = new TagList(); | ||
TryAddSchemeTag(ref tags, scheme); | ||
AddErrorTag(ref tags, exception); | ||
|
||
_challengeCount.Add(1, tags); | ||
} | ||
|
||
public void ForbidSucceeded(string? scheme) | ||
{ | ||
if (_forbidCount.Enabled) | ||
{ | ||
ForbidSucceededCore(scheme); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void ForbidSucceededCore(string? scheme) | ||
{ | ||
var tags = new TagList(); | ||
TryAddSchemeTag(ref tags, scheme); | ||
_forbidCount.Add(1, tags); | ||
} | ||
|
||
public void ForbidFailed(string? scheme, Exception exception) | ||
{ | ||
if (_forbidCount.Enabled) | ||
{ | ||
ForbidFailedCore(scheme, exception); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void ForbidFailedCore(string? scheme, Exception exception) | ||
{ | ||
var tags = new TagList(); | ||
TryAddSchemeTag(ref tags, scheme); | ||
AddErrorTag(ref tags, exception); | ||
|
||
_forbidCount.Add(1, tags); | ||
} | ||
|
||
public void SignInSucceeded(string? scheme) | ||
{ | ||
if (_signInCount.Enabled) | ||
{ | ||
SignInSucceededCore(scheme); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void SignInSucceededCore(string? scheme) | ||
{ | ||
var tags = new TagList(); | ||
TryAddSchemeTag(ref tags, scheme); | ||
_signInCount.Add(1, tags); | ||
} | ||
|
||
public void SignInFailed(string? scheme, Exception exception) | ||
{ | ||
if (_signInCount.Enabled) | ||
{ | ||
SignInFailedCore(scheme, exception); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void SignInFailedCore(string? scheme, Exception exception) | ||
{ | ||
var tags = new TagList(); | ||
TryAddSchemeTag(ref tags, scheme); | ||
AddErrorTag(ref tags, exception); | ||
|
||
_signInCount.Add(1, tags); | ||
} | ||
|
||
public void SignOutSucceeded(string? scheme) | ||
{ | ||
if (_signOutCount.Enabled) | ||
{ | ||
SignOutSucceededCore(scheme); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void SignOutSucceededCore(string? scheme) | ||
{ | ||
var tags = new TagList(); | ||
TryAddSchemeTag(ref tags, scheme); | ||
_signOutCount.Add(1, tags); | ||
} | ||
|
||
public void SignOutFailed(string? scheme, Exception exception) | ||
{ | ||
if (_signOutCount.Enabled) | ||
{ | ||
SignOutFailedCore(scheme, exception); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void SignOutFailedCore(string? scheme, Exception exception) | ||
{ | ||
var tags = new TagList(); | ||
TryAddSchemeTag(ref tags, scheme); | ||
AddErrorTag(ref tags, exception); | ||
|
||
_signOutCount.Add(1, tags); | ||
} | ||
|
||
private static void TryAddSchemeTag(ref TagList tags, string? scheme) | ||
{ | ||
if (scheme is not null) | ||
{ | ||
tags.Add("aspnetcore.authentication.scheme", scheme); | ||
} | ||
} | ||
|
||
private static void AddErrorTag(ref TagList tags, Exception exception) | ||
{ | ||
tags.Add("error.type", exception.GetType().FullName); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be great to have a doc describing the meaning and format of each metric and hopefully add them to https://github.com/open-telemetry/semantic-conventions/blob/main/docs/dotnet/dotnet-aspnetcore-metrics.md. Happy to help if necessary.