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

[InMemoryExporter] Throw ObjectDisposedException if Export happened after disposal #3607

Merged
merged 4 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1 @@
override OpenTelemetry.Exporter.InMemoryExporter<T>.Dispose(bool disposing) -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
override OpenTelemetry.Exporter.InMemoryExporter<T>.Dispose(bool disposing) -> void
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

* Changed error handling, `InMemoryExporter` will now throw
`ObjectDisposedException` if `Export` is invoked after the exporter is
disposed.
([#3607](https://github.com/open-telemetry/opentelemetry-dotnet/issues/3607))

## 1.4.0-alpha.2

Released 2022-Aug-18
Expand Down
24 changes: 23 additions & 1 deletion src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;

namespace OpenTelemetry.Exporter
Expand All @@ -23,6 +24,7 @@ public class InMemoryExporter<T> : BaseExporter<T>
{
private readonly ICollection<T> exportedItems;
private readonly ExportFunc onExport;
private bool disposed;

public InMemoryExporter(ICollection<T> exportedItems)
{
Expand All @@ -37,7 +39,27 @@ internal InMemoryExporter(ExportFunc exportFunc)

internal delegate ExportResult ExportFunc(in Batch<T> batch);

public override ExportResult Export(in Batch<T> batch) => this.onExport(batch);
public override ExportResult Export(in Batch<T> batch)
{
if (this.disposed)
{
// Since in-memory exporter is designed for testing purpose, having an early error would help developers to catch the bug during early stage of the development.
throw new ObjectDisposedException(this.GetType().Name, "The in-memory exporter is still being invoked after it is disposed. This indicates a wrong use of the OpenTelemetry .NET SDK, where the object lifecycle is not properly managed.");
reyang marked this conversation as resolved.
Show resolved Hide resolved
}

return this.onExport(batch);
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (!this.disposed)
{
this.disposed = true;
}

base.Dispose(disposing);
}

private ExportResult DefaultExport(in Batch<T> batch)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal sealed class PrometheusExporter : BaseExporter<Metric>, IPullMetricExpo
{
private Func<int, bool> funcCollect;
private Func<Batch<Metric>, ExportResult> funcExport;
private bool disposed = false;
private bool disposed;

/// <summary>
/// Initializes a new instance of the <see cref="PrometheusExporter"/> class.
Expand Down