-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Add AuthN/AuthZ metrics #59557
Merged
Merged
Add AuthN/AuthZ metrics #59557
Changes from all 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 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
204 changes: 204 additions & 0 deletions
204
src/Http/Authentication.Core/src/AuthenticationMetrics.cs
This file contains 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,204 @@ | ||
// 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 AuthenticatedRequestCompleted(string? scheme, AuthenticateResult? result, Exception? exception, long startTimestamp, long currentTimestamp) | ||
{ | ||
if (_authenticatedRequestDuration.Enabled) | ||
{ | ||
AuthenticatedRequestCompletedCore(scheme, result, exception, startTimestamp, currentTimestamp); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void AuthenticatedRequestCompletedCore(string? scheme, AuthenticateResult? result, Exception? exception, long startTimestamp, long currentTimestamp) | ||
{ | ||
var tags = new TagList(); | ||
|
||
if (scheme is not null) | ||
{ | ||
AddSchemeTag(ref tags, scheme); | ||
} | ||
|
||
if (result is not null) | ||
{ | ||
tags.Add("aspnetcore.authentication.result", 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. | ||
}); | ||
} | ||
|
||
if (exception is not null) | ||
{ | ||
AddErrorTag(ref tags, exception); | ||
} | ||
|
||
var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); | ||
_authenticatedRequestDuration.Record(duration.TotalSeconds, tags); | ||
} | ||
|
||
public void ChallengeCompleted(string? scheme, Exception? exception) | ||
{ | ||
if (_challengeCount.Enabled) | ||
{ | ||
ChallengeCompletedCore(scheme, exception); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void ChallengeCompletedCore(string? scheme, Exception? exception) | ||
{ | ||
var tags = new TagList(); | ||
|
||
if (scheme is not null) | ||
{ | ||
AddSchemeTag(ref tags, scheme); | ||
} | ||
|
||
if (exception is not null) | ||
{ | ||
AddErrorTag(ref tags, exception); | ||
} | ||
|
||
_challengeCount.Add(1, tags); | ||
} | ||
|
||
public void ForbidCompleted(string? scheme, Exception? exception) | ||
{ | ||
if (_forbidCount.Enabled) | ||
{ | ||
ForbidCompletedCore(scheme, exception); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void ForbidCompletedCore(string? scheme, Exception? exception) | ||
{ | ||
var tags = new TagList(); | ||
|
||
if (scheme is not null) | ||
{ | ||
AddSchemeTag(ref tags, scheme); | ||
} | ||
|
||
if (exception is not null) | ||
{ | ||
AddErrorTag(ref tags, exception); | ||
} | ||
|
||
_forbidCount.Add(1, tags); | ||
} | ||
|
||
public void SignInCompleted(string? scheme, Exception? exception) | ||
{ | ||
if (_signInCount.Enabled) | ||
{ | ||
SignInCompletedCore(scheme, exception); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void SignInCompletedCore(string? scheme, Exception? exception) | ||
{ | ||
var tags = new TagList(); | ||
|
||
if (scheme is not null) | ||
{ | ||
AddSchemeTag(ref tags, scheme); | ||
} | ||
|
||
if (exception is not null) | ||
{ | ||
AddErrorTag(ref tags, exception); | ||
} | ||
|
||
_signInCount.Add(1, tags); | ||
} | ||
|
||
public void SignOutCompleted(string? scheme, Exception? exception) | ||
{ | ||
if (_signOutCount.Enabled) | ||
{ | ||
SignOutCompletedCore(scheme, exception); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void SignOutCompletedCore(string? scheme, Exception? exception) | ||
{ | ||
var tags = new TagList(); | ||
|
||
if (scheme is not null) | ||
{ | ||
AddSchemeTag(ref tags, scheme); | ||
} | ||
|
||
if (exception is not null) | ||
{ | ||
AddErrorTag(ref tags, exception); | ||
} | ||
|
||
_signOutCount.Add(1, tags); | ||
} | ||
|
||
private static void AddSchemeTag(ref TagList tags, string scheme) | ||
{ | ||
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 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.