From f52b0bac737546df8721d5233e12c4321b941d48 Mon Sep 17 00:00:00 2001 From: Timothy Mothra Lee Date: Fri, 21 Jul 2023 11:44:10 -0700 Subject: [PATCH] include AAD in the logger messages --- .../src/Internals/AzureMonitorTransmitter.cs | 15 ++++--- .../AzureMonitorExporterEventSource.cs | 39 ++++++++++--------- .../src/Internals/HttpPipelineHelper.cs | 6 ++- .../Internals/TransmitFromStorageHandler.cs | 11 ++++-- 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/AzureMonitorTransmitter.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/AzureMonitorTransmitter.cs index ac6b40c90f77..53f19d399e45 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/AzureMonitorTransmitter.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/AzureMonitorTransmitter.cs @@ -30,6 +30,7 @@ internal class AzureMonitorTransmitter : ITransmitter private readonly ConnectionVars _connectionVars; internal readonly TransmissionStateManager _transmissionStateManager; internal readonly TransmitFromStorageHandler? _transmitFromStorageHandler; + private readonly bool _isAadEnabled; private bool _disposed; public AzureMonitorTransmitter(AzureMonitorExporterOptions options, IPlatform platform) @@ -45,13 +46,13 @@ public AzureMonitorTransmitter(AzureMonitorExporterOptions options, IPlatform pl _transmissionStateManager = new TransmissionStateManager(); - _applicationInsightsRestClient = InitializeRestClient(options, _connectionVars); + _applicationInsightsRestClient = InitializeRestClient(options, _connectionVars, out _isAadEnabled); _fileBlobProvider = InitializeOfflineStorage(platform, _connectionVars, options.DisableOfflineStorage, options.StorageDirectory); if (_fileBlobProvider != null) { - _transmitFromStorageHandler = new TransmitFromStorageHandler(_applicationInsightsRestClient, _fileBlobProvider, _transmissionStateManager, _connectionVars); + _transmitFromStorageHandler = new TransmitFromStorageHandler(_applicationInsightsRestClient, _fileBlobProvider, _transmissionStateManager, _connectionVars, _isAadEnabled); } _statsbeat = InitializeStatsbeat(options, _connectionVars, platform); @@ -76,7 +77,7 @@ internal static ConnectionVars InitializeConnectionVars(AzureMonitorExporterOpti throw new InvalidOperationException("A connection string was not found. Please set your connection string."); } - private static ApplicationInsightsRestClient InitializeRestClient(AzureMonitorExporterOptions options, ConnectionVars connectionVars) + private static ApplicationInsightsRestClient InitializeRestClient(AzureMonitorExporterOptions options, ConnectionVars connectionVars, out bool isAadEnabled) { HttpPipeline pipeline; @@ -89,11 +90,13 @@ private static ApplicationInsightsRestClient InitializeRestClient(AzureMonitorEx new IngestionRedirectPolicy() }; + isAadEnabled = true; pipeline = HttpPipelineBuilder.Build(options, httpPipelinePolicy); AzureMonitorExporterEventSource.Log.SetAADCredentialsToPipeline(options.Credential.GetType().Name, scope); } else { + isAadEnabled = false; var httpPipelinePolicy = new HttpPipelinePolicy[] { new IngestionRedirectPolicy() }; pipeline = HttpPipelineBuilder.Build(options, httpPipelinePolicy); } @@ -179,13 +182,13 @@ await _applicationInsightsRestClient.InternalTrackAsync(telemetryItems, cancella if (result == ExportResult.Failure && _fileBlobProvider != null) { _transmissionStateManager.EnableBackOff(httpMessage.Response); - result = HttpPipelineHelper.HandleFailures(httpMessage, _fileBlobProvider, _connectionVars, origin); + result = HttpPipelineHelper.HandleFailures(httpMessage, _fileBlobProvider, _connectionVars, origin, _isAadEnabled); } else { _transmissionStateManager.ResetConsecutiveErrors(); _transmissionStateManager.CloseTransmission(); - AzureMonitorExporterEventSource.Log.TransmissionSuccess(origin, _connectionVars.InstrumentationKey); + AzureMonitorExporterEventSource.Log.TransmissionSuccess(origin, _isAadEnabled, _connectionVars.InstrumentationKey); } } else @@ -199,7 +202,7 @@ await _applicationInsightsRestClient.InternalTrackAsync(telemetryItems, cancella } catch (Exception ex) { - AzureMonitorExporterEventSource.Log.TransmitterFailed(origin, _connectionVars.InstrumentationKey, ex); + AzureMonitorExporterEventSource.Log.TransmitterFailed(origin, _isAadEnabled, _connectionVars.InstrumentationKey, ex); } return result; diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Diagnostics/AzureMonitorExporterEventSource.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Diagnostics/AzureMonitorExporterEventSource.cs index 66c104d94ab9..b39d04f88941 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Diagnostics/AzureMonitorExporterEventSource.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/Diagnostics/AzureMonitorExporterEventSource.cs @@ -67,7 +67,7 @@ public void FailedToTransmit(Exception ex) public void FailedToTransmit(string exceptionMessage) => WriteEvent(7, exceptionMessage); [NonEvent] - public void TransmissionFailed(int statusCode, TelemetryItemOrigin origin, bool willRetry, ConnectionVars connectionVars, string? requestEndpoint, Response? response) + public void TransmissionFailed(int statusCode, TelemetryItemOrigin origin, bool isAadEnabled, bool willRetry, ConnectionVars connectionVars, string? requestEndpoint, Response? response) { if (IsEnabled(EventLevel.Verbose)) { @@ -80,6 +80,7 @@ public void TransmissionFailed(int statusCode, TelemetryItemOrigin origin, bool errorMessage: "N/A", action: willRetry ? "Telemetry is stored offline for retry" : "Telemetry is dropped", origin: origin.ToString(), + isAadEnabled: isAadEnabled, instrumentationKey: connectionVars.InstrumentationKey, configuredEndpoint: connectionVars.IngestionEndpoint, actualEndpoint: requestEndpoint); @@ -93,6 +94,7 @@ public void TransmissionFailed(int statusCode, TelemetryItemOrigin origin, bool errorMessage: error ?? "N/A", action: willRetry ? "Telemetry is stored offline for retry" : "Telemetry is dropped", origin: origin.ToString(), + isAadEnabled: isAadEnabled, instrumentationKey: connectionVars.InstrumentationKey, configuredEndpoint: connectionVars.IngestionEndpoint, actualEndpoint: requestEndpoint); @@ -106,15 +108,16 @@ public void TransmissionFailed(int statusCode, TelemetryItemOrigin origin, bool errorMessage: "(To get exact error change LogLevel to Verbose)", action: willRetry ? "Telemetry is stored offline for retry" : "Telemetry is dropped", origin: origin.ToString(), + isAadEnabled: isAadEnabled, instrumentationKey: connectionVars.InstrumentationKey, configuredEndpoint: connectionVars.IngestionEndpoint, actualEndpoint: requestEndpoint); } } - [Event(8, Message = "Transmission failed. StatusCode: {0}. Error from Ingestion: {1}. Action: {2}. Origin: {3}. Instrumentation Key: {4}. Configured Endpoint: {5}. Actual Endpoint: {6}", Level = EventLevel.Critical)] - public void TransmissionFailed(int statusCode, string errorMessage, string action, string origin, string instrumentationKey, string configuredEndpoint, string? actualEndpoint) - => WriteEvent(8, statusCode, errorMessage, action, origin, instrumentationKey, configuredEndpoint, actualEndpoint); + [Event(8, Message = "Transmission failed. StatusCode: {0}. Error from Ingestion: {1}. Action: {2}. Origin: {3}. AAD Enabled: {4}. Instrumentation Key: {5}. Configured Endpoint: {6}. Actual Endpoint: {7}", Level = EventLevel.Critical)] + public void TransmissionFailed(int statusCode, string errorMessage, string action, string origin, bool isAadEnabled, string instrumentationKey, string configuredEndpoint, string? actualEndpoint) + => WriteEvent(8, statusCode, errorMessage, action, origin, isAadEnabled, instrumentationKey, configuredEndpoint, actualEndpoint); [Event(9, Message = "{0} has been disposed.", Level = EventLevel.Informational)] public void DisposedObject(string name) => WriteEvent(9, name); @@ -264,19 +267,19 @@ public void SdkVersionCreateFailed(Exception ex) public void SdkVersionCreateFailed(string exceptionMessage) => WriteEvent(24, exceptionMessage); [NonEvent] - public void FailedToTransmitFromStorage(string instrumentationKey, Exception ex) + public void FailedToTransmitFromStorage(bool isAadEnabled, string instrumentationKey, Exception ex) { if (IsEnabled(EventLevel.Error)) { - FailedToTransmitFromStorage(instrumentationKey, ex.FlattenException().ToInvariantString()); + FailedToTransmitFromStorage(isAadEnabled, instrumentationKey, ex.FlattenException().ToInvariantString()); } } - [Event(25, Message = "Failed to transmit from storage due to an exception. Instrumentation Key: {0}. {1}", Level = EventLevel.Error)] - public void FailedToTransmitFromStorage(string instrumentationKey, string exceptionMessage) => WriteEvent(25, instrumentationKey, exceptionMessage); + [Event(25, Message = "Failed to transmit from storage due to an exception. AAD Enabled: {0}. Instrumentation Key: {1}. {2}", Level = EventLevel.Error)] + public void FailedToTransmitFromStorage(bool isAadEnabled, string instrumentationKey, string exceptionMessage) => WriteEvent(25, isAadEnabled, instrumentationKey, exceptionMessage); - [Event(26, Message = "Successfully transmitted a blob from storage. Instrumentation Key: {0}", Level = EventLevel.Verbose)] - public void TransmitFromStorageSuccess(string instrumentationKey) => WriteEvent(26, instrumentationKey); + [Event(26, Message = "Successfully transmitted a blob from storage. AAD Enabled: {0}. Instrumentation Key: {1}", Level = EventLevel.Verbose)] + public void TransmitFromStorageSuccess(bool isAadEnabled, string instrumentationKey) => WriteEvent(26, isAadEnabled, instrumentationKey); [Event(27, Message = "HttpPipelineBuilder is built with AAD Credentials. TokenCredential: {0} Scope: {1}", Level = EventLevel.Informational)] public void SetAADCredentialsToPipeline(string credentialTypeName, string scope) => WriteEvent(27, credentialTypeName, scope); @@ -312,28 +315,28 @@ public void ErrorInitializingStatsbeat(ConnectionVars connectionVars, Exception public void ErrorInitializingStatsbeat(string instrumentationKey, string configuredEndpoint, string exceptionMessage) => WriteEvent(31, instrumentationKey, configuredEndpoint, exceptionMessage); [NonEvent] - public void TransmissionSuccess(TelemetryItemOrigin origin, string instrumentationKey) + public void TransmissionSuccess(TelemetryItemOrigin origin, bool isAadEnabled, string instrumentationKey) { if (IsEnabled(EventLevel.Verbose)) { - TransmissionSuccess(origin.ToString(), instrumentationKey); + TransmissionSuccess(origin.ToString(), isAadEnabled, instrumentationKey); } } - [Event(32, Message = "Successfully transmitted a batch of telemetry Items. Origin: {0}. Instrumentation Key: {1}", Level = EventLevel.Verbose)] - public void TransmissionSuccess(string origin, string instrumentationKey) => WriteEvent(32, origin, instrumentationKey); + [Event(32, Message = "Successfully transmitted a batch of telemetry Items. Origin: {0}. AAD: {1}. Instrumentation Key: {2}", Level = EventLevel.Verbose)] + public void TransmissionSuccess(string origin, bool isAadEnabled, string instrumentationKey) => WriteEvent(32, origin, isAadEnabled, instrumentationKey); [NonEvent] - public void TransmitterFailed(TelemetryItemOrigin origin, string instrumentationKey, Exception ex) + public void TransmitterFailed(TelemetryItemOrigin origin, bool isAadEnabled, string instrumentationKey, Exception ex) { if (IsEnabled(EventLevel.Error)) { - TransmitterFailed(origin.ToString(), instrumentationKey, ex.FlattenException().ToInvariantString()); + TransmitterFailed(origin.ToString(), isAadEnabled, instrumentationKey, ex.FlattenException().ToInvariantString()); } } - [Event(33, Message = "Transmitter failed due to an exception. Origin: {0}. Instrumentation Key: {1}. {2}", Level = EventLevel.Error)] - public void TransmitterFailed(string origin, string instrumentationKey, string exceptionMessage) => WriteEvent(33, origin, instrumentationKey, exceptionMessage); + [Event(33, Message = "Transmitter failed due to an exception. Origin: {0}. AAD: {1}. Instrumentation Key: {2}. {3}", Level = EventLevel.Error)] + public void TransmitterFailed(string origin, bool isAadEnabled, string instrumentationKey, string exceptionMessage) => WriteEvent(33, origin, isAadEnabled, instrumentationKey, exceptionMessage); [Event(34, Message = "Exporter encountered a transmission failure and will wait {0} milliseconds before transmitting again.", Level = EventLevel.Warning)] public void BackoffEnabled(double milliseconds) => WriteEvent(34, milliseconds); diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/HttpPipelineHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/HttpPipelineHelper.cs index 5f4358d26967..5ed9b3a1e559 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/HttpPipelineHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/HttpPipelineHelper.cs @@ -168,7 +168,7 @@ internal static ExportResult IsSuccess(HttpMessage httpMessage) return ExportResult.Failure; } - internal static ExportResult HandleFailures(HttpMessage httpMessage, PersistentBlobProvider blobProvider, ConnectionVars connectionVars, TelemetryItemOrigin origin) + internal static ExportResult HandleFailures(HttpMessage httpMessage, PersistentBlobProvider blobProvider, ConnectionVars connectionVars, TelemetryItemOrigin origin, bool isAadEnabled) { ExportResult result = ExportResult.Failure; int statusCode = 0; @@ -223,6 +223,7 @@ internal static ExportResult HandleFailures(HttpMessage httpMessage, PersistentB AzureMonitorExporterEventSource.Log.TransmissionFailed( origin: origin, statusCode: statusCode, + isAadEnabled: isAadEnabled, connectionVars: connectionVars, requestEndpoint: httpMessage.Request.Uri.Host, willRetry: (result == ExportResult.Success), @@ -231,7 +232,7 @@ internal static ExportResult HandleFailures(HttpMessage httpMessage, PersistentB return result; } - internal static void HandleFailures(HttpMessage httpMessage, PersistentBlob blob, PersistentBlobProvider blobProvider, ConnectionVars connectionVars) + internal static void HandleFailures(HttpMessage httpMessage, PersistentBlob blob, PersistentBlobProvider blobProvider, ConnectionVars connectionVars, bool isAadEnabled) { int statusCode = 0; bool willRetry = true; @@ -273,6 +274,7 @@ internal static void HandleFailures(HttpMessage httpMessage, PersistentBlob blob AzureMonitorExporterEventSource.Log.TransmissionFailed( origin: TelemetryItemOrigin.Storage, + isAadEnabled: isAadEnabled, statusCode: statusCode, connectionVars: connectionVars, requestEndpoint: httpMessage.Request.Uri.Host, diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TransmitFromStorageHandler.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TransmitFromStorageHandler.cs index 15776e472032..fee2453bcecc 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TransmitFromStorageHandler.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TransmitFromStorageHandler.cs @@ -18,12 +18,14 @@ internal class TransmitFromStorageHandler : IDisposable internal PersistentBlobProvider _blobProvider; private readonly TransmissionStateManager _transmissionStateManager; private readonly System.Timers.Timer _transmitFromStorageTimer; + private readonly bool _isAadEnabled; private bool _disposed; - internal TransmitFromStorageHandler(ApplicationInsightsRestClient applicationInsightsRestClient, PersistentBlobProvider blobProvider, TransmissionStateManager transmissionStateManager, ConnectionVars connectionVars) + internal TransmitFromStorageHandler(ApplicationInsightsRestClient applicationInsightsRestClient, PersistentBlobProvider blobProvider, TransmissionStateManager transmissionStateManager, ConnectionVars connectionVars, bool isAadEnabled) { _applicationInsightsRestClient = applicationInsightsRestClient; _connectionVars = connectionVars; + _isAadEnabled = isAadEnabled; _blobProvider = blobProvider; _transmissionStateManager = transmissionStateManager; _transmitFromStorageTimer = new System.Timers.Timer(); @@ -31,6 +33,7 @@ internal TransmitFromStorageHandler(ApplicationInsightsRestClient applicationIns _transmitFromStorageTimer.AutoReset = true; _transmitFromStorageTimer.Interval = 120000; _transmitFromStorageTimer.Start(); + _isAadEnabled = isAadEnabled; } internal void TransmitFromStorage(object? sender, ElapsedEventArgs? e) @@ -54,7 +57,7 @@ internal void TransmitFromStorage(object? sender, ElapsedEventArgs? e) _transmissionStateManager.ResetConsecutiveErrors(); _transmissionStateManager.CloseTransmission(); - AzureMonitorExporterEventSource.Log.TransmitFromStorageSuccess(_connectionVars.InstrumentationKey); + AzureMonitorExporterEventSource.Log.TransmitFromStorageSuccess(_isAadEnabled, _connectionVars.InstrumentationKey); // In case if the delete fails, there is a possibility // that the current batch will be transmitted more than once resulting in duplicates. @@ -64,13 +67,13 @@ internal void TransmitFromStorage(object? sender, ElapsedEventArgs? e) else { _transmissionStateManager.EnableBackOff(httpMessage.Response); - HttpPipelineHelper.HandleFailures(httpMessage, blob, _blobProvider, _connectionVars); + HttpPipelineHelper.HandleFailures(httpMessage, blob, _blobProvider, _connectionVars, _isAadEnabled); break; } } catch (Exception ex) { - AzureMonitorExporterEventSource.Log.FailedToTransmitFromStorage(_connectionVars.InstrumentationKey, ex); + AzureMonitorExporterEventSource.Log.FailedToTransmitFromStorage(_isAadEnabled, _connectionVars.InstrumentationKey, ex); } } else