From 78fb028fe7595e9959aea43fffdcff1d2f48db6e Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Thu, 25 Aug 2022 20:51:37 -0700 Subject: [PATCH 1/4] Throw ObjectDisposedException if InMemoryExporter.Export is called after Dispose --- .../.publicApi/net462/PublicAPI.Unshipped.txt | 1 + .../netstandard2.0/PublicAPI.Unshipped.txt | 1 + .../CHANGELOG.md | 4 ++++ .../InMemoryExporter.cs | 23 ++++++++++++++++++- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt index e69de29bb2d..fb80e7c3a78 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/net462/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +override OpenTelemetry.Exporter.InMemoryExporter.Dispose(bool disposing) -> void diff --git a/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index e69de29bb2d..fb80e7c3a78 100644 --- a/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Exporter.InMemory/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +override OpenTelemetry.Exporter.InMemoryExporter.Dispose(bool disposing) -> void diff --git a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md index c96141b47de..d2ed3999f4a 100644 --- a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* Changed error handling, `InMemoryExporter` will now throw + `ObjectDisposedException` if `Export` is invoked after the exporter is + disposed. + ## 1.4.0-alpha.2 Released 2022-Aug-18 diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs index 9715119d6b2..38bd6dff2be 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs @@ -14,6 +14,7 @@ // limitations under the License. // +using System; using System.Collections.Generic; namespace OpenTelemetry.Exporter @@ -23,6 +24,7 @@ public class InMemoryExporter : BaseExporter { private readonly ICollection exportedItems; private readonly ExportFunc onExport; + private bool disposed = false; public InMemoryExporter(ICollection exportedItems) { @@ -37,7 +39,26 @@ internal InMemoryExporter(ExportFunc exportFunc) internal delegate ExportResult ExportFunc(in Batch batch); - public override ExportResult Export(in Batch batch) => this.onExport(batch); + public override ExportResult Export(in Batch batch) + { + if (this.disposed) + { + throw new ObjectDisposedException(this.GetType().Name); + } + + return this.onExport(batch); + } + + /// + protected override void Dispose(bool disposing) + { + if (!this.disposed) + { + this.disposed = true; + } + + base.Dispose(disposing); + } private ExportResult DefaultExport(in Batch batch) { From 9e597c51bf652ebf869d3fc6a4c9b93338fce737 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Thu, 25 Aug 2022 20:54:31 -0700 Subject: [PATCH 2/4] changelog --- src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md index d2ed3999f4a..e701088e4d2 100644 --- a/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.InMemory/CHANGELOG.md @@ -5,6 +5,7 @@ * 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 From fc72e1299df0daaff92903dce341882a31fcf786 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Fri, 26 Aug 2022 09:31:55 -0700 Subject: [PATCH 3/4] address comments --- src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs | 5 +++-- .../Internal/PrometheusExporter.cs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs index 38bd6dff2be..ca8ffb53485 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs @@ -24,7 +24,7 @@ public class InMemoryExporter : BaseExporter { private readonly ICollection exportedItems; private readonly ExportFunc onExport; - private bool disposed = false; + private bool disposed; public InMemoryExporter(ICollection exportedItems) { @@ -43,7 +43,8 @@ public override ExportResult Export(in Batch batch) { if (this.disposed) { - throw new ObjectDisposedException(this.GetType().Name); + // 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."); } return this.onExport(batch); diff --git a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporter.cs b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporter.cs index b0d5a5eb7db..6f04a4e2ee2 100644 --- a/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporter.cs +++ b/src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/PrometheusExporter.cs @@ -28,7 +28,7 @@ internal sealed class PrometheusExporter : BaseExporter, IPullMetricExpo { private Func funcCollect; private Func, ExportResult> funcExport; - private bool disposed = false; + private bool disposed; /// /// Initializes a new instance of the class. From 61dfb7ee685d67f7938fab456be0a4fe3ef07cb0 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Fri, 26 Aug 2022 10:21:55 -0700 Subject: [PATCH 4/4] remove wording --- src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs index ca8ffb53485..f69f9424953 100644 --- a/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs +++ b/src/OpenTelemetry.Exporter.InMemory/InMemoryExporter.cs @@ -43,8 +43,8 @@ public override ExportResult Export(in Batch 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."); + // 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."); } return this.onExport(batch);