| | | 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.CodeAnalysis; |
| | | 6 | | using System.Diagnostics.Tracing; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Threading; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Http |
| | | 11 | | { |
| | | 12 | | [EventSource(Name = "System.Net.Http")] |
| | | 13 | | internal sealed partial class HttpTelemetry : EventSource |
| | | 14 | | { |
| | 0 | 15 | | 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 |
| | 0 | 39 | | { |
| | 0 | 40 | | Interlocked.Increment(ref _startedRequests); |
| | | 41 | | |
| | 0 | 42 | | pathAndQuery = UriRedactionHelper.GetRedactedPathAndQuery(pathAndQuery); |
| | | 43 | | |
| | 0 | 44 | | WriteEvent(eventId: 1, scheme, host, port, pathAndQuery, versionMajor, versionMinor, versionPolicy); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | [NonEvent] |
| | | 48 | | public void RequestStart(HttpRequestMessage request) |
| | 0 | 49 | | { |
| | 0 | 50 | | Debug.Assert(request.RequestUri != null && request.RequestUri.IsAbsoluteUri); |
| | | 51 | | |
| | 0 | 52 | | RequestStart( |
| | 0 | 53 | | request.RequestUri.Scheme, |
| | 0 | 54 | | request.RequestUri.IdnHost, |
| | 0 | 55 | | request.RequestUri.Port, |
| | 0 | 56 | | request.RequestUri.PathAndQuery, |
| | 0 | 57 | | (byte)request.Version.Major, |
| | 0 | 58 | | (byte)request.Version.Minor, |
| | 0 | 59 | | request.VersionPolicy); |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | [NonEvent] |
| | | 63 | | public void RequestStop(HttpResponseMessage? response) |
| | 0 | 64 | | { |
| | 0 | 65 | | RequestStop(response is null ? -1 : (int)response.StatusCode); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | [Event(2, Level = EventLevel.Informational, Version = 1)] |
| | | 69 | | private void RequestStop(int statusCode) |
| | 0 | 70 | | { |
| | 0 | 71 | | Interlocked.Increment(ref _stoppedRequests); |
| | 0 | 72 | | WriteEvent(eventId: 2, statusCode); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | [NonEvent] |
| | | 76 | | public void RequestFailed(Exception exception) |
| | 0 | 77 | | { |
| | 0 | 78 | | Interlocked.Increment(ref _failedRequests); |
| | | 79 | | |
| | 0 | 80 | | if (IsEnabled(EventLevel.Error, EventKeywords.None)) |
| | 0 | 81 | | { |
| | 0 | 82 | | RequestFailed(exceptionMessage: exception.Message); |
| | | 83 | | |
| | 0 | 84 | | if (IsEnabled(EventLevel.Error, Keywords.RequestFailedDetailed)) |
| | 0 | 85 | | { |
| | 0 | 86 | | RequestFailedDetailed(exception: exception.ToString()); |
| | 0 | 87 | | } |
| | 0 | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | [Event(3, Level = EventLevel.Error, Version = 1)] |
| | | 92 | | private void RequestFailed(string exceptionMessage) |
| | 0 | 93 | | { |
| | 0 | 94 | | WriteEvent(eventId: 3, exceptionMessage); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | [NonEvent] |
| | | 98 | | private void ConnectionEstablished(byte versionMajor, byte versionMinor, long connectionId, string scheme, strin |
| | 0 | 99 | | { |
| | 0 | 100 | | string? remoteAddress = remoteEndPoint?.Address?.ToString(); |
| | 0 | 101 | | ConnectionEstablished(versionMajor, versionMinor, connectionId, scheme, host, port, remoteAddress); |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | [Event(4, Level = EventLevel.Informational, Version = 1)] |
| | | 105 | | private void ConnectionEstablished(byte versionMajor, byte versionMinor, long connectionId, string scheme, strin |
| | 0 | 106 | | { |
| | 0 | 107 | | WriteEvent(eventId: 4, versionMajor, versionMinor, connectionId, scheme, host, port, remoteAddress); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | [Event(5, Level = EventLevel.Informational, Version = 1)] |
| | | 111 | | private void ConnectionClosed(byte versionMajor, byte versionMinor, long connectionId) |
| | 0 | 112 | | { |
| | 0 | 113 | | WriteEvent(eventId: 5, versionMajor, versionMinor, connectionId); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | [Event(6, Level = EventLevel.Informational)] |
| | | 117 | | private void RequestLeftQueue(double timeOnQueueMilliseconds, byte versionMajor, byte versionMinor) |
| | 0 | 118 | | { |
| | 0 | 119 | | WriteEvent(eventId: 6, timeOnQueueMilliseconds, versionMajor, versionMinor); |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | [Event(7, Level = EventLevel.Informational, Version = 1)] |
| | | 123 | | public void RequestHeadersStart(long connectionId) |
| | 0 | 124 | | { |
| | 0 | 125 | | WriteEvent(eventId: 7, connectionId); |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | [Event(8, Level = EventLevel.Informational)] |
| | | 129 | | public void RequestHeadersStop() |
| | 0 | 130 | | { |
| | 0 | 131 | | WriteEvent(eventId: 8); |
| | 0 | 132 | | } |
| | | 133 | | |
| | | 134 | | [Event(9, Level = EventLevel.Informational)] |
| | | 135 | | public void RequestContentStart() |
| | 0 | 136 | | { |
| | 0 | 137 | | WriteEvent(eventId: 9); |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | [Event(10, Level = EventLevel.Informational)] |
| | | 141 | | public void RequestContentStop(long contentLength) |
| | 0 | 142 | | { |
| | 0 | 143 | | WriteEvent(eventId: 10, contentLength); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | [Event(11, Level = EventLevel.Informational)] |
| | | 147 | | public void ResponseHeadersStart() |
| | 0 | 148 | | { |
| | 0 | 149 | | WriteEvent(eventId: 11); |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | [Event(12, Level = EventLevel.Informational, Version = 1)] |
| | | 153 | | public void ResponseHeadersStop(int statusCode) |
| | 0 | 154 | | { |
| | 0 | 155 | | WriteEvent(eventId: 12, statusCode); |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | [Event(13, Level = EventLevel.Informational)] |
| | | 159 | | public void ResponseContentStart() |
| | 0 | 160 | | { |
| | 0 | 161 | | WriteEvent(eventId: 13); |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | [Event(14, Level = EventLevel.Informational)] |
| | | 165 | | public void ResponseContentStop() |
| | 0 | 166 | | { |
| | 0 | 167 | | WriteEvent(eventId: 14); |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | [Event(15, Level = EventLevel.Error, Keywords = Keywords.RequestFailedDetailed)] |
| | | 171 | | private void RequestFailedDetailed(string exception) |
| | 0 | 172 | | { |
| | 0 | 173 | | WriteEvent(eventId: 15, exception); |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | [Event(16, Level = EventLevel.Informational)] |
| | | 177 | | private void Redirect(string redirectUri) |
| | 0 | 178 | | { |
| | 0 | 179 | | WriteEvent(eventId: 16, redirectUri); |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | [NonEvent] |
| | | 183 | | public void Redirect(Uri redirectUri) |
| | 0 | 184 | | { |
| | 0 | 185 | | Debug.Assert(redirectUri.IsAbsoluteUri); |
| | | 186 | | |
| | 0 | 187 | | string uriString = UriRedactionHelper.GetRedactedUriString(redirectUri); |
| | | 188 | | |
| | 0 | 189 | | Redirect(uriString); |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | [NonEvent] |
| | | 193 | | public void Http11ConnectionEstablished(long connectionId, string scheme, string host, int port, IPEndPoint? rem |
| | 0 | 194 | | { |
| | 0 | 195 | | Interlocked.Increment(ref _openedHttp11Connections); |
| | 0 | 196 | | ConnectionEstablished(versionMajor: 1, versionMinor: 1, connectionId, scheme, host, port, remoteEndPoint); |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | [NonEvent] |
| | | 200 | | public void Http11ConnectionClosed(long connectionId) |
| | 0 | 201 | | { |
| | 0 | 202 | | long count = Interlocked.Decrement(ref _openedHttp11Connections); |
| | 0 | 203 | | Debug.Assert(count >= 0); |
| | 0 | 204 | | ConnectionClosed(versionMajor: 1, versionMinor: 1, connectionId); |
| | 0 | 205 | | } |
| | | 206 | | |
| | | 207 | | [NonEvent] |
| | | 208 | | public void Http20ConnectionEstablished(long connectionId, string scheme, string host, int port, IPEndPoint? rem |
| | 0 | 209 | | { |
| | 0 | 210 | | Interlocked.Increment(ref _openedHttp20Connections); |
| | 0 | 211 | | ConnectionEstablished(versionMajor: 2, versionMinor: 0, connectionId, scheme, host, port, remoteEndPoint); |
| | 0 | 212 | | } |
| | | 213 | | |
| | | 214 | | [NonEvent] |
| | | 215 | | public void Http20ConnectionClosed(long connectionId) |
| | 0 | 216 | | { |
| | 0 | 217 | | long count = Interlocked.Decrement(ref _openedHttp20Connections); |
| | 0 | 218 | | Debug.Assert(count >= 0); |
| | 0 | 219 | | ConnectionClosed(versionMajor: 2, versionMinor: 0, connectionId); |
| | 0 | 220 | | } |
| | | 221 | | |
| | | 222 | | [NonEvent] |
| | | 223 | | public void Http30ConnectionEstablished(long connectionId, string scheme, string host, int port, IPEndPoint? rem |
| | 0 | 224 | | { |
| | 0 | 225 | | Interlocked.Increment(ref _openedHttp30Connections); |
| | 0 | 226 | | ConnectionEstablished(versionMajor: 3, versionMinor: 0, connectionId, scheme, host, port, remoteEndPoint); |
| | 0 | 227 | | } |
| | | 228 | | |
| | | 229 | | [NonEvent] |
| | | 230 | | public void Http30ConnectionClosed(long connectionId) |
| | 0 | 231 | | { |
| | 0 | 232 | | long count = Interlocked.Decrement(ref _openedHttp30Connections); |
| | 0 | 233 | | Debug.Assert(count >= 0); |
| | 0 | 234 | | ConnectionClosed(versionMajor: 3, versionMinor: 0, connectionId); |
| | 0 | 235 | | } |
| | | 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 |
| | 0 | 241 | | { |
| | 0 | 242 | | arg1 ??= ""; |
| | 0 | 243 | | arg2 ??= ""; |
| | 0 | 244 | | arg4 ??= ""; |
| | | 245 | | |
| | 0 | 246 | | fixed (char* arg1Ptr = arg1) |
| | 0 | 247 | | fixed (char* arg2Ptr = arg2) |
| | 0 | 248 | | fixed (char* arg4Ptr = arg4) |
| | 0 | 249 | | { |
| | | 250 | | const int NumEventDatas = 7; |
| | 0 | 251 | | EventData* descrs = stackalloc EventData[NumEventDatas]; |
| | | 252 | | |
| | 0 | 253 | | descrs[0] = new EventData |
| | 0 | 254 | | { |
| | 0 | 255 | | DataPointer = (IntPtr)(arg1Ptr), |
| | 0 | 256 | | Size = (arg1.Length + 1) * sizeof(char) |
| | 0 | 257 | | }; |
| | 0 | 258 | | descrs[1] = new EventData |
| | 0 | 259 | | { |
| | 0 | 260 | | DataPointer = (IntPtr)(arg2Ptr), |
| | 0 | 261 | | Size = (arg2.Length + 1) * sizeof(char) |
| | 0 | 262 | | }; |
| | 0 | 263 | | descrs[2] = new EventData |
| | 0 | 264 | | { |
| | 0 | 265 | | DataPointer = (IntPtr)(&arg3), |
| | 0 | 266 | | Size = sizeof(int) |
| | 0 | 267 | | }; |
| | 0 | 268 | | descrs[3] = new EventData |
| | 0 | 269 | | { |
| | 0 | 270 | | DataPointer = (IntPtr)(arg4Ptr), |
| | 0 | 271 | | Size = (arg4.Length + 1) * sizeof(char) |
| | 0 | 272 | | }; |
| | 0 | 273 | | descrs[4] = new EventData |
| | 0 | 274 | | { |
| | 0 | 275 | | DataPointer = (IntPtr)(&arg5), |
| | 0 | 276 | | Size = sizeof(byte) |
| | 0 | 277 | | }; |
| | 0 | 278 | | descrs[5] = new EventData |
| | 0 | 279 | | { |
| | 0 | 280 | | DataPointer = (IntPtr)(&arg6), |
| | 0 | 281 | | Size = sizeof(byte) |
| | 0 | 282 | | }; |
| | 0 | 283 | | descrs[6] = new EventData |
| | 0 | 284 | | { |
| | 0 | 285 | | DataPointer = (IntPtr)(&arg7), |
| | 0 | 286 | | Size = sizeof(HttpVersionPolicy) |
| | 0 | 287 | | }; |
| | | 288 | | |
| | 0 | 289 | | WriteEventCore(eventId, NumEventDatas, descrs); |
| | 0 | 290 | | } |
| | 0 | 291 | | } |
| | | 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) |
| | 0 | 297 | | { |
| | | 298 | | const int NumEventDatas = 3; |
| | 0 | 299 | | EventData* descrs = stackalloc EventData[NumEventDatas]; |
| | | 300 | | |
| | 0 | 301 | | descrs[0] = new EventData |
| | 0 | 302 | | { |
| | 0 | 303 | | DataPointer = (IntPtr)(&arg1), |
| | 0 | 304 | | Size = sizeof(double) |
| | 0 | 305 | | }; |
| | 0 | 306 | | descrs[1] = new EventData |
| | 0 | 307 | | { |
| | 0 | 308 | | DataPointer = (IntPtr)(&arg2), |
| | 0 | 309 | | Size = sizeof(byte) |
| | 0 | 310 | | }; |
| | 0 | 311 | | descrs[2] = new EventData |
| | 0 | 312 | | { |
| | 0 | 313 | | DataPointer = (IntPtr)(&arg3), |
| | 0 | 314 | | Size = sizeof(byte) |
| | 0 | 315 | | }; |
| | | 316 | | |
| | 0 | 317 | | WriteEventCore(eventId, NumEventDatas, descrs); |
| | 0 | 318 | | } |
| | | 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) |
| | 0 | 324 | | { |
| | | 325 | | const int NumEventDatas = 3; |
| | 0 | 326 | | EventData* descrs = stackalloc EventData[NumEventDatas]; |
| | | 327 | | |
| | 0 | 328 | | descrs[0] = new EventData |
| | 0 | 329 | | { |
| | 0 | 330 | | DataPointer = (IntPtr)(&arg1), |
| | 0 | 331 | | Size = sizeof(byte) |
| | 0 | 332 | | }; |
| | 0 | 333 | | descrs[1] = new EventData |
| | 0 | 334 | | { |
| | 0 | 335 | | DataPointer = (IntPtr)(&arg2), |
| | 0 | 336 | | Size = sizeof(byte) |
| | 0 | 337 | | }; |
| | 0 | 338 | | descrs[2] = new EventData |
| | 0 | 339 | | { |
| | 0 | 340 | | DataPointer = (IntPtr)(&arg3), |
| | 0 | 341 | | Size = sizeof(long) |
| | 0 | 342 | | }; |
| | | 343 | | |
| | 0 | 344 | | WriteEventCore(eventId, NumEventDatas, descrs); |
| | 0 | 345 | | } |
| | | 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 |
| | 0 | 351 | | { |
| | 0 | 352 | | arg4 ??= ""; |
| | 0 | 353 | | arg5 ??= ""; |
| | 0 | 354 | | arg7 ??= ""; |
| | | 355 | | |
| | 0 | 356 | | fixed (char* arg4Ptr = arg4) |
| | 0 | 357 | | fixed (char* arg5Ptr = arg5) |
| | 0 | 358 | | fixed (char* arg7Ptr = arg7) |
| | 0 | 359 | | { |
| | | 360 | | const int NumEventDatas = 7; |
| | 0 | 361 | | EventData* descrs = stackalloc EventData[NumEventDatas]; |
| | | 362 | | |
| | 0 | 363 | | descrs[0] = new EventData |
| | 0 | 364 | | { |
| | 0 | 365 | | DataPointer = (IntPtr)(&arg1), |
| | 0 | 366 | | Size = sizeof(byte) |
| | 0 | 367 | | }; |
| | 0 | 368 | | descrs[1] = new EventData |
| | 0 | 369 | | { |
| | 0 | 370 | | DataPointer = (IntPtr)(&arg2), |
| | 0 | 371 | | Size = sizeof(byte) |
| | 0 | 372 | | }; |
| | 0 | 373 | | descrs[2] = new EventData |
| | 0 | 374 | | { |
| | 0 | 375 | | DataPointer = (IntPtr)(&arg3), |
| | 0 | 376 | | Size = sizeof(long) |
| | 0 | 377 | | }; |
| | 0 | 378 | | descrs[3] = new EventData |
| | 0 | 379 | | { |
| | 0 | 380 | | DataPointer = (IntPtr)arg4Ptr, |
| | 0 | 381 | | Size = (arg4.Length + 1) * sizeof(char) |
| | 0 | 382 | | }; |
| | 0 | 383 | | descrs[4] = new EventData |
| | 0 | 384 | | { |
| | 0 | 385 | | DataPointer = (IntPtr)arg5Ptr, |
| | 0 | 386 | | Size = (arg5.Length + 1) * sizeof(char) |
| | 0 | 387 | | }; |
| | 0 | 388 | | descrs[5] = new EventData |
| | 0 | 389 | | { |
| | 0 | 390 | | DataPointer = (IntPtr)(&arg6), |
| | 0 | 391 | | Size = sizeof(int) |
| | 0 | 392 | | }; |
| | 0 | 393 | | descrs[6] = new EventData |
| | 0 | 394 | | { |
| | 0 | 395 | | DataPointer = (IntPtr)arg7Ptr, |
| | 0 | 396 | | Size = (arg7.Length + 1) * sizeof(char) |
| | 0 | 397 | | }; |
| | | 398 | | |
| | 0 | 399 | | WriteEventCore(eventId, NumEventDatas, descrs); |
| | 0 | 400 | | } |
| | 0 | 401 | | } |
| | | 402 | | } |
| | | 403 | | } |