diff --git a/src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs b/src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs index a5238d22fc6..490d443bbe6 100644 --- a/src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs +++ b/src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs @@ -21,8 +21,8 @@ namespace OpenTelemetry.Metrics public class BaseExportingMetricReader : MetricReader { protected readonly BaseExporter exporter; - private readonly ExportModes supportedExportModes = ExportModes.Push | ExportModes.Pull; + private bool disposed; public BaseExportingMetricReader(BaseExporter exporter) { @@ -61,6 +61,11 @@ internal override void SetParentProvider(BaseProvider parentProvider) /// protected override void Dispose(bool disposing) { + if (this.disposed) + { + return; + } + if (disposing) { try @@ -73,6 +78,8 @@ protected override void Dispose(bool disposing) } } + this.disposed = true; + base.Dispose(disposing); } } diff --git a/src/OpenTelemetry/Metrics/MetricReader.cs b/src/OpenTelemetry/Metrics/MetricReader.cs index e734c2692c5..c9fb1487d4b 100644 --- a/src/OpenTelemetry/Metrics/MetricReader.cs +++ b/src/OpenTelemetry/Metrics/MetricReader.cs @@ -23,7 +23,6 @@ public abstract class MetricReader : IDisposable private const AggregationTemporality CumulativeAndDelta = AggregationTemporality.Cumulative | AggregationTemporality.Delta; private AggregationTemporality preferredAggregationTemporality = CumulativeAndDelta; private AggregationTemporality supportedAggregationTemporality = CumulativeAndDelta; - private bool disposed; public BaseProvider ParentProvider { get; private set; } @@ -61,12 +60,8 @@ public virtual void OnCollect(Batch metrics) /// public void Dispose() { - if (!this.disposed) - { - this.Dispose(true); - GC.SuppressFinalize(this); - this.disposed = true; - } + this.Dispose(true); + GC.SuppressFinalize(this); } internal virtual void SetParentProvider(BaseProvider parentProvider) diff --git a/src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs b/src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs index 0f02ed1c983..cf43a335fd7 100644 --- a/src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs +++ b/src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs @@ -27,6 +27,7 @@ public class PeriodicExportingMetricReader : BaseExportingMetricReader private readonly Task exportTask; private readonly CancellationTokenSource token; + private bool disposed; public PeriodicExportingMetricReader( BaseExporter exporter, @@ -62,6 +63,11 @@ public override void OnCollect(Batch metrics) /// protected override void Dispose(bool disposing) { + if (this.disposed) + { + return; + } + if (disposing) { try @@ -76,6 +82,8 @@ protected override void Dispose(bool disposing) } } + this.disposed = true; + base.Dispose(disposing); } }