| | | 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.Threading; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http |
| | | 9 | | { |
| | | 10 | | // Implements distributed tracing logic for managing the "HTTP connection_setup" and "HTTP wait_for_connection" Acti |
| | | 11 | | internal static class ConnectionSetupDistributedTracing |
| | | 12 | | { |
| | 0 | 13 | | private static readonly ActivitySource s_connectionsActivitySource = new ActivitySource(DiagnosticsHandlerLoggin |
| | | 14 | | |
| | | 15 | | public static Activity? StartConnectionSetupActivity(bool isSecure, string? serverAddress, int port) |
| | 0 | 16 | | { |
| | 0 | 17 | | Activity? activity = null; |
| | 0 | 18 | | if (s_connectionsActivitySource.HasListeners()) |
| | 0 | 19 | | { |
| | | 20 | | // Connection activities should be new roots and not parented under whatever |
| | | 21 | | // request happens to be in progress when the connection is started. |
| | 0 | 22 | | Activity.Current = null; |
| | 0 | 23 | | activity = s_connectionsActivitySource.StartActivity(DiagnosticsHandlerLoggingStrings.ConnectionSetupAct |
| | 0 | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | if (activity is not null) |
| | 0 | 27 | | { |
| | 0 | 28 | | Debug.Assert(serverAddress is not null, "serverAddress should not be null when System.Net.Http.EnableAct |
| | 0 | 29 | | activity.DisplayName = $"HTTP connection_setup {serverAddress}:{port}"; |
| | 0 | 30 | | if (activity.IsAllDataRequested) |
| | 0 | 31 | | { |
| | 0 | 32 | | activity.SetTag("server.address", serverAddress); |
| | 0 | 33 | | activity.SetTag("server.port", port); |
| | 0 | 34 | | activity.SetTag("url.scheme", isSecure ? "https" : "http"); |
| | 0 | 35 | | } |
| | 0 | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | return activity; |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | public static void StopConnectionSetupActivity(Activity activity, Exception? exception, IPEndPoint? remoteEndPoi |
| | 0 | 42 | | { |
| | 0 | 43 | | Debug.Assert(activity is not null); |
| | 0 | 44 | | if (exception is not null) |
| | 0 | 45 | | { |
| | 0 | 46 | | ReportError(activity, exception); |
| | 0 | 47 | | } |
| | | 48 | | else |
| | 0 | 49 | | { |
| | 0 | 50 | | if (activity.IsAllDataRequested && remoteEndPoint is not null) |
| | 0 | 51 | | { |
| | 0 | 52 | | activity.SetTag("network.peer.address", remoteEndPoint.Address.ToString()); |
| | 0 | 53 | | } |
| | 0 | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | activity.Stop(); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | public static void ReportError(Activity? activity, Exception exception) |
| | 0 | 60 | | { |
| | 0 | 61 | | Debug.Assert(exception is not null); |
| | 0 | 62 | | if (activity is null) return; |
| | 0 | 63 | | activity.SetStatus(ActivityStatusCode.Error); |
| | | 64 | | |
| | 0 | 65 | | if (activity.IsAllDataRequested) |
| | 0 | 66 | | { |
| | 0 | 67 | | DiagnosticsHelper.TryGetErrorType(null, exception, out string? errorType); |
| | 0 | 68 | | Debug.Assert(errorType is not null, "DiagnosticsHelper.TryGetErrorType() should succeed whenever an exce |
| | 0 | 69 | | activity.SetTag("error.type", errorType); |
| | 0 | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | public static Activity? StartWaitForConnectionActivity(HttpAuthority authority) |
| | 0 | 74 | | { |
| | 0 | 75 | | Activity? activity = s_connectionsActivitySource.StartActivity(DiagnosticsHandlerLoggingStrings.WaitForConne |
| | 0 | 76 | | activity?.DisplayName = $"HTTP wait_for_connection {authority.HostValue}:{authority.Port}"; |
| | | 77 | | |
| | 0 | 78 | | return activity; |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public static void AddConnectionLinkToRequestActivity(Activity connectionSetupActivity) |
| | 0 | 82 | | { |
| | 0 | 83 | | Debug.Assert(connectionSetupActivity is not null); |
| | | 84 | | |
| | | 85 | | // We only support links for request activities created by the "System.Net.Http" ActivitySource. |
| | 0 | 86 | | if (GlobalHttpSettings.DiagnosticsHandler.EnableActivityPropagation && DiagnosticsHandler.s_activitySource.H |
| | 0 | 87 | | { |
| | 0 | 88 | | Activity? requestActivity = Activity.Current; |
| | 0 | 89 | | if (requestActivity?.Source == DiagnosticsHandler.s_activitySource) |
| | 0 | 90 | | { |
| | 0 | 91 | | requestActivity.AddLink(new ActivityLink(connectionSetupActivity.Context)); |
| | 0 | 92 | | } |
| | 0 | 93 | | } |
| | 0 | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |