Skip to content

Commit

Permalink
Merge branch 'main' into metricreader-tcs
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Oct 25, 2021
2 parents e43e5f6 + 68a1761 commit 2d159b4
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 93 deletions.
16 changes: 10 additions & 6 deletions src/OpenTelemetry.Api/Context/ThreadLocalRuntimeContextSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace OpenTelemetry.Context
public class ThreadLocalRuntimeContextSlot<T> : RuntimeContextSlot<T>, IRuntimeContextSlotValueAccessor
{
private readonly ThreadLocal<T> slot;
private bool disposedValue;
private bool disposed;

/// <summary>
/// Initializes a new instance of the <see cref="ThreadLocalRuntimeContextSlot{T}"/> class.
Expand Down Expand Up @@ -62,13 +62,17 @@ public override void Set(T value)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (disposing && !this.disposedValue)
if (!this.disposed)
{
this.slot.Dispose();
this.disposedValue = true;
if (disposing)
{
this.slot.Dispose();
}

this.disposed = true;
}

base.Dispose(disposing);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal sealed class JaegerThriftClientTransport : TTransport
{
private readonly IJaegerClient client;
private readonly MemoryStream byteStream;
private bool isDisposed;
private bool disposed;

public JaegerThriftClientTransport(string host, int port)
: this(host, port, new MemoryStream(), new JaegerUdpClient())
Expand Down Expand Up @@ -88,13 +88,18 @@ public override string ToString()

protected override void Dispose(bool disposing)
{
if (!this.isDisposed && disposing)
if (this.disposed)
{
return;
}

if (disposing)
{
this.byteStream?.Dispose();
this.client?.Dispose();
}

this.isDisposed = true;
this.disposed = true;
}
}
}
6 changes: 3 additions & 3 deletions src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class JaegerExporter : BaseExporter<Activity>
private readonly InMemoryTransport memoryTransport;
private readonly TProtocol memoryProtocol;
private int batchByteSize;
private bool disposedValue; // To detect redundant dispose calls
private bool disposed;

public JaegerExporter(JaegerExporterOptions options)
: this(options, null)
Expand Down Expand Up @@ -169,7 +169,7 @@ internal void AppendSpan(JaegerSpan jaegerSpan)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (!this.disposedValue)
if (!this.disposed)
{
if (disposing)
{
Expand All @@ -179,7 +179,7 @@ protected override void Dispose(bool disposing)
this.memoryProtocol.Dispose();
}

this.disposedValue = true;
this.disposed = true;
}

base.Dispose(disposing);
Expand Down
21 changes: 12 additions & 9 deletions src/OpenTelemetry/BaseExportProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,24 @@ protected override bool OnShutdown(int timeoutMilliseconds)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (disposing && !this.disposed)
if (!this.disposed)
{
try
{
this.exporter.Dispose();
}
catch (Exception ex)
if (disposing)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex);
try
{
this.exporter.Dispose();
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex);
}
}

this.disposed = true;
}

base.Dispose(disposing);
}
}
}
48 changes: 44 additions & 4 deletions src/OpenTelemetry/BatchExportProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public abstract class BatchExportProcessor<T> : BaseExportProcessor<T>
private readonly ManualResetEvent shutdownTrigger = new ManualResetEvent(false);
private long shutdownDrainTarget = long.MaxValue;
private long droppedCount;
private bool disposed;

/// <summary>
/// Initializes a new instance of the <see cref="BatchExportProcessor{T}"/> class.
Expand Down Expand Up @@ -139,7 +140,14 @@ protected override bool OnForceFlush(int timeoutMilliseconds)
{
if (timeoutMilliseconds == Timeout.Infinite)
{
WaitHandle.WaitAny(triggers, pollingMilliseconds);
try
{
WaitHandle.WaitAny(triggers, pollingMilliseconds);
}
catch (ObjectDisposedException)
{
return false;
}
}
else
{
Expand All @@ -150,7 +158,14 @@ protected override bool OnForceFlush(int timeoutMilliseconds)
return this.circularBuffer.RemovedCount >= head;
}

WaitHandle.WaitAny(triggers, Math.Min((int)timeout, pollingMilliseconds));
try
{
WaitHandle.WaitAny(triggers, Math.Min((int)timeout, pollingMilliseconds));
}
catch (ObjectDisposedException)
{
return false;
}
}

if (this.circularBuffer.RemovedCount >= head)
Expand Down Expand Up @@ -190,6 +205,24 @@ protected override bool OnShutdown(int timeoutMilliseconds)
return this.exporter.Shutdown((int)Math.Max(timeout, 0));
}

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

this.disposed = true;
}

base.Dispose(disposing);
}

private void ExporterProc()
{
var triggers = new WaitHandle[] { this.exportTrigger, this.shutdownTrigger };
Expand All @@ -199,7 +232,14 @@ private void ExporterProc()
// only wait when the queue doesn't have enough items, otherwise keep busy and send data continuously
if (this.circularBuffer.Count < this.maxExportBatchSize)
{
WaitHandle.WaitAny(triggers, this.scheduledDelayMilliseconds);
try
{
WaitHandle.WaitAny(triggers, this.scheduledDelayMilliseconds);
}
catch (ObjectDisposedException)
{
return;
}
}

if (this.circularBuffer.Count > 0)
Expand All @@ -215,7 +255,7 @@ private void ExporterProc()

if (this.circularBuffer.RemovedCount >= this.shutdownDrainTarget)
{
break;
return;
}
}
}
Expand Down
37 changes: 19 additions & 18 deletions src/OpenTelemetry/CompositeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,33 +139,34 @@ protected override bool OnShutdown(int timeoutMilliseconds)
return result;
}

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

if (disposing)
{
var cur = this.head;

while (cur != null)
if (disposing)
{
try
{
cur.Value?.Dispose();
}
catch (Exception ex)
var cur = this.head;

while (cur != null)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex);
try
{
cur.Value?.Dispose();
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex);
}

cur = cur.Next;
}

cur = cur.Next;
}

this.disposed = true;
}

this.disposed = true;
base.Dispose(disposing);
}

private class DoublyLinkedListNode
Expand Down
18 changes: 9 additions & 9 deletions src/OpenTelemetry/Logs/OpenTelemetryLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,19 @@ internal OpenTelemetryLoggerProvider AddProcessor(BaseProcessor<LogRecord> proce

protected override void Dispose(bool disposing)
{
if (this.disposed)
if (!this.disposed)
{
return;
}
if (disposing)
{
// Wait for up to 5 seconds grace period
this.Processor?.Shutdown(5000);
this.Processor?.Dispose();
}

if (disposing)
{
// Wait for up to 5 seconds grace period
this.Processor?.Shutdown(5000);
this.Processor?.Dispose();
this.disposed = true;
}

this.disposed = true;
base.Dispose(disposing);
}
}
}
32 changes: 15 additions & 17 deletions src/OpenTelemetry/Metrics/BaseExportingMetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,27 @@ protected override bool OnShutdown(int timeoutMilliseconds)
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (this.disposed)
if (!this.disposed)
{
return;
}

if (disposing)
{
try
if (disposing)
{
if (this.exporter is IPullMetricExporter pullExporter)
try
{
pullExporter.Collect = null;
}
if (this.exporter is IPullMetricExporter pullExporter)
{
pullExporter.Collect = null;
}

this.exporter.Dispose();
}
catch (Exception)
{
// TODO: Log
this.exporter.Dispose();
}
catch (Exception)
{
// TODO: Log
}
}
}

this.disposed = true;
this.disposed = true;
}

base.Dispose(disposing);
}
Expand Down
29 changes: 20 additions & 9 deletions src/OpenTelemetry/Metrics/MeterProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ internal sealed class MeterProviderSdk : MeterProvider
private readonly MeterListener listener;
private readonly MetricReader reader;
private int metricIndex = -1;
private bool disposed;

internal MeterProviderSdk(
Resource resource,
Expand Down Expand Up @@ -447,21 +448,31 @@ internal bool OnShutdown(int timeoutMilliseconds)

protected override void Dispose(bool disposing)
{
if (this.instrumentations != null)
if (!this.disposed)
{
foreach (var item in this.instrumentations)
if (disposing)
{
(item as IDisposable)?.Dispose();
if (this.instrumentations != null)
{
foreach (var item in this.instrumentations)
{
(item as IDisposable)?.Dispose();
}

this.instrumentations.Clear();
}

// Wait for up to 5 seconds grace period
this.reader?.Shutdown(5000);
this.reader?.Dispose();

this.listener.Dispose();
}

this.instrumentations.Clear();
this.disposed = true;
}

// Wait for up to 5 seconds grace period
this.reader?.Shutdown(5000);
this.reader?.Dispose();

this.listener.Dispose();
base.Dispose(disposing);
}
}
}
Loading

0 comments on commit 2d159b4

Please sign in to comment.