-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add metrics to rate limiting #47758
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 metrics to rate limiting #47758
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6f6bb59
Add metrics to rate limiting
JamesNK 14905fa
Update
JamesNK e834f81
Fix build
JamesNK d6d935f
API review feedback
JamesNK f514919
PR feedback
JamesNK d4c1ba3
Update src/Middleware/RateLimiting/src/RateLimitingMetrics.cs
JamesNK 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
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
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 |
---|---|---|
|
@@ -22,4 +22,4 @@ internal enum RequestRejectionReason | |
EndpointLimiter, | ||
GlobalLimiter, | ||
RequestCanceled | ||
} | ||
} |
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
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
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,161 @@ | ||
// 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.Extensions.Metrics; | ||
|
||
namespace Microsoft.AspNetCore.RateLimiting; | ||
|
||
internal sealed class RateLimitingMetrics : IDisposable | ||
{ | ||
public const string MeterName = "Microsoft.AspNetCore.RateLimiting"; | ||
|
||
private readonly Meter _meter; | ||
private readonly UpDownCounter<long> _currentLeaseRequestsCounter; | ||
private readonly Histogram<double> _leaseRequestDurationCounter; | ||
private readonly UpDownCounter<long> _currentRequestsQueuedCounter; | ||
private readonly Histogram<double> _queuedRequestDurationCounter; | ||
private readonly Counter<long> _leaseFailedRequestsCounter; | ||
|
||
public RateLimitingMetrics(IMeterFactory meterFactory) | ||
{ | ||
_meter = meterFactory.CreateMeter(MeterName); | ||
|
||
_currentLeaseRequestsCounter = _meter.CreateUpDownCounter<long>( | ||
"current-leased-requests", | ||
description: "Number of HTTP requests that are currently active on the server that hold a rate limiting lease."); | ||
JamesNK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
_leaseRequestDurationCounter = _meter.CreateHistogram<double>( | ||
"leased-request-duration", | ||
unit: "s", | ||
description: "The duration of rate limiting leases held by HTTP requests on the server."); | ||
|
||
_currentRequestsQueuedCounter = _meter.CreateUpDownCounter<long>( | ||
"current-queued-requests", | ||
description: "Number of HTTP requests that are currently queued, waiting to acquire a rate limiting lease."); | ||
|
||
_queuedRequestDurationCounter = _meter.CreateHistogram<double>( | ||
"queued-request-duration", | ||
unit: "s", | ||
description: "The duration of HTTP requests in a queue, waiting to acquire a rate limiting lease."); | ||
|
||
_leaseFailedRequestsCounter = _meter.CreateCounter<long>( | ||
"lease-failed-requests", | ||
description: "Number of HTTP requests that failed to acquire a rate limiting lease. Requests could be rejected by global or endpoint rate limiting policies. Or the request could be canceled while waiting for the lease."); | ||
} | ||
|
||
public void LeaseFailed(string? policyName, string? method, string? route, RequestRejectionReason reason) | ||
{ | ||
if (_leaseFailedRequestsCounter.Enabled) | ||
{ | ||
LeaseFailedCore(policyName, method, route, reason); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void LeaseFailedCore(string? policyName, string? method, string? route, RequestRejectionReason reason) | ||
{ | ||
var tags = new TagList(); | ||
InitializeRateLimitingTags(ref tags, policyName, method, route); | ||
tags.Add("reason", reason.ToString()); | ||
_leaseFailedRequestsCounter.Add(1, tags); | ||
} | ||
|
||
public void LeaseStart(string? policyName, string? method, string? route) | ||
{ | ||
if (_currentLeaseRequestsCounter.Enabled) | ||
halter73 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
LeaseStartCore(policyName, method, route); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void LeaseStartCore(string? policyName, string? method, string? route) | ||
{ | ||
var tags = new TagList(); | ||
InitializeRateLimitingTags(ref tags, policyName, method, route); | ||
_currentLeaseRequestsCounter.Add(1, tags); | ||
} | ||
|
||
public void LeaseEnd(string? policyName, string? method, string? route, long startTimestamp, long currentTimestamp) | ||
{ | ||
if (_currentLeaseRequestsCounter.Enabled || _leaseRequestDurationCounter.Enabled) | ||
{ | ||
LeaseEndCore(policyName, method, route, startTimestamp, currentTimestamp); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void LeaseEndCore(string? policyName, string? method, string? route, long startTimestamp, long currentTimestamp) | ||
{ | ||
var tags = new TagList(); | ||
InitializeRateLimitingTags(ref tags, policyName, method, route); | ||
|
||
_currentLeaseRequestsCounter.Add(-1, tags); | ||
|
||
var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); | ||
_leaseRequestDurationCounter.Record(duration.TotalSeconds, tags); | ||
} | ||
|
||
public void QueueStart(string? policyName, string? method, string? route) | ||
{ | ||
if (_currentRequestsQueuedCounter.Enabled) | ||
{ | ||
QueueStartCore(policyName, method, route); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void QueueStartCore(string? policyName, string? method, string? route) | ||
{ | ||
var tags = new TagList(); | ||
InitializeRateLimitingTags(ref tags, policyName, method, route); | ||
_currentRequestsQueuedCounter.Add(1, tags); | ||
} | ||
|
||
public void QueueEnd(string? policyName, string? method, string? route, RequestRejectionReason? reason, long startTimestamp, long currentTimestamp) | ||
{ | ||
if (_currentRequestsQueuedCounter.Enabled || _queuedRequestDurationCounter.Enabled) | ||
{ | ||
QueueEndCore(policyName, method, route, reason, startTimestamp, currentTimestamp); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void QueueEndCore(string? policyName, string? method, string? route, RequestRejectionReason? reason, long startTimestamp, long currentTimestamp) | ||
{ | ||
var tags = new TagList(); | ||
InitializeRateLimitingTags(ref tags, policyName, method, route); | ||
_currentRequestsQueuedCounter.Add(-1, tags); | ||
|
||
if (reason != null) | ||
{ | ||
tags.Add("reason", reason.Value.ToString()); | ||
} | ||
var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); | ||
_queuedRequestDurationCounter.Record(duration.TotalSeconds, tags); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_meter.Dispose(); | ||
} | ||
|
||
private static void InitializeRateLimitingTags(ref TagList tags, string? policyName, string? method, string? route) | ||
{ | ||
if (policyName is not null) | ||
{ | ||
tags.Add("policy", policyName); | ||
} | ||
if (method is not null) | ||
{ | ||
tags.Add("method", method); | ||
} | ||
if (route is not null) | ||
{ | ||
tags.Add("route", route); | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.