-
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 all 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.RateLimiting; | ||
|
||
internal readonly struct MetricsContext | ||
{ | ||
public readonly string? PolicyName; | ||
public readonly string? Method; | ||
public readonly string? Route; | ||
public readonly bool CurrentLeaseRequestsCounterEnabled; | ||
public readonly bool CurrentRequestsQueuedCounterEnabled; | ||
|
||
public MetricsContext(string? policyName, string? method, string? route, bool currentLeaseRequestsCounterEnabled, bool currentRequestsQueuedCounterEnabled) | ||
{ | ||
PolicyName = policyName; | ||
Method = method; | ||
Route = route; | ||
CurrentLeaseRequestsCounterEnabled = currentLeaseRequestsCounterEnabled; | ||
CurrentRequestsQueuedCounterEnabled = currentRequestsQueuedCounterEnabled; | ||
} | ||
} |
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,177 @@ | ||
// 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> _currentLeasedRequestsCounter; | ||
private readonly Histogram<double> _leasedRequestDurationCounter; | ||
private readonly UpDownCounter<long> _currentQueuedRequestsCounter; | ||
private readonly Histogram<double> _queuedRequestDurationCounter; | ||
private readonly Counter<long> _leaseFailedRequestsCounter; | ||
|
||
public RateLimitingMetrics(IMeterFactory meterFactory) | ||
{ | ||
_meter = meterFactory.CreateMeter(MeterName); | ||
|
||
_currentLeasedRequestsCounter = _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
|
||
|
||
_leasedRequestDurationCounter = _meter.CreateHistogram<double>( | ||
"leased-request-duration", | ||
unit: "s", | ||
description: "The duration of rate limiting leases held by HTTP requests on the server."); | ||
|
||
_currentQueuedRequestsCounter = _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 bool CurrentLeasedRequestsCounterEnabled => _currentLeasedRequestsCounter.Enabled; | ||
public bool CurrentQueuedRequestsCounterEnabled => _currentQueuedRequestsCounter.Enabled; | ||
|
||
public void LeaseFailed(in MetricsContext metricsContext, RequestRejectionReason reason) | ||
{ | ||
if (_leaseFailedRequestsCounter.Enabled) | ||
{ | ||
LeaseFailedCore(metricsContext, reason); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void LeaseFailedCore(in MetricsContext metricsContext, RequestRejectionReason reason) | ||
{ | ||
var tags = new TagList(); | ||
InitializeRateLimitingTags(ref tags, metricsContext); | ||
tags.Add("reason", reason.ToString()); | ||
_leaseFailedRequestsCounter.Add(1, tags); | ||
} | ||
|
||
public void LeaseStart(in MetricsContext metricsContext) | ||
{ | ||
if (metricsContext.CurrentLeaseRequestsCounterEnabled) | ||
{ | ||
LeaseStartCore(metricsContext); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public void LeaseStartCore(in MetricsContext metricsContext) | ||
{ | ||
var tags = new TagList(); | ||
InitializeRateLimitingTags(ref tags, metricsContext); | ||
_currentLeasedRequestsCounter.Add(1, tags); | ||
} | ||
|
||
public void LeaseEnd(in MetricsContext metricsContext, long startTimestamp, long currentTimestamp) | ||
{ | ||
if (metricsContext.CurrentLeaseRequestsCounterEnabled || _leasedRequestDurationCounter.Enabled) | ||
{ | ||
LeaseEndCore(metricsContext, startTimestamp, currentTimestamp); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void LeaseEndCore(in MetricsContext metricsContext, long startTimestamp, long currentTimestamp) | ||
{ | ||
var tags = new TagList(); | ||
InitializeRateLimitingTags(ref tags, metricsContext); | ||
|
||
if (metricsContext.CurrentLeaseRequestsCounterEnabled) | ||
{ | ||
_currentLeasedRequestsCounter.Add(-1, tags); | ||
} | ||
|
||
if (_leasedRequestDurationCounter.Enabled) | ||
{ | ||
var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); | ||
_leasedRequestDurationCounter.Record(duration.TotalSeconds, tags); | ||
} | ||
} | ||
|
||
public void QueueStart(in MetricsContext metricsContext) | ||
{ | ||
if (metricsContext.CurrentRequestsQueuedCounterEnabled) | ||
{ | ||
QueueStartCore(metricsContext); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void QueueStartCore(in MetricsContext metricsContext) | ||
{ | ||
var tags = new TagList(); | ||
InitializeRateLimitingTags(ref tags, metricsContext); | ||
_currentQueuedRequestsCounter.Add(1, tags); | ||
} | ||
|
||
public void QueueEnd(in MetricsContext metricsContext, RequestRejectionReason? reason, long startTimestamp, long currentTimestamp) | ||
{ | ||
if (metricsContext.CurrentRequestsQueuedCounterEnabled || _queuedRequestDurationCounter.Enabled) | ||
{ | ||
QueueEndCore(metricsContext, reason, startTimestamp, currentTimestamp); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private void QueueEndCore(in MetricsContext metricsContext, RequestRejectionReason? reason, long startTimestamp, long currentTimestamp) | ||
{ | ||
var tags = new TagList(); | ||
InitializeRateLimitingTags(ref tags, metricsContext); | ||
|
||
if (metricsContext.CurrentRequestsQueuedCounterEnabled) | ||
{ | ||
_currentQueuedRequestsCounter.Add(-1, tags); | ||
} | ||
|
||
if (_queuedRequestDurationCounter.Enabled) | ||
{ | ||
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, in MetricsContext metricsContext) | ||
{ | ||
if (metricsContext.PolicyName is not null) | ||
{ | ||
tags.Add("policy", metricsContext.PolicyName); | ||
} | ||
if (metricsContext.Method is not null) | ||
{ | ||
tags.Add("method", metricsContext.Method); | ||
} | ||
if (metricsContext.Route is not null) | ||
{ | ||
tags.Add("route", metricsContext.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.