| | | 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.Collections.Concurrent; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Diagnostics.Metrics; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | |
| | | 12 | | namespace System.Net.Http.Metrics |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Represents a unique combination of tags for tracking active requests. |
| | | 16 | | /// </summary> |
| | | 17 | | internal readonly struct ActiveRequestsTagKey : IEquatable<ActiveRequestsTagKey> |
| | | 18 | | { |
| | | 19 | | public readonly string? Scheme; |
| | | 20 | | public readonly string? Host; |
| | | 21 | | public readonly int Port; |
| | | 22 | | public readonly string Method; |
| | | 23 | | private readonly int _hashCode; |
| | | 24 | | |
| | | 25 | | public ActiveRequestsTagKey(string? scheme, string? host, int port, string method) |
| | | 26 | | { |
| | | 27 | | Scheme = scheme; |
| | | 28 | | Host = host; |
| | | 29 | | Port = port; |
| | | 30 | | Method = method; |
| | | 31 | | _hashCode = HashCode.Combine(scheme, host, port, method); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public bool Equals(ActiveRequestsTagKey other) => |
| | | 35 | | Scheme == other.Scheme && |
| | | 36 | | Host == other.Host && |
| | | 37 | | Port == other.Port && |
| | | 38 | | Method == other.Method; |
| | | 39 | | |
| | | 40 | | public override bool Equals(object? obj) => obj is ActiveRequestsTagKey other && Equals(other); |
| | | 41 | | |
| | | 42 | | public override int GetHashCode() => _hashCode; |
| | | 43 | | |
| | | 44 | | public TagList ToTagList() |
| | | 45 | | { |
| | | 46 | | TagList tags = default; |
| | | 47 | | if (Scheme is not null) |
| | | 48 | | { |
| | | 49 | | tags.Add("url.scheme", Scheme); |
| | | 50 | | tags.Add("server.address", Host); |
| | | 51 | | tags.Add("server.port", DiagnosticsHelper.GetBoxedInt32(Port)); |
| | | 52 | | } |
| | | 53 | | tags.Add("http.request.method", Method); |
| | | 54 | | return tags; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public override string ToString() => |
| | | 58 | | $"{Method}{(Scheme is not null ? $" {Scheme}://{Host}:{Port}" : "")}"; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Thread-safe tracker for active request counts by tag combination. |
| | | 63 | | /// </summary> |
| | | 64 | | internal sealed class ActiveRequestsTracker |
| | | 65 | | { |
| | | 66 | | private readonly ConcurrentDictionary<ActiveRequestsTagKey, long> _counts = new(); |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Increments the count for the specified tag combination. |
| | | 70 | | /// </summary> |
| | | 71 | | public void Increment(in ActiveRequestsTagKey key) |
| | | 72 | | { |
| | | 73 | | _counts.AddOrUpdate(key, 1, static (_, currentValue) => currentValue + 1); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Decrements the count for the specified tag combination. |
| | | 78 | | /// Removes the entry if the count reaches zero. |
| | | 79 | | /// </summary> |
| | | 80 | | public void Decrement(in ActiveRequestsTagKey key) |
| | | 81 | | { |
| | | 82 | | // We need to atomically decrement and remove if zero. |
| | | 83 | | // Use a spin loop with TryGetValue/TryUpdate/TryRemove to handle this safely. |
| | | 84 | | while (true) |
| | | 85 | | { |
| | | 86 | | if (!_counts.TryGetValue(key, out long currentValue)) |
| | | 87 | | { |
| | | 88 | | // Key doesn't exist, nothing to decrement. |
| | | 89 | | // This shouldn't happen in normal operation but we handle it gracefully. |
| | | 90 | | Debug.Fail($"Decrement for non-existing request {key}"); |
| | | 91 | | return; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | if (currentValue <= 1) |
| | | 95 | | { |
| | | 96 | | // Try to remove the entry since it will become zero. |
| | | 97 | | // Use the overload that checks the current value to ensure atomicity. |
| | | 98 | | if (_counts.TryRemove(new KeyValuePair<ActiveRequestsTagKey, long>(key, currentValue))) |
| | | 99 | | { |
| | | 100 | | return; |
| | | 101 | | } |
| | | 102 | | // Another thread modified the value, retry. |
| | | 103 | | } |
| | | 104 | | else |
| | | 105 | | { |
| | | 106 | | // Try to decrement the value. |
| | | 107 | | if (_counts.TryUpdate(key, currentValue - 1, currentValue)) |
| | | 108 | | { |
| | | 109 | | return; |
| | | 110 | | } |
| | | 111 | | // Another thread modified the value, retry. |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Returns measurements for all tag combinations with non-zero counts. |
| | | 118 | | /// </summary> |
| | | 119 | | public IEnumerable<Measurement<long>> GetMeasurements() |
| | | 120 | | { |
| | | 121 | | foreach (KeyValuePair<ActiveRequestsTagKey, long> entry in _counts) |
| | | 122 | | { |
| | | 123 | | yield return new Measurement<long>(entry.Value, entry.Key.ToTagList()); |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | internal sealed class MetricsHandler : HttpMessageHandlerStage |
| | | 129 | | { |
| | | 130 | | private readonly HttpMessageHandler _innerHandler; |
| | 0 | 131 | | private readonly ActiveRequestsTracker _activeRequestsTracker = new(); |
| | | 132 | | private readonly ObservableUpDownCounter<long> _activeRequests; |
| | | 133 | | private readonly Histogram<double> _requestsDuration; |
| | | 134 | | private readonly IWebProxy? _proxy; |
| | | 135 | | |
| | 0 | 136 | | public MetricsHandler(HttpMessageHandler innerHandler, IMeterFactory? meterFactory, IWebProxy? proxy, out Meter |
| | 0 | 137 | | { |
| | 0 | 138 | | Debug.Assert(GlobalHttpSettings.MetricsHandler.IsGloballyEnabled); |
| | | 139 | | |
| | 0 | 140 | | _innerHandler = innerHandler; |
| | 0 | 141 | | _proxy = proxy; |
| | | 142 | | |
| | 0 | 143 | | meter = meterFactory?.Create("System.Net.Http") ?? SharedMeter.Instance; |
| | | 144 | | |
| | | 145 | | // Meter has a cache for the instruments it owns |
| | 0 | 146 | | _activeRequests = meter.CreateObservableUpDownCounter<long>( |
| | 0 | 147 | | "http.client.active_requests", |
| | 0 | 148 | | observeValues: _activeRequestsTracker.GetMeasurements, |
| | 0 | 149 | | unit: "{request}", |
| | 0 | 150 | | description: "Number of outbound HTTP requests that are currently active on the client."); |
| | 0 | 151 | | _requestsDuration = meter.CreateHistogram<double>( |
| | 0 | 152 | | "http.client.request.duration", |
| | 0 | 153 | | unit: "s", |
| | 0 | 154 | | description: "Duration of HTTP client requests.", |
| | 0 | 155 | | advice: DiagnosticsHelper.ShortHistogramAdvice); |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | internal override ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, CancellationT |
| | 0 | 159 | | { |
| | 0 | 160 | | if (_activeRequests.Enabled || _requestsDuration.Enabled) |
| | 0 | 161 | | { |
| | 0 | 162 | | return SendAsyncWithMetrics(request, async, cancellationToken); |
| | | 163 | | } |
| | | 164 | | else |
| | 0 | 165 | | { |
| | 0 | 166 | | return async ? |
| | 0 | 167 | | new ValueTask<HttpResponseMessage>(_innerHandler.SendAsync(request, cancellationToken)) : |
| | 0 | 168 | | new ValueTask<HttpResponseMessage>(_innerHandler.Send(request, cancellationToken)); |
| | | 169 | | } |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | private async ValueTask<HttpResponseMessage> SendAsyncWithMetrics(HttpRequestMessage request, bool async, Cancel |
| | 0 | 173 | | { |
| | 0 | 174 | | Debug.Assert(GlobalHttpSettings.MetricsHandler.IsGloballyEnabled); |
| | | 175 | | |
| | 0 | 176 | | (long startTimestamp, bool recordCurrentRequests, ActiveRequestsTagKey requestTagKey) = RequestStart(request |
| | 0 | 177 | | HttpResponseMessage? response = null; |
| | 0 | 178 | | Exception? exception = null; |
| | | 179 | | try |
| | 0 | 180 | | { |
| | 0 | 181 | | response = async ? |
| | 0 | 182 | | await _innerHandler.SendAsync(request, cancellationToken).ConfigureAwait(false) : |
| | 0 | 183 | | _innerHandler.Send(request, cancellationToken); |
| | 0 | 184 | | return response; |
| | | 185 | | } |
| | 0 | 186 | | catch (Exception ex) |
| | 0 | 187 | | { |
| | 0 | 188 | | exception = ex; |
| | 0 | 189 | | throw; |
| | | 190 | | } |
| | | 191 | | finally |
| | 0 | 192 | | { |
| | 0 | 193 | | RequestStop(request, response, exception, startTimestamp, recordCurrentRequests, requestTagKey); |
| | 0 | 194 | | } |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | protected override void Dispose(bool disposing) |
| | 0 | 198 | | { |
| | 0 | 199 | | if (disposing) |
| | 0 | 200 | | { |
| | 0 | 201 | | _innerHandler.Dispose(); |
| | 0 | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | base.Dispose(disposing); |
| | 0 | 205 | | } |
| | | 206 | | |
| | | 207 | | private (long StartTimestamp, bool RecordCurrentRequests, ActiveRequestsTagKey RequestTagKey) RequestStart(HttpR |
| | 0 | 208 | | { |
| | 0 | 209 | | bool recordCurrentRequests = _activeRequests.Enabled; |
| | 0 | 210 | | long startTimestamp = Stopwatch.GetTimestamp(); |
| | | 211 | | |
| | 0 | 212 | | ActiveRequestsTagKey requestTagKey = CreateActiveRequestsTagKey(request); |
| | 0 | 213 | | if (recordCurrentRequests) |
| | 0 | 214 | | { |
| | 0 | 215 | | _activeRequestsTracker.Increment(requestTagKey); |
| | 0 | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | return (startTimestamp, recordCurrentRequests, requestTagKey); |
| | 0 | 219 | | } |
| | | 220 | | |
| | | 221 | | private void RequestStop(HttpRequestMessage request, HttpResponseMessage? response, Exception? exception, long s |
| | 0 | 222 | | { |
| | 0 | 223 | | if (recordCurrentRequests) |
| | 0 | 224 | | { |
| | 0 | 225 | | _activeRequestsTracker.Decrement(requestTagKey); |
| | 0 | 226 | | } |
| | | 227 | | |
| | 0 | 228 | | if (!_requestsDuration.Enabled) |
| | 0 | 229 | | { |
| | 0 | 230 | | return; |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | TagList tags = requestTagKey.ToTagList(); |
| | 0 | 234 | | if (response is not null) |
| | 0 | 235 | | { |
| | 0 | 236 | | tags.Add("http.response.status_code", DiagnosticsHelper.GetBoxedInt32((int)response.StatusCode)); |
| | 0 | 237 | | tags.Add("network.protocol.version", DiagnosticsHelper.GetProtocolVersionString(response.Version)); |
| | 0 | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | if (DiagnosticsHelper.TryGetErrorType(response, exception, out string? errorType)) |
| | 0 | 241 | | { |
| | 0 | 242 | | tags.Add("error.type", errorType); |
| | 0 | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | TimeSpan durationTime = Stopwatch.GetElapsedTime(startTimestamp, Stopwatch.GetTimestamp()); |
| | | 246 | | |
| | 0 | 247 | | List<Action<HttpMetricsEnrichmentContext>>? callbacks = HttpMetricsEnrichmentContext.GetEnrichmentCallbacksF |
| | 0 | 248 | | if (callbacks is null) |
| | 0 | 249 | | { |
| | 0 | 250 | | _requestsDuration.Record(durationTime.TotalSeconds, tags); |
| | 0 | 251 | | } |
| | | 252 | | else |
| | 0 | 253 | | { |
| | 0 | 254 | | HttpMetricsEnrichmentContext.RecordDurationWithEnrichment(callbacks, request, response, exception, durat |
| | 0 | 255 | | } |
| | 0 | 256 | | } |
| | | 257 | | |
| | | 258 | | private ActiveRequestsTagKey CreateActiveRequestsTagKey(HttpRequestMessage request) |
| | 0 | 259 | | { |
| | 0 | 260 | | string? scheme = null; |
| | 0 | 261 | | string? host = null; |
| | 0 | 262 | | int port = 0; |
| | | 263 | | |
| | 0 | 264 | | if (request.RequestUri is Uri requestUri && requestUri.IsAbsoluteUri) |
| | 0 | 265 | | { |
| | 0 | 266 | | scheme = requestUri.Scheme; |
| | 0 | 267 | | host = DiagnosticsHelper.GetServerAddress(request, _proxy); |
| | 0 | 268 | | port = requestUri.Port; |
| | 0 | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | string method = (string)DiagnosticsHelper.GetMethodTag(request.Method, out _).Value!; |
| | | 272 | | |
| | 0 | 273 | | return new ActiveRequestsTagKey(scheme, host, port, method); |
| | 0 | 274 | | } |
| | | 275 | | |
| | | 276 | | private sealed class SharedMeter : Meter |
| | | 277 | | { |
| | 0 | 278 | | public static Meter Instance { get; } = new SharedMeter(); |
| | | 279 | | private SharedMeter() |
| | 0 | 280 | | : base("System.Net.Http") |
| | 0 | 281 | | { |
| | 0 | 282 | | } |
| | | 283 | | |
| | | 284 | | protected override void Dispose(bool disposing) |
| | 0 | 285 | | { |
| | | 286 | | // NOP to prevent disposing the global instance from MeterListener callbacks. |
| | 0 | 287 | | } |
| | | 288 | | } |
| | | 289 | | } |
| | | 290 | | } |
| | | 291 | | |