Skip to content

Commit

Permalink
[InMemoryExporter] Include original stack trace in ObjectDisposedExce…
Browse files Browse the repository at this point in the history
…ption (#3609)

* InMemoryExporter dispose tweaks.

* Use Shutdown instead of ForceFlush.

* Fix another flaky test.

* Test fix.

* Test cleanup.
  • Loading branch information
CodeBlanch committed Aug 26, 2022
1 parent 372767a commit 28c0bcc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs
Expand Up @@ -25,6 +25,7 @@ public class InMemoryExporter<T> : BaseExporter<T>
private readonly ICollection<T> exportedItems;
private readonly ExportFunc onExport;
private bool disposed;
private string disposedStackTrace;

public InMemoryExporter(ICollection<T> exportedItems)
{
Expand All @@ -44,7 +45,9 @@ public override ExportResult Export(in Batch<T> batch)
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);
Expand All @@ -55,6 +58,7 @@ protected override void Dispose(bool disposing)
{
if (!this.disposed)
{
this.disposedStackTrace = Environment.StackTrace;
this.disposed = true;
}

Expand Down
Expand Up @@ -32,8 +32,9 @@ public void StateValuesAndScopeBufferingTest()

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);

Expand All @@ -44,7 +45,7 @@ public void StateValuesAndScopeBufferingTest()
logRecord.ScopeProvider = scopeProvider;
logRecord.StateValues = state;

exporter.OnEnd(logRecord);
processor.OnEnd(logRecord);

state.Dispose();

Expand All @@ -70,6 +71,10 @@ public void StateValuesAndScopeBufferingTest()
null);

Assert.True(foundScope);

processor.Shutdown();

Assert.Single(exportedItems);
}

[Fact]
Expand All @@ -81,15 +86,16 @@ public void StateBufferingTest()
// 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();

var state = new LogRecordTest.DisposingState("Hello world");
logRecord.State = state;

exporter.OnEnd(logRecord);
processor.OnEnd(logRecord);
processor.Shutdown();

state.Dispose();

Expand Down

0 comments on commit 28c0bcc

Please sign in to comment.