| | | 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.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Diagnostics.Metrics; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | |
| | | 11 | | namespace System.Net.Http.Metrics |
| | | 12 | | { |
| | | 13 | | internal sealed class MetricsHandler : HttpMessageHandlerStage |
| | | 14 | | { |
| | | 15 | | private readonly HttpMessageHandler _innerHandler; |
| | | 16 | | private readonly UpDownCounter<long> _activeRequests; |
| | | 17 | | private readonly Histogram<double> _requestsDuration; |
| | | 18 | | private readonly IWebProxy? _proxy; |
| | | 19 | | |
| | 0 | 20 | | public MetricsHandler(HttpMessageHandler innerHandler, IMeterFactory? meterFactory, IWebProxy? proxy, out Meter |
| | 0 | 21 | | { |
| | 0 | 22 | | Debug.Assert(GlobalHttpSettings.MetricsHandler.IsGloballyEnabled); |
| | | 23 | | |
| | 0 | 24 | | _innerHandler = innerHandler; |
| | 0 | 25 | | _proxy = proxy; |
| | | 26 | | |
| | 0 | 27 | | meter = meterFactory?.Create("System.Net.Http") ?? SharedMeter.Instance; |
| | | 28 | | |
| | | 29 | | // Meter has a cache for the instruments it owns |
| | 0 | 30 | | _activeRequests = meter.CreateUpDownCounter<long>( |
| | 0 | 31 | | "http.client.active_requests", |
| | 0 | 32 | | unit: "{request}", |
| | 0 | 33 | | description: "Number of outbound HTTP requests that are currently active on the client."); |
| | 0 | 34 | | _requestsDuration = meter.CreateHistogram<double>( |
| | 0 | 35 | | "http.client.request.duration", |
| | 0 | 36 | | unit: "s", |
| | 0 | 37 | | description: "Duration of HTTP client requests.", |
| | 0 | 38 | | advice: DiagnosticsHelper.ShortHistogramAdvice); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | internal override ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, CancellationT |
| | 0 | 42 | | { |
| | 0 | 43 | | if (_activeRequests.Enabled || _requestsDuration.Enabled) |
| | 0 | 44 | | { |
| | 0 | 45 | | return SendAsyncWithMetrics(request, async, cancellationToken); |
| | | 46 | | } |
| | | 47 | | else |
| | 0 | 48 | | { |
| | 0 | 49 | | return async ? |
| | 0 | 50 | | new ValueTask<HttpResponseMessage>(_innerHandler.SendAsync(request, cancellationToken)) : |
| | 0 | 51 | | new ValueTask<HttpResponseMessage>(_innerHandler.Send(request, cancellationToken)); |
| | | 52 | | } |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | private async ValueTask<HttpResponseMessage> SendAsyncWithMetrics(HttpRequestMessage request, bool async, Cancel |
| | 0 | 56 | | { |
| | 0 | 57 | | Debug.Assert(GlobalHttpSettings.MetricsHandler.IsGloballyEnabled); |
| | | 58 | | |
| | 0 | 59 | | (long startTimestamp, bool recordCurrentRequests) = RequestStart(request); |
| | 0 | 60 | | HttpResponseMessage? response = null; |
| | 0 | 61 | | Exception? exception = null; |
| | | 62 | | try |
| | 0 | 63 | | { |
| | 0 | 64 | | response = async ? |
| | 0 | 65 | | await _innerHandler.SendAsync(request, cancellationToken).ConfigureAwait(false) : |
| | 0 | 66 | | _innerHandler.Send(request, cancellationToken); |
| | 0 | 67 | | return response; |
| | | 68 | | } |
| | 0 | 69 | | catch (Exception ex) |
| | 0 | 70 | | { |
| | 0 | 71 | | exception = ex; |
| | 0 | 72 | | throw; |
| | | 73 | | } |
| | | 74 | | finally |
| | 0 | 75 | | { |
| | 0 | 76 | | RequestStop(request, response, exception, startTimestamp, recordCurrentRequests); |
| | 0 | 77 | | } |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | protected override void Dispose(bool disposing) |
| | 0 | 81 | | { |
| | 0 | 82 | | if (disposing) |
| | 0 | 83 | | { |
| | 0 | 84 | | _innerHandler.Dispose(); |
| | 0 | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | base.Dispose(disposing); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | private (long StartTimestamp, bool RecordCurrentRequests) RequestStart(HttpRequestMessage request) |
| | 0 | 91 | | { |
| | 0 | 92 | | bool recordCurrentRequests = _activeRequests.Enabled; |
| | 0 | 93 | | long startTimestamp = Stopwatch.GetTimestamp(); |
| | | 94 | | |
| | 0 | 95 | | if (recordCurrentRequests) |
| | 0 | 96 | | { |
| | 0 | 97 | | TagList tags = InitializeCommonTags(request); |
| | 0 | 98 | | _activeRequests.Add(1, tags); |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | return (startTimestamp, recordCurrentRequests); |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | private void RequestStop(HttpRequestMessage request, HttpResponseMessage? response, Exception? exception, long s |
| | 0 | 105 | | { |
| | 0 | 106 | | TagList tags = InitializeCommonTags(request); |
| | | 107 | | |
| | 0 | 108 | | if (recordCurrentRequests) |
| | 0 | 109 | | { |
| | 0 | 110 | | _activeRequests.Add(-1, tags); |
| | 0 | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | if (!_requestsDuration.Enabled) |
| | 0 | 114 | | { |
| | 0 | 115 | | return; |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | if (response is not null) |
| | 0 | 119 | | { |
| | 0 | 120 | | tags.Add("http.response.status_code", DiagnosticsHelper.GetBoxedInt32((int)response.StatusCode)); |
| | 0 | 121 | | tags.Add("network.protocol.version", DiagnosticsHelper.GetProtocolVersionString(response.Version)); |
| | 0 | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | if (DiagnosticsHelper.TryGetErrorType(response, exception, out string? errorType)) |
| | 0 | 125 | | { |
| | 0 | 126 | | tags.Add("error.type", errorType); |
| | 0 | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | TimeSpan durationTime = Stopwatch.GetElapsedTime(startTimestamp, Stopwatch.GetTimestamp()); |
| | | 130 | | |
| | 0 | 131 | | List<Action<HttpMetricsEnrichmentContext>>? callbacks = HttpMetricsEnrichmentContext.GetEnrichmentCallbacksF |
| | 0 | 132 | | if (callbacks is null) |
| | 0 | 133 | | { |
| | 0 | 134 | | _requestsDuration.Record(durationTime.TotalSeconds, tags); |
| | 0 | 135 | | } |
| | | 136 | | else |
| | 0 | 137 | | { |
| | 0 | 138 | | HttpMetricsEnrichmentContext.RecordDurationWithEnrichment(callbacks, request, response, exception, durat |
| | 0 | 139 | | } |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | private TagList InitializeCommonTags(HttpRequestMessage request) |
| | 0 | 143 | | { |
| | 0 | 144 | | TagList tags = default; |
| | | 145 | | |
| | 0 | 146 | | if (request.RequestUri is Uri requestUri && requestUri.IsAbsoluteUri) |
| | 0 | 147 | | { |
| | 0 | 148 | | tags.Add("url.scheme", requestUri.Scheme); |
| | 0 | 149 | | tags.Add("server.address", DiagnosticsHelper.GetServerAddress(request, _proxy)); |
| | 0 | 150 | | tags.Add("server.port", DiagnosticsHelper.GetBoxedInt32(requestUri.Port)); |
| | 0 | 151 | | } |
| | 0 | 152 | | tags.Add(DiagnosticsHelper.GetMethodTag(request.Method, out _)); |
| | | 153 | | |
| | 0 | 154 | | return tags; |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | private sealed class SharedMeter : Meter |
| | | 158 | | { |
| | 0 | 159 | | public static Meter Instance { get; } = new SharedMeter(); |
| | | 160 | | private SharedMeter() |
| | 0 | 161 | | : base("System.Net.Http") |
| | 0 | 162 | | { |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | protected override void Dispose(bool disposing) |
| | 0 | 166 | | { |
| | | 167 | | // NOP to prevent disposing the global instance from MeterListener callbacks. |
| | 0 | 168 | | } |
| | | 169 | | } |
| | | 170 | | } |
| | | 171 | | } |