| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Diagnostics.Tracing; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.Text; |
| | | 8 | | using Azure.Core; |
| | | 9 | | using Azure.Core.Diagnostics; |
| | | 10 | | |
| | | 11 | | namespace Azure.Identity |
| | | 12 | | { |
| | | 13 | | [EventSource(Name = EventSourceName)] |
| | | 14 | | internal sealed class AzureIdentityEventSource : EventSource |
| | | 15 | | { |
| | | 16 | | private const string EventSourceName = "Azure-Identity"; |
| | | 17 | | |
| | | 18 | | private const int GetTokenEvent = 1; |
| | | 19 | | private const int GetTokenSucceededEvent = 2; |
| | | 20 | | private const int GetTokenFailedEvent = 3; |
| | | 21 | | private const int ProbeImdsEndpointEvent = 4; |
| | | 22 | | private const int ImdsEndpointFoundEvent = 5; |
| | | 23 | | private const int ImdsEndpointUnavailableEvent = 6; |
| | | 24 | | |
| | 4 | 25 | | private AzureIdentityEventSource() : base(EventSourceName, EventSourceSettings.Default, AzureEventSourceListener |
| | | 26 | | |
| | 2786 | 27 | | public static AzureIdentityEventSource Singleton { get; } = new AzureIdentityEventSource(); |
| | | 28 | | |
| | | 29 | | [NonEvent] |
| | | 30 | | public void GetToken(string method, TokenRequestContext context) |
| | | 31 | | { |
| | 1346 | 32 | | if (IsEnabled(EventLevel.Informational, EventKeywords.All)) |
| | | 33 | | { |
| | 1286 | 34 | | GetToken(method, FormatStringArray(context.Scopes), context.ParentRequestId); |
| | | 35 | | } |
| | 1346 | 36 | | } |
| | | 37 | | |
| | | 38 | | [Event(GetTokenEvent, Level = EventLevel.Informational, Message = "{0} invoked. Scopes: {1} ParentRequestId: {2} |
| | | 39 | | public void GetToken(string method, string scopes, string parentRequestId) |
| | | 40 | | { |
| | 1286 | 41 | | WriteEvent(GetTokenEvent, method, scopes, parentRequestId); |
| | 1286 | 42 | | } |
| | | 43 | | |
| | | 44 | | [NonEvent] |
| | | 45 | | public void GetTokenSucceeded(string method, TokenRequestContext context, DateTimeOffset expiresOn) |
| | | 46 | | { |
| | 428 | 47 | | if (IsEnabled(EventLevel.Informational, EventKeywords.All)) |
| | | 48 | | { |
| | 412 | 49 | | GetTokenSucceeded(method, FormatStringArray(context.Scopes), context.ParentRequestId, expiresOn.ToString |
| | | 50 | | } |
| | 428 | 51 | | } |
| | | 52 | | |
| | | 53 | | [Event(GetTokenSucceededEvent, Level = EventLevel.Informational, Message = "{0} succeeded. Scopes: {1} ParentReq |
| | | 54 | | public void GetTokenSucceeded(string method, string scopes, string parentRequestId, string expiresOn) |
| | | 55 | | { |
| | 412 | 56 | | WriteEvent(GetTokenSucceededEvent, method, scopes, parentRequestId, expiresOn); |
| | 412 | 57 | | } |
| | | 58 | | |
| | | 59 | | [NonEvent] |
| | | 60 | | public void GetTokenFailed(string method, TokenRequestContext context, Exception ex) |
| | | 61 | | { |
| | 918 | 62 | | if (IsEnabled(EventLevel.Informational, EventKeywords.All)) |
| | | 63 | | { |
| | 874 | 64 | | GetTokenFailed(method, FormatStringArray(context.Scopes), context.ParentRequestId, FormatException(ex)); |
| | | 65 | | } |
| | 918 | 66 | | } |
| | | 67 | | |
| | | 68 | | [Event(GetTokenFailedEvent, Level = EventLevel.Informational, Message = "{0} was unable to retrieve an access to |
| | | 69 | | public void GetTokenFailed(string method, string scopes, string parentRequestId, string exception) |
| | | 70 | | { |
| | 874 | 71 | | WriteEvent(GetTokenFailedEvent, method, scopes, parentRequestId, exception); |
| | 874 | 72 | | } |
| | | 73 | | |
| | | 74 | | [NonEvent] |
| | | 75 | | public void ProbeImdsEndpoint(Uri uri) |
| | | 76 | | { |
| | 24 | 77 | | if (IsEnabled(EventLevel.Informational, EventKeywords.All)) |
| | | 78 | | { |
| | 24 | 79 | | ProbeImdsEndpoint(uri.ToString()); |
| | | 80 | | } |
| | 24 | 81 | | } |
| | | 82 | | |
| | | 83 | | [Event(ProbeImdsEndpointEvent, Level = EventLevel.Informational, Message = "Probing IMDS endpoint for availabili |
| | | 84 | | public void ProbeImdsEndpoint(string uri) |
| | | 85 | | { |
| | 24 | 86 | | WriteEvent(ProbeImdsEndpointEvent, uri); |
| | 24 | 87 | | } |
| | | 88 | | |
| | | 89 | | [NonEvent] |
| | | 90 | | public void ImdsEndpointFound(Uri uri) |
| | | 91 | | { |
| | 0 | 92 | | if (IsEnabled(EventLevel.Informational, EventKeywords.All)) |
| | | 93 | | { |
| | 0 | 94 | | ImdsEndpointFound(uri.ToString()); |
| | | 95 | | } |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | [Event(ImdsEndpointFoundEvent, Level = EventLevel.Informational, Message = "IMDS endpoint is available. Endpoint |
| | | 99 | | public void ImdsEndpointFound(string uri) |
| | | 100 | | { |
| | 0 | 101 | | WriteEvent(ImdsEndpointFoundEvent, uri); |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | [NonEvent] |
| | | 105 | | public void ImdsEndpointUnavailable(Uri uri) |
| | | 106 | | { |
| | 24 | 107 | | if (IsEnabled(EventLevel.Informational, EventKeywords.All)) |
| | | 108 | | { |
| | 24 | 109 | | ImdsEndpointUnavailable(uri.ToString()); |
| | | 110 | | } |
| | 24 | 111 | | } |
| | | 112 | | |
| | | 113 | | [Event(ImdsEndpointUnavailableEvent, Level = EventLevel.Informational, Message = "IMDS endpoint is did not respo |
| | | 114 | | public void ImdsEndpointUnavailable(string uri) |
| | | 115 | | { |
| | 24 | 116 | | WriteEvent(ImdsEndpointUnavailableEvent, uri); |
| | 24 | 117 | | } |
| | | 118 | | |
| | | 119 | | [NonEvent] |
| | | 120 | | private static string FormatException(Exception ex) |
| | | 121 | | { |
| | 874 | 122 | | StringBuilder sb = new StringBuilder(); |
| | 874 | 123 | | bool nest = false; |
| | | 124 | | do |
| | | 125 | | { |
| | 1094 | 126 | | if (nest) |
| | | 127 | | { |
| | | 128 | | // Format how Exception.ToString() would. |
| | 220 | 129 | | sb.AppendLine() |
| | 220 | 130 | | .Append(" ---> "); |
| | | 131 | | } |
| | | 132 | | // Do not include StackTrace, but do include HResult (often useful for CryptographicExceptions or IOExce |
| | 1094 | 133 | | sb.Append(ex.GetType().FullName) |
| | 1094 | 134 | | .Append(" (0x") |
| | 1094 | 135 | | .Append(ex.HResult.ToString("x", CultureInfo.InvariantCulture)) |
| | 1094 | 136 | | .Append("): ") |
| | 1094 | 137 | | .Append(ex.Message); |
| | 1094 | 138 | | ex = ex.InnerException; |
| | 1094 | 139 | | nest = true; |
| | | 140 | | } |
| | 1094 | 141 | | while (ex != null); |
| | 874 | 142 | | return sb.ToString(); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | [NonEvent] |
| | | 146 | | private static string FormatStringArray(string[] array) |
| | | 147 | | { |
| | 2572 | 148 | | return new StringBuilder("[ ").Append(string.Join(", ", array)).Append(" ]").ToString(); |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | } |