< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 311
Coverable lines: 311
Total lines: 525
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 70
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpTelemetry.AnyOS.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5using System.Diagnostics.Tracing;
 6using System.Threading;
 7
 8namespace System.Net.Http
 9{
 10    internal sealed partial class HttpTelemetry
 11    {
 12        private IncrementingPollingCounter? _startedRequestsPerSecondCounter;
 13        private IncrementingPollingCounter? _failedRequestsPerSecondCounter;
 14        private PollingCounter? _startedRequestsCounter;
 15        private PollingCounter? _currentRequestsCounter;
 16        private PollingCounter? _failedRequestsCounter;
 17        private PollingCounter? _totalHttp11ConnectionsCounter;
 18        private PollingCounter? _totalHttp20ConnectionsCounter;
 19        private PollingCounter? _totalHttp30ConnectionsCounter;
 20        private EventCounter? _http11RequestsQueueDurationCounter;
 21        private EventCounter? _http20RequestsQueueDurationCounter;
 22        private EventCounter? _http30RequestsQueueDurationCounter;
 23
 24        [NonEvent]
 25        public void RequestLeftQueue(int versionMajor, TimeSpan duration)
 026        {
 027            Debug.Assert(versionMajor is 1 or 2 or 3);
 28
 029            EventCounter? counter = versionMajor switch
 030            {
 031                1 => _http11RequestsQueueDurationCounter,
 032                2 => _http20RequestsQueueDurationCounter,
 033                _ => _http30RequestsQueueDurationCounter
 034            };
 35
 036            double timeOnQueueMs = duration.TotalMilliseconds;
 37
 038            counter?.WriteMetric(timeOnQueueMs);
 39
 040            RequestLeftQueue(timeOnQueueMs, (byte)versionMajor, versionMinor: versionMajor == 1 ? (byte)1 : (byte)0);
 041        }
 42
 43        protected override void OnEventCommand(EventCommandEventArgs command)
 044        {
 045            if (command.Command == EventCommand.Enable)
 046            {
 47                // This is the convention for initializing counters in the RuntimeEventSource (lazily on the first enabl
 48                // They aren't disabled afterwards...
 49
 50                // The cumulative number of HTTP requests started since the process started.
 051                _startedRequestsCounter ??= new PollingCounter("requests-started", this, () => Interlocked.Read(ref _sta
 052                {
 053                    DisplayName = "Requests Started",
 054                };
 55
 56                // The number of HTTP requests started per second since the process started.
 057                _startedRequestsPerSecondCounter ??= new IncrementingPollingCounter("requests-started-rate", this, () =>
 058                {
 059                    DisplayName = "Requests Started Rate",
 060                    DisplayRateTimeScale = TimeSpan.FromSeconds(1)
 061                };
 62
 63                // The cumulative number of HTTP requests failed since the process started.
 64                // Failed means that an exception occurred during the handler's Send(Async) call as a result of a connec
 65                // In case of using HttpClient's SendAsync(and friends) with buffering, this includes exceptions that oc
 66                // In case of using HttpClient's helper methods (GetString/ByteArray/Stream), this includes responses wi
 067                _failedRequestsCounter ??= new PollingCounter("requests-failed", this, () => Interlocked.Read(ref _faile
 068                {
 069                    DisplayName = "Requests Failed"
 070                };
 71
 72                // The number of HTTP requests failed per second since the process started.
 073                _failedRequestsPerSecondCounter ??= new IncrementingPollingCounter("requests-failed-rate", this, () => I
 074                {
 075                    DisplayName = "Requests Failed Rate",
 076                    DisplayRateTimeScale = TimeSpan.FromSeconds(1)
 077                };
 78
 79                // The current number of active HTTP requests that have started but not yet completed or failed.
 80                // Use (-_stoppedRequests + _startedRequests) to avoid returning a negative value if _stoppedRequests is
 81                // incremented after reading _startedRequests due to race conditions with completing the HTTP request.
 082                _currentRequestsCounter ??= new PollingCounter("current-requests", this, () => -Interlocked.Read(ref _st
 083                {
 084                    DisplayName = "Current Requests"
 085                };
 86
 087                _totalHttp11ConnectionsCounter ??= new PollingCounter("http11-connections-current-total", this, () => In
 088                {
 089                    DisplayName = "Current Http 1.1 Connections"
 090                };
 91
 092                _totalHttp20ConnectionsCounter ??= new PollingCounter("http20-connections-current-total", this, () => In
 093                {
 094                    DisplayName = "Current Http 2.0 Connections"
 095                };
 96
 097                _totalHttp30ConnectionsCounter ??= new PollingCounter("http30-connections-current-total", this, () => In
 098                {
 099                    DisplayName = "Current Http 3.0 Connections"
 0100                };
 101
 0102                _http11RequestsQueueDurationCounter ??= new EventCounter("http11-requests-queue-duration", this)
 0103                {
 0104                    DisplayName = "HTTP 1.1 Requests Queue Duration",
 0105                    DisplayUnits = "ms"
 0106                };
 107
 0108                _http20RequestsQueueDurationCounter ??= new EventCounter("http20-requests-queue-duration", this)
 0109                {
 0110                    DisplayName = "HTTP 2.0 Requests Queue Duration",
 0111                    DisplayUnits = "ms"
 0112                };
 113
 0114                _http30RequestsQueueDurationCounter ??= new EventCounter("http30-requests-queue-duration", this)
 0115                {
 0116                    DisplayName = "HTTP 3.0 Requests Queue Duration",
 0117                    DisplayUnits = "ms"
 0118                };
 0119            }
 0120        }
 121    }
 122}

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpTelemetry.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Diagnostics.Tracing;
 7using System.Text;
 8using System.Threading;
 9
 10namespace System.Net.Http
 11{
 12    [EventSource(Name = "System.Net.Http")]
 13    internal sealed partial class HttpTelemetry : EventSource
 14    {
 015        public static readonly HttpTelemetry Log = new HttpTelemetry();
 16
 17        public static class Keywords
 18        {
 19            public const EventKeywords RequestFailedDetailed = (EventKeywords)1;
 20        }
 21
 22        private long _startedRequests;
 23        private long _stoppedRequests;
 24        private long _failedRequests;
 25
 26        private long _openedHttp11Connections;
 27        private long _openedHttp20Connections;
 28        private long _openedHttp30Connections;
 29
 30        // NOTE
 31        // - The 'Start' and 'Stop' suffixes on the following event names have special meaning in EventSource. They
 32        //   enable creating 'activities'.
 33        //   For more information, take a look at the following blog post:
 34        //   https://blogs.msdn.microsoft.com/vancem/2015/09/14/exploring-eventsource-activity-correlation-and-causation
 35        // - A stop event's event id must be next one after its start event.
 36
 37        [Event(1, Level = EventLevel.Informational)]
 38        private void RequestStart(string scheme, string host, int port, string pathAndQuery, byte versionMajor, byte ver
 039        {
 040            Interlocked.Increment(ref _startedRequests);
 41
 042            pathAndQuery = UriRedactionHelper.GetRedactedPathAndQuery(pathAndQuery);
 43
 044            WriteEvent(eventId: 1, scheme, host, port, pathAndQuery, versionMajor, versionMinor, versionPolicy);
 045        }
 46
 47        [NonEvent]
 48        public void RequestStart(HttpRequestMessage request)
 049        {
 050            Debug.Assert(request.RequestUri != null && request.RequestUri.IsAbsoluteUri);
 51
 052            RequestStart(
 053                request.RequestUri.Scheme,
 054                request.RequestUri.IdnHost,
 055                request.RequestUri.Port,
 056                request.RequestUri.PathAndQuery,
 057                (byte)request.Version.Major,
 058                (byte)request.Version.Minor,
 059                request.VersionPolicy);
 060        }
 61
 62        [NonEvent]
 63        public void RequestStop(HttpResponseMessage? response)
 064        {
 065            RequestStop(response is null ? -1 : (int)response.StatusCode);
 066        }
 67
 68        [Event(2, Level = EventLevel.Informational, Version = 1)]
 69        private void RequestStop(int statusCode)
 070        {
 071            Interlocked.Increment(ref _stoppedRequests);
 072            WriteEvent(eventId: 2, statusCode);
 073        }
 74
 75        [NonEvent]
 76        public void RequestFailed(Exception exception)
 077        {
 078            Interlocked.Increment(ref _failedRequests);
 79
 080            if (IsEnabled(EventLevel.Error, EventKeywords.None))
 081            {
 082                RequestFailed(exceptionMessage: exception.Message);
 83
 084                if (IsEnabled(EventLevel.Error, Keywords.RequestFailedDetailed))
 085                {
 086                    RequestFailedDetailed(exception: exception.ToString());
 087                }
 088            }
 089        }
 90
 91        [Event(3, Level = EventLevel.Error, Version = 1)]
 92        private void RequestFailed(string exceptionMessage)
 093        {
 094            WriteEvent(eventId: 3, exceptionMessage);
 095        }
 96
 97        [NonEvent]
 98        private void ConnectionEstablished(byte versionMajor, byte versionMinor, long connectionId, string scheme, strin
 099        {
 0100            string? remoteAddress = remoteEndPoint?.Address?.ToString();
 0101            ConnectionEstablished(versionMajor, versionMinor, connectionId, scheme, host, port, remoteAddress);
 0102        }
 103
 104        [Event(4, Level = EventLevel.Informational, Version = 1)]
 105        private void ConnectionEstablished(byte versionMajor, byte versionMinor, long connectionId, string scheme, strin
 0106        {
 0107            WriteEvent(eventId: 4, versionMajor, versionMinor, connectionId, scheme, host, port, remoteAddress);
 0108        }
 109
 110        [Event(5, Level = EventLevel.Informational, Version = 1)]
 111        private void ConnectionClosed(byte versionMajor, byte versionMinor, long connectionId)
 0112        {
 0113            WriteEvent(eventId: 5, versionMajor, versionMinor, connectionId);
 0114        }
 115
 116        [Event(6, Level = EventLevel.Informational)]
 117        private void RequestLeftQueue(double timeOnQueueMilliseconds, byte versionMajor, byte versionMinor)
 0118        {
 0119            WriteEvent(eventId: 6, timeOnQueueMilliseconds, versionMajor, versionMinor);
 0120        }
 121
 122        [Event(7, Level = EventLevel.Informational, Version = 1)]
 123        public void RequestHeadersStart(long connectionId)
 0124        {
 0125            WriteEvent(eventId: 7, connectionId);
 0126        }
 127
 128        [Event(8, Level = EventLevel.Informational)]
 129        public void RequestHeadersStop()
 0130        {
 0131            WriteEvent(eventId: 8);
 0132        }
 133
 134        [Event(9, Level = EventLevel.Informational)]
 135        public void RequestContentStart()
 0136        {
 0137            WriteEvent(eventId: 9);
 0138        }
 139
 140        [Event(10, Level = EventLevel.Informational)]
 141        public void RequestContentStop(long contentLength)
 0142        {
 0143            WriteEvent(eventId: 10, contentLength);
 0144        }
 145
 146        [Event(11, Level = EventLevel.Informational)]
 147        public void ResponseHeadersStart()
 0148        {
 0149            WriteEvent(eventId: 11);
 0150        }
 151
 152        [Event(12, Level = EventLevel.Informational, Version = 1)]
 153        public void ResponseHeadersStop(int statusCode)
 0154        {
 0155            WriteEvent(eventId: 12, statusCode);
 0156        }
 157
 158        [Event(13, Level = EventLevel.Informational)]
 159        public void ResponseContentStart()
 0160        {
 0161            WriteEvent(eventId: 13);
 0162        }
 163
 164        [Event(14, Level = EventLevel.Informational)]
 165        public void ResponseContentStop()
 0166        {
 0167            WriteEvent(eventId: 14);
 0168        }
 169
 170        [Event(15, Level = EventLevel.Error, Keywords = Keywords.RequestFailedDetailed)]
 171        private void RequestFailedDetailed(string exception)
 0172        {
 0173            WriteEvent(eventId: 15, exception);
 0174        }
 175
 176        [Event(16, Level = EventLevel.Informational)]
 177        private void Redirect(string redirectUri)
 0178        {
 0179            WriteEvent(eventId: 16, redirectUri);
 0180        }
 181
 182        [NonEvent]
 183        public void Redirect(Uri redirectUri)
 0184        {
 0185            Debug.Assert(redirectUri.IsAbsoluteUri);
 186
 0187            string uriString = UriRedactionHelper.GetRedactedUriString(redirectUri);
 188
 0189            Redirect(uriString);
 0190        }
 191
 192        [NonEvent]
 193        public void Http11ConnectionEstablished(long connectionId, string scheme, string host, int port, IPEndPoint? rem
 0194        {
 0195            Interlocked.Increment(ref _openedHttp11Connections);
 0196            ConnectionEstablished(versionMajor: 1, versionMinor: 1, connectionId, scheme, host, port, remoteEndPoint);
 0197        }
 198
 199        [NonEvent]
 200        public void Http11ConnectionClosed(long connectionId)
 0201        {
 0202            long count = Interlocked.Decrement(ref _openedHttp11Connections);
 0203            Debug.Assert(count >= 0);
 0204            ConnectionClosed(versionMajor: 1, versionMinor: 1, connectionId);
 0205        }
 206
 207        [NonEvent]
 208        public void Http20ConnectionEstablished(long connectionId, string scheme, string host, int port, IPEndPoint? rem
 0209        {
 0210            Interlocked.Increment(ref _openedHttp20Connections);
 0211            ConnectionEstablished(versionMajor: 2, versionMinor: 0, connectionId, scheme, host, port, remoteEndPoint);
 0212        }
 213
 214        [NonEvent]
 215        public void Http20ConnectionClosed(long connectionId)
 0216        {
 0217            long count = Interlocked.Decrement(ref _openedHttp20Connections);
 0218            Debug.Assert(count >= 0);
 0219            ConnectionClosed(versionMajor: 2, versionMinor: 0, connectionId);
 0220        }
 221
 222        [NonEvent]
 223        public void Http30ConnectionEstablished(long connectionId, string scheme, string host, int port, IPEndPoint? rem
 0224        {
 0225            Interlocked.Increment(ref _openedHttp30Connections);
 0226            ConnectionEstablished(versionMajor: 3, versionMinor: 0, connectionId, scheme, host, port, remoteEndPoint);
 0227        }
 228
 229        [NonEvent]
 230        public void Http30ConnectionClosed(long connectionId)
 0231        {
 0232            long count = Interlocked.Decrement(ref _openedHttp30Connections);
 0233            Debug.Assert(count >= 0);
 0234            ConnectionClosed(versionMajor: 3, versionMinor: 0, connectionId);
 0235        }
 236
 237        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
 238            Justification = "Parameters to this method are primitive and are trimmer safe")]
 239        [NonEvent]
 240        private unsafe void WriteEvent(int eventId, string? arg1, string? arg2, int arg3, string? arg4, byte arg5, byte 
 0241        {
 0242            arg1 ??= "";
 0243            arg2 ??= "";
 0244            arg4 ??= "";
 245
 0246            fixed (char* arg1Ptr = arg1)
 0247            fixed (char* arg2Ptr = arg2)
 0248            fixed (char* arg4Ptr = arg4)
 0249            {
 250                const int NumEventDatas = 7;
 0251                EventData* descrs = stackalloc EventData[NumEventDatas];
 252
 0253                descrs[0] = new EventData
 0254                {
 0255                    DataPointer = (IntPtr)(arg1Ptr),
 0256                    Size = (arg1.Length + 1) * sizeof(char)
 0257                };
 0258                descrs[1] = new EventData
 0259                {
 0260                    DataPointer = (IntPtr)(arg2Ptr),
 0261                    Size = (arg2.Length + 1) * sizeof(char)
 0262                };
 0263                descrs[2] = new EventData
 0264                {
 0265                    DataPointer = (IntPtr)(&arg3),
 0266                    Size = sizeof(int)
 0267                };
 0268                descrs[3] = new EventData
 0269                {
 0270                    DataPointer = (IntPtr)(arg4Ptr),
 0271                    Size = (arg4.Length + 1) * sizeof(char)
 0272                };
 0273                descrs[4] = new EventData
 0274                {
 0275                    DataPointer = (IntPtr)(&arg5),
 0276                    Size = sizeof(byte)
 0277                };
 0278                descrs[5] = new EventData
 0279                {
 0280                    DataPointer = (IntPtr)(&arg6),
 0281                    Size = sizeof(byte)
 0282                };
 0283                descrs[6] = new EventData
 0284                {
 0285                    DataPointer = (IntPtr)(&arg7),
 0286                    Size = sizeof(HttpVersionPolicy)
 0287                };
 288
 0289                WriteEventCore(eventId, NumEventDatas, descrs);
 0290            }
 0291        }
 292
 293        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
 294            Justification = "Parameters to this method are primitive and are trimmer safe")]
 295        [NonEvent]
 296        private unsafe void WriteEvent(int eventId, double arg1, byte arg2, byte arg3)
 0297        {
 298            const int NumEventDatas = 3;
 0299            EventData* descrs = stackalloc EventData[NumEventDatas];
 300
 0301            descrs[0] = new EventData
 0302            {
 0303                DataPointer = (IntPtr)(&arg1),
 0304                Size = sizeof(double)
 0305            };
 0306            descrs[1] = new EventData
 0307            {
 0308                DataPointer = (IntPtr)(&arg2),
 0309                Size = sizeof(byte)
 0310            };
 0311            descrs[2] = new EventData
 0312            {
 0313                DataPointer = (IntPtr)(&arg3),
 0314                Size = sizeof(byte)
 0315            };
 316
 0317            WriteEventCore(eventId, NumEventDatas, descrs);
 0318        }
 319
 320        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
 321            Justification = "Parameters to this method are primitive and are trimmer safe")]
 322        [NonEvent]
 323        private unsafe void WriteEvent(int eventId, byte arg1, byte arg2, long arg3)
 0324        {
 325            const int NumEventDatas = 3;
 0326            EventData* descrs = stackalloc EventData[NumEventDatas];
 327
 0328            descrs[0] = new EventData
 0329            {
 0330                DataPointer = (IntPtr)(&arg1),
 0331                Size = sizeof(byte)
 0332            };
 0333            descrs[1] = new EventData
 0334            {
 0335                DataPointer = (IntPtr)(&arg2),
 0336                Size = sizeof(byte)
 0337            };
 0338            descrs[2] = new EventData
 0339            {
 0340                DataPointer = (IntPtr)(&arg3),
 0341                Size = sizeof(long)
 0342            };
 343
 0344            WriteEventCore(eventId, NumEventDatas, descrs);
 0345        }
 346
 347        [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
 348           Justification = "Parameters to this method are primitive and are trimmer safe")]
 349        [NonEvent]
 350        private unsafe void WriteEvent(int eventId, byte arg1, byte arg2, long arg3, string? arg4, string arg5, int arg6
 0351        {
 0352            arg4 ??= "";
 0353            arg5 ??= "";
 0354            arg7 ??= "";
 355
 0356            fixed (char* arg4Ptr = arg4)
 0357            fixed (char* arg5Ptr = arg5)
 0358            fixed (char* arg7Ptr = arg7)
 0359            {
 360                const int NumEventDatas = 7;
 0361                EventData* descrs = stackalloc EventData[NumEventDatas];
 362
 0363                descrs[0] = new EventData
 0364                {
 0365                    DataPointer = (IntPtr)(&arg1),
 0366                    Size = sizeof(byte)
 0367                };
 0368                descrs[1] = new EventData
 0369                {
 0370                    DataPointer = (IntPtr)(&arg2),
 0371                    Size = sizeof(byte)
 0372                };
 0373                descrs[2] = new EventData
 0374                {
 0375                    DataPointer = (IntPtr)(&arg3),
 0376                    Size = sizeof(long)
 0377                };
 0378                descrs[3] = new EventData
 0379                {
 0380                    DataPointer = (IntPtr)arg4Ptr,
 0381                    Size = (arg4.Length + 1) * sizeof(char)
 0382                };
 0383                descrs[4] = new EventData
 0384                {
 0385                    DataPointer = (IntPtr)arg5Ptr,
 0386                    Size = (arg5.Length + 1) * sizeof(char)
 0387                };
 0388                descrs[5] = new EventData
 0389                {
 0390                    DataPointer = (IntPtr)(&arg6),
 0391                    Size = sizeof(int)
 0392                };
 0393                descrs[6] = new EventData
 0394                {
 0395                    DataPointer = (IntPtr)arg7Ptr,
 0396                    Size = (arg7.Length + 1) * sizeof(char)
 0397                };
 398
 0399                WriteEventCore(eventId, NumEventDatas, descrs);
 0400            }
 0401        }
 402    }
 403}

Methods/Properties

RequestLeftQueue(System.Int32,System.TimeSpan)
OnEventCommand(System.Diagnostics.Tracing.EventCommandEventArgs)
.cctor()
RequestStart(System.String,System.String,System.Int32,System.String,System.Byte,System.Byte,System.Net.Http.HttpVersionPolicy)
RequestStart(System.Net.Http.HttpRequestMessage)
RequestStop(System.Net.Http.HttpResponseMessage)
RequestStop(System.Int32)
RequestFailed(System.Exception)
RequestFailed(System.String)
ConnectionEstablished(System.Byte,System.Byte,System.Int64,System.String,System.String,System.Int32,System.Net.IPEndPoint)
ConnectionEstablished(System.Byte,System.Byte,System.Int64,System.String,System.String,System.Int32,System.String)
ConnectionClosed(System.Byte,System.Byte,System.Int64)
RequestLeftQueue(System.Double,System.Byte,System.Byte)
RequestHeadersStart(System.Int64)
RequestHeadersStop()
RequestContentStart()
RequestContentStop(System.Int64)
ResponseHeadersStart()
ResponseHeadersStop(System.Int32)
ResponseContentStart()
ResponseContentStop()
RequestFailedDetailed(System.String)
Redirect(System.String)
Redirect(System.Uri)
Http11ConnectionEstablished(System.Int64,System.String,System.String,System.Int32,System.Net.IPEndPoint)
Http11ConnectionClosed(System.Int64)
Http20ConnectionEstablished(System.Int64,System.String,System.String,System.Int32,System.Net.IPEndPoint)
Http20ConnectionClosed(System.Int64)
Http30ConnectionEstablished(System.Int64,System.String,System.String,System.Int32,System.Net.IPEndPoint)
Http30ConnectionClosed(System.Int64)
WriteEvent(System.Int32,System.String,System.String,System.Int32,System.String,System.Byte,System.Byte,System.Net.Http.HttpVersionPolicy)
WriteEvent(System.Int32,System.Double,System.Byte,System.Byte)
WriteEvent(System.Int32,System.Byte,System.Byte,System.Int64)
WriteEvent(System.Int32,System.Byte,System.Byte,System.Int64,System.String,System.String,System.Int32,System.String)