diff --git a/src/OpenTelemetry/Logs/LoggerProviderSdk.cs b/src/OpenTelemetry/Logs/LoggerProviderSdk.cs index 4287fcad3a2..cc502387d0c 100644 --- a/src/OpenTelemetry/Logs/LoggerProviderSdk.cs +++ b/src/OpenTelemetry/Logs/LoggerProviderSdk.cs @@ -217,16 +217,13 @@ protected override void Dispose(bool disposing) { if (disposing) { - if (this.instrumentations != null) + foreach (var item in this.instrumentations) { - foreach (var item in this.instrumentations) - { - (item as IDisposable)?.Dispose(); - } - - this.instrumentations.Clear(); + (item as IDisposable)?.Dispose(); } + this.instrumentations.Clear(); + // Wait for up to 5 seconds grace period this.Processor?.Shutdown(5000); this.Processor?.Dispose(); diff --git a/src/OpenTelemetry/Metrics/MeterProviderSdk.cs b/src/OpenTelemetry/Metrics/MeterProviderSdk.cs index a07187de4ef..91b709921b1 100644 --- a/src/OpenTelemetry/Metrics/MeterProviderSdk.cs +++ b/src/OpenTelemetry/Metrics/MeterProviderSdk.cs @@ -451,16 +451,13 @@ protected override void Dispose(bool disposing) { if (disposing) { - if (this.instrumentations != null) + foreach (var item in this.instrumentations) { - foreach (var item in this.instrumentations) - { - (item as IDisposable)?.Dispose(); - } - - this.instrumentations.Clear(); + (item as IDisposable)?.Dispose(); } + this.instrumentations.Clear(); + // Wait for up to 5 seconds grace period this.reader?.Shutdown(5000); this.reader?.Dispose(); diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index fb5b24bfe7c..5f9a3bad07c 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -352,18 +352,14 @@ internal bool OnForceFlush(int timeoutMilliseconds) internal bool OnShutdown(int timeoutMilliseconds) { // TO DO Put OnShutdown logic in a task to run within the user provider timeOutMilliseconds - bool? result; - if (this.instrumentations != null) + foreach (var item in this.instrumentations) { - foreach (var item in this.instrumentations) - { - (item as IDisposable)?.Dispose(); - } - - this.instrumentations.Clear(); + (item as IDisposable)?.Dispose(); } - result = this.processor?.Shutdown(timeoutMilliseconds); + this.instrumentations.Clear(); + + bool? result = this.processor?.Shutdown(timeoutMilliseconds); this.listener?.Dispose(); return result ?? true; } @@ -374,16 +370,13 @@ protected override void Dispose(bool disposing) { if (disposing) { - if (this.instrumentations != null) + foreach (var item in this.instrumentations) { - foreach (var item in this.instrumentations) - { - (item as IDisposable)?.Dispose(); - } - - this.instrumentations.Clear(); + (item as IDisposable)?.Dispose(); } + this.instrumentations.Clear(); + (this.sampler as IDisposable)?.Dispose(); // Wait for up to 5 seconds grace period