Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AzureMonitorExporter] include AAD in the logger messages #37782

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ 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();
_transmitFromStorageTimer.Elapsed += TransmitFromStorage;
_transmitFromStorageTimer.AutoReset = true;
_transmitFromStorageTimer.Interval = 120000;
_transmitFromStorageTimer.Start();
_isAadEnabled = isAadEnabled;
}

internal void TransmitFromStorage(object? sender, ElapsedEventArgs? e)
Expand All @@ -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.
Expand All @@ -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
Expand Down