| | | 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 | | |
| | | 7 | | namespace System.Net.Http.Metrics |
| | | 8 | | { |
| | 0 | 9 | | internal sealed class SocketsHttpHandlerMetrics(Meter meter) |
| | | 10 | | { |
| | 0 | 11 | | public readonly UpDownCounter<long> OpenConnections = meter.CreateUpDownCounter<long>( |
| | 0 | 12 | | name: "http.client.open_connections", |
| | 0 | 13 | | unit: "{connection}", |
| | 0 | 14 | | description: "Number of outbound HTTP connections that are currently active or idle on the client."); |
| | | 15 | | |
| | 0 | 16 | | public readonly Histogram<double> ConnectionDuration = meter.CreateHistogram<double>( |
| | 0 | 17 | | name: "http.client.connection.duration", |
| | 0 | 18 | | unit: "s", |
| | 0 | 19 | | description: "The duration of successfully established outbound HTTP connections.", |
| | 0 | 20 | | advice: new InstrumentAdvice<double>() |
| | 0 | 21 | | { |
| | 0 | 22 | | // These values are not based on a standard and may change in the future. |
| | 0 | 23 | | HistogramBucketBoundaries = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60, 120, 300] |
| | 0 | 24 | | }); |
| | | 25 | | |
| | 0 | 26 | | public readonly Histogram<double> RequestsQueueDuration = meter.CreateHistogram<double>( |
| | 0 | 27 | | name: "http.client.request.time_in_queue", |
| | 0 | 28 | | unit: "s", |
| | 0 | 29 | | description: "The amount of time requests spent on a queue waiting for an available connection.", |
| | 0 | 30 | | advice: DiagnosticsHelper.ShortHistogramAdvice); |
| | | 31 | | |
| | | 32 | | public void RequestLeftQueue(HttpRequestMessage request, HttpConnectionPool pool, TimeSpan duration, int version |
| | 0 | 33 | | { |
| | 0 | 34 | | Debug.Assert(versionMajor is 1 or 2 or 3); |
| | | 35 | | |
| | 0 | 36 | | if (RequestsQueueDuration.Enabled) |
| | 0 | 37 | | { |
| | 0 | 38 | | TagList tags = default; |
| | | 39 | | |
| | | 40 | | // While requests may report HTTP/1.0 as the protocol, we treat all HTTP/1.X connections as HTTP/1.1. |
| | 0 | 41 | | tags.Add("network.protocol.version", versionMajor switch |
| | 0 | 42 | | { |
| | 0 | 43 | | 1 => "1.1", |
| | 0 | 44 | | 2 => "2", |
| | 0 | 45 | | _ => "3" |
| | 0 | 46 | | }); |
| | | 47 | | |
| | 0 | 48 | | tags.Add("url.scheme", pool.IsSecure ? "https" : "http"); |
| | | 49 | | |
| | 0 | 50 | | Debug.Assert(pool.TelemetryServerAddress is not null, "TelemetryServerAddress should not be null when Sy |
| | 0 | 51 | | tags.Add("server.address", pool.TelemetryServerAddress); |
| | | 52 | | |
| | 0 | 53 | | if (!pool.IsDefaultPort) |
| | 0 | 54 | | { |
| | 0 | 55 | | tags.Add("server.port", pool.OriginAuthority.Port); |
| | 0 | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | tags.Add(DiagnosticsHelper.GetMethodTag(request.Method, out _)); |
| | | 59 | | |
| | 0 | 60 | | RequestsQueueDuration.Record(duration.TotalSeconds, tags); |
| | 0 | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | } |
| | | 64 | | } |