Skip to content

Commit

Permalink
Fix offline storage during network errors (#38832)
Browse files Browse the repository at this point in the history
* Fix offline storage during network errors

* fix net462

* changelog
  • Loading branch information
vishweshbankwar committed Sep 19, 2023
1 parent 108a37e commit 9864850
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
4 changes: 4 additions & 0 deletions sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

### Bugs Fixed

* Fixed an issue during network failures which prevented the exporter to store
the telemetry offline for retrying at a later time.
([#38832](https://github.com/Azure/azure-sdk-for-net/pull/38832))

### Other Changes

* Update OpenTelemetry dependencies
Expand Down
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")
{
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

0 comments on commit 9864850

Please sign in to comment.