|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Diagnostics.Metrics; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using Microsoft.Extensions.Metrics; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.RateLimiting; |
| 10 | + |
| 11 | +internal sealed class RateLimitingMetrics : IDisposable |
| 12 | +{ |
| 13 | + public const string MeterName = "Microsoft.AspNetCore.RateLimiting"; |
| 14 | + |
| 15 | + private readonly Meter _meter; |
| 16 | + private readonly UpDownCounter<long> _currentLeasedRequestsCounter; |
| 17 | + private readonly Histogram<double> _leasedRequestDurationCounter; |
| 18 | + private readonly UpDownCounter<long> _currentQueuedRequestsCounter; |
| 19 | + private readonly Histogram<double> _queuedRequestDurationCounter; |
| 20 | + private readonly Counter<long> _leaseFailedRequestsCounter; |
| 21 | + |
| 22 | + public RateLimitingMetrics(IMeterFactory meterFactory) |
| 23 | + { |
| 24 | + _meter = meterFactory.CreateMeter(MeterName); |
| 25 | + |
| 26 | + _currentLeasedRequestsCounter = _meter.CreateUpDownCounter<long>( |
| 27 | + "current-leased-requests", |
| 28 | + description: "Number of HTTP requests that are currently active on the server that hold a rate limiting lease."); |
| 29 | + |
| 30 | + _leasedRequestDurationCounter = _meter.CreateHistogram<double>( |
| 31 | + "leased-request-duration", |
| 32 | + unit: "s", |
| 33 | + description: "The duration of rate limiting leases held by HTTP requests on the server."); |
| 34 | + |
| 35 | + _currentQueuedRequestsCounter = _meter.CreateUpDownCounter<long>( |
| 36 | + "current-queued-requests", |
| 37 | + description: "Number of HTTP requests that are currently queued, waiting to acquire a rate limiting lease."); |
| 38 | + |
| 39 | + _queuedRequestDurationCounter = _meter.CreateHistogram<double>( |
| 40 | + "queued-request-duration", |
| 41 | + unit: "s", |
| 42 | + description: "The duration of HTTP requests in a queue, waiting to acquire a rate limiting lease."); |
| 43 | + |
| 44 | + _leaseFailedRequestsCounter = _meter.CreateCounter<long>( |
| 45 | + "lease-failed-requests", |
| 46 | + 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."); |
| 47 | + } |
| 48 | + |
| 49 | + public bool CurrentLeasedRequestsCounterEnabled => _currentLeasedRequestsCounter.Enabled; |
| 50 | + public bool CurrentQueuedRequestsCounterEnabled => _currentQueuedRequestsCounter.Enabled; |
| 51 | + |
| 52 | + public void LeaseFailed(in MetricsContext metricsContext, RequestRejectionReason reason) |
| 53 | + { |
| 54 | + if (_leaseFailedRequestsCounter.Enabled) |
| 55 | + { |
| 56 | + LeaseFailedCore(metricsContext, reason); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 61 | + private void LeaseFailedCore(in MetricsContext metricsContext, RequestRejectionReason reason) |
| 62 | + { |
| 63 | + var tags = new TagList(); |
| 64 | + InitializeRateLimitingTags(ref tags, metricsContext); |
| 65 | + tags.Add("reason", reason.ToString()); |
| 66 | + _leaseFailedRequestsCounter.Add(1, tags); |
| 67 | + } |
| 68 | + |
| 69 | + public void LeaseStart(in MetricsContext metricsContext) |
| 70 | + { |
| 71 | + if (metricsContext.CurrentLeaseRequestsCounterEnabled) |
| 72 | + { |
| 73 | + LeaseStartCore(metricsContext); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 78 | + public void LeaseStartCore(in MetricsContext metricsContext) |
| 79 | + { |
| 80 | + var tags = new TagList(); |
| 81 | + InitializeRateLimitingTags(ref tags, metricsContext); |
| 82 | + _currentLeasedRequestsCounter.Add(1, tags); |
| 83 | + } |
| 84 | + |
| 85 | + public void LeaseEnd(in MetricsContext metricsContext, long startTimestamp, long currentTimestamp) |
| 86 | + { |
| 87 | + if (metricsContext.CurrentLeaseRequestsCounterEnabled || _leasedRequestDurationCounter.Enabled) |
| 88 | + { |
| 89 | + LeaseEndCore(metricsContext, startTimestamp, currentTimestamp); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 94 | + private void LeaseEndCore(in MetricsContext metricsContext, long startTimestamp, long currentTimestamp) |
| 95 | + { |
| 96 | + var tags = new TagList(); |
| 97 | + InitializeRateLimitingTags(ref tags, metricsContext); |
| 98 | + |
| 99 | + if (metricsContext.CurrentLeaseRequestsCounterEnabled) |
| 100 | + { |
| 101 | + _currentLeasedRequestsCounter.Add(-1, tags); |
| 102 | + } |
| 103 | + |
| 104 | + if (_leasedRequestDurationCounter.Enabled) |
| 105 | + { |
| 106 | + var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); |
| 107 | + _leasedRequestDurationCounter.Record(duration.TotalSeconds, tags); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public void QueueStart(in MetricsContext metricsContext) |
| 112 | + { |
| 113 | + if (metricsContext.CurrentRequestsQueuedCounterEnabled) |
| 114 | + { |
| 115 | + QueueStartCore(metricsContext); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 120 | + private void QueueStartCore(in MetricsContext metricsContext) |
| 121 | + { |
| 122 | + var tags = new TagList(); |
| 123 | + InitializeRateLimitingTags(ref tags, metricsContext); |
| 124 | + _currentQueuedRequestsCounter.Add(1, tags); |
| 125 | + } |
| 126 | + |
| 127 | + public void QueueEnd(in MetricsContext metricsContext, RequestRejectionReason? reason, long startTimestamp, long currentTimestamp) |
| 128 | + { |
| 129 | + if (metricsContext.CurrentRequestsQueuedCounterEnabled || _queuedRequestDurationCounter.Enabled) |
| 130 | + { |
| 131 | + QueueEndCore(metricsContext, reason, startTimestamp, currentTimestamp); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 136 | + private void QueueEndCore(in MetricsContext metricsContext, RequestRejectionReason? reason, long startTimestamp, long currentTimestamp) |
| 137 | + { |
| 138 | + var tags = new TagList(); |
| 139 | + InitializeRateLimitingTags(ref tags, metricsContext); |
| 140 | + |
| 141 | + if (metricsContext.CurrentRequestsQueuedCounterEnabled) |
| 142 | + { |
| 143 | + _currentQueuedRequestsCounter.Add(-1, tags); |
| 144 | + } |
| 145 | + |
| 146 | + if (_queuedRequestDurationCounter.Enabled) |
| 147 | + { |
| 148 | + if (reason != null) |
| 149 | + { |
| 150 | + tags.Add("reason", reason.Value.ToString()); |
| 151 | + } |
| 152 | + var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); |
| 153 | + _queuedRequestDurationCounter.Record(duration.TotalSeconds, tags); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public void Dispose() |
| 158 | + { |
| 159 | + _meter.Dispose(); |
| 160 | + } |
| 161 | + |
| 162 | + private static void InitializeRateLimitingTags(ref TagList tags, in MetricsContext metricsContext) |
| 163 | + { |
| 164 | + if (metricsContext.PolicyName is not null) |
| 165 | + { |
| 166 | + tags.Add("policy", metricsContext.PolicyName); |
| 167 | + } |
| 168 | + if (metricsContext.Method is not null) |
| 169 | + { |
| 170 | + tags.Add("method", metricsContext.Method); |
| 171 | + } |
| 172 | + if (metricsContext.Route is not null) |
| 173 | + { |
| 174 | + tags.Add("route", metricsContext.Route); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments