Skip to content

Commit

Permalink
improve dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang committed Sep 17, 2021
1 parent fd06676 commit b780244
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace OpenTelemetry.Metrics
public class BaseExportingMetricReader : MetricReader
{
protected readonly BaseExporter<Metric> exporter;

private readonly ExportModes supportedExportModes = ExportModes.Push | ExportModes.Pull;
private bool disposed;

public BaseExportingMetricReader(BaseExporter<Metric> exporter)
{
Expand Down Expand Up @@ -61,6 +61,11 @@ internal override void SetParentProvider(BaseProvider parentProvider)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (this.disposed)
{
return;
}

if (disposing)
{
try
Expand All @@ -73,6 +78,8 @@ protected override void Dispose(bool disposing)
}
}

this.disposed = true;

base.Dispose(disposing);
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/OpenTelemetry/Metrics/MetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -61,12 +60,8 @@ public virtual void OnCollect(Batch<Metric> metrics)
/// <inheritdoc/>
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)
Expand Down
8 changes: 8 additions & 0 deletions src/OpenTelemetry/Metrics/PeriodicExportingMetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class PeriodicExportingMetricReader : BaseExportingMetricReader

private readonly Task exportTask;
private readonly CancellationTokenSource token;
private bool disposed;

public PeriodicExportingMetricReader(
BaseExporter<Metric> exporter,
Expand Down Expand Up @@ -62,6 +63,11 @@ public override void OnCollect(Batch<Metric> metrics)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (this.disposed)
{
return;
}

if (disposing)
{
try
Expand All @@ -76,6 +82,8 @@ protected override void Dispose(bool disposing)
}
}

this.disposed = true;

base.Dispose(disposing);
}
}
Expand Down

0 comments on commit b780244

Please sign in to comment.