[InMemoryExporter] Include original stack trace in ObjectDisposedException (#3609)

* InMemoryExporter dispose tweaks.

* Use Shutdown instead of ForceFlush.

* Fix another flaky test.

* Test fix.

* Test cleanup.
This commit is contained in:
Mikel Blanchard 2022-08-26 13:46:33 -07:00 committed by GitHub
parent 372767ac6e
commit 28c0bccca8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -25,6 +25,7 @@ namespace OpenTelemetry.Exporter
private readonly ICollection<T> exportedItems;
private readonly ExportFunc onExport;
private bool disposed;
private string disposedStackTrace;
public InMemoryExporter(ICollection<T> exportedItems)
{
@ -44,7 +45,9 @@ namespace OpenTelemetry.Exporter
if (this.disposed)
{
// Note: In-memory exporter is designed for testing purposes so this error is strategic to surface lifecycle management bugs during development.
throw new ObjectDisposedException(this.GetType().Name, "The in-memory exporter is still being invoked after it has been disposed. This could be the result of invalid lifecycle management of the OpenTelemetry .NET SDK.");
throw new ObjectDisposedException(
this.GetType().Name,
$"The in-memory exporter is still being invoked after it has been disposed. This could be the result of invalid lifecycle management of the OpenTelemetry .NET SDK. Dispose was called on the following stack trace:{Environment.NewLine}{this.disposedStackTrace}");
}
return this.onExport(batch);
@ -55,6 +58,7 @@ namespace OpenTelemetry.Exporter
{
if (!this.disposed)
{
this.disposedStackTrace = Environment.StackTrace;
this.disposed = true;
}

View File

@ -32,8 +32,9 @@ namespace OpenTelemetry.Logs.Tests
List<LogRecord> exportedItems = new();
using var exporter = new BatchLogRecordExportProcessor(
new InMemoryExporter<LogRecord>(exportedItems));
using var processor = new BatchLogRecordExportProcessor(
new InMemoryExporter<LogRecord>(exportedItems),
scheduledDelayMilliseconds: int.MaxValue);
using var scope = scopeProvider.Push(exportedItems);
@ -44,7 +45,7 @@ namespace OpenTelemetry.Logs.Tests
logRecord.ScopeProvider = scopeProvider;
logRecord.StateValues = state;
exporter.OnEnd(logRecord);
processor.OnEnd(logRecord);
state.Dispose();
@ -70,6 +71,10 @@ namespace OpenTelemetry.Logs.Tests
null);
Assert.True(foundScope);
processor.Shutdown();
Assert.Single(exportedItems);
}
[Fact]
@ -81,7 +86,7 @@ namespace OpenTelemetry.Logs.Tests
// StateValues/ParseStateValues behavior.
List<LogRecord> exportedItems = new();
using var exporter = new BatchLogRecordExportProcessor(
using var processor = new BatchLogRecordExportProcessor(
new InMemoryExporter<LogRecord>(exportedItems));
var logRecord = new LogRecord();
@ -89,7 +94,8 @@ namespace OpenTelemetry.Logs.Tests
var state = new LogRecordTest.DisposingState("Hello world");
logRecord.State = state;
exporter.OnEnd(logRecord);
processor.OnEnd(logRecord);
processor.Shutdown();
state.Dispose();