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

Fix offline storage during network errors #38832

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal async Task<HttpMessage> InternalTrackAsync(IEnumerable<TelemetryItem> b
catch (Exception ex)
{
AzureMonitorExporterEventSource.Log.FailedToTransmit(ex);
if (ex.InnerException?.Source != "System.Net.Http")
if (ex.InnerException?.Source != "System.Net.Http" && ex.InnerException?.Source != "System")
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
{
message?.Dispose();
throw;
Expand Down Expand Up @@ -70,7 +70,7 @@ internal async Task<HttpMessage> InternalTrackAsync(ReadOnlyMemory<byte> body, C
catch (Exception ex)
{
AzureMonitorExporterEventSource.Log.FailedToTransmit(ex);
if (ex.InnerException?.Source != "System.Net.Http")
if (ex.InnerException?.Source != "System.Net.Http" && ex.InnerException?.Source != "System")
{
message?.Dispose();
throw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public async ValueTask<ExportResult> TrackAsync(IEnumerable<TelemetryItem> telem

if (result == ExportResult.Failure && _fileBlobProvider != null)
{
_transmissionStateManager.EnableBackOff(httpMessage.Response);
_transmissionStateManager.EnableBackOff(httpMessage.HasResponse ? httpMessage.Response : null);
result = HttpPipelineHelper.HandleFailures(httpMessage, _fileBlobProvider, _connectionVars, origin, _isAadEnabled);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal void ResetTransmission(object? source, System.Timers.ElapsedEventArgs e
CloseTransmission();
}

internal void EnableBackOff(Response response)
internal void EnableBackOff(Response? response)
{
if (Interlocked.Exchange(ref _syncBackOffIntervalCalculation, 1) == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal void TransmitFromStorage(object? sender, ElapsedEventArgs? e)
}
else
{
_transmissionStateManager.EnableBackOff(httpMessage.Response);
_transmissionStateManager.EnableBackOff(httpMessage.HasResponse ? httpMessage.Response : null);
HttpPipelineHelper.HandleFailures(httpMessage, blob, _blobProvider, _connectionVars, _isAadEnabled);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ public void FailureResponseCode429()
Assert.Single(transmitter._fileBlobProvider.GetBlobs());
}

[Fact]
public void NetworkFailure()
{
using var activity = CreateActivity("TestActivity");
var telemetryItem = CreateTelemetryItem(activity);
List<TelemetryItem> telemetryItems = new List<TelemetryItem>();
telemetryItems.Add(telemetryItem);

// Transmit
using var transmitter = GetTransmitter(null);
transmitter.TrackAsync(telemetryItems, TelemetryItemOrigin.UnitTest, false, CancellationToken.None).EnsureCompleted();

//Assert
Assert.NotNull(transmitter._fileBlobProvider);
Assert.Single(transmitter._fileBlobProvider.GetBlobs());
}

[Fact]
public void FailureResponseCode206()
{
Expand Down Expand Up @@ -260,17 +277,32 @@ public void TelemetryIsStoredOfflineWhenTransmissionStateIsSetToOpen()
transmitter.Dispose();
}

private static AzureMonitorTransmitter GetTransmitter(params MockResponse[] mockResponse)
private static AzureMonitorTransmitter GetTransmitter(params MockResponse[]? mockResponse)
{
MockTransport mockTransport = new MockTransport(mockResponse);
AzureMonitorExporterOptions options = new AzureMonitorExporterOptions
AzureMonitorTransmitter transmitter;
AzureMonitorExporterOptions options;
if (mockResponse == null)
{
ConnectionString = $"InstrumentationKey={testIkey};IngestionEndpoint={testEndpoint}",
StorageDirectory = "C:\\test",
Transport = mockTransport,
EnableStatsbeat = false, // disabled in tests.
};
AzureMonitorTransmitter transmitter = new AzureMonitorTransmitter(options, new MockPlatform());
options = new AzureMonitorExporterOptions
{
ConnectionString = $"InstrumentationKey={testIkey};IngestionEndpoint={testEndpoint}",
StorageDirectory = "C:\\test",
EnableStatsbeat = false, // disabled in tests.
};
}
else
{
MockTransport mockTransport = new MockTransport(mockResponse);
options = new AzureMonitorExporterOptions
{
ConnectionString = $"InstrumentationKey={testIkey};IngestionEndpoint={testEndpoint}",
StorageDirectory = "C:\\test",
Transport = mockTransport,
EnableStatsbeat = false, // disabled in tests.
};
}

transmitter = new AzureMonitorTransmitter(options, new MockPlatform());

// Overwrite storage with mock
transmitter._fileBlobProvider = new MockFileProvider();
Expand Down