diff --git a/docs/trace/extending-the-sdk/MyProcessor.cs b/docs/trace/extending-the-sdk/MyProcessor.cs index 49c259de9e5..01eb41aebb3 100644 --- a/docs/trace/extending-the-sdk/MyProcessor.cs +++ b/docs/trace/extending-the-sdk/MyProcessor.cs @@ -44,9 +44,10 @@ protected override bool OnForceFlush(int timeoutMilliseconds) return true; } - protected override void OnShutdown(int timeoutMilliseconds) + protected override bool OnShutdown(int timeoutMilliseconds) { Console.WriteLine($"{this.name}.OnShutdown({timeoutMilliseconds})"); + return true; } protected override void Dispose(bool disposing) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 3019205c447..69e750531dd 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* Changed `ActivityProcessor.OnShutdown` and `ActivityProcessor.Shutdown` to + return boolean value + ([#1282](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1282)) + ## 0.6.0-beta.1 Released 2020-Sep-15 diff --git a/src/OpenTelemetry/Trace/ActivityExporter.cs b/src/OpenTelemetry/Trace/ActivityExporter.cs index d4ef57213f8..8efca8f5a3f 100644 --- a/src/OpenTelemetry/Trace/ActivityExporter.cs +++ b/src/OpenTelemetry/Trace/ActivityExporter.cs @@ -59,6 +59,9 @@ public abstract class ActivityExporter : IDisposable /// The number of milliseconds to wait, or Timeout.Infinite to /// wait indefinitely. /// + /// + /// Returns true when shutdown succeeded; otherwise, false. + /// /// /// Thrown when the timeoutMilliseconds is smaller than -1. /// @@ -66,7 +69,7 @@ public abstract class ActivityExporter : IDisposable /// This function guarantees thread-safety. Only the first call will /// win, subsequent calls will be no-op. /// - public void Shutdown(int timeoutMilliseconds = Timeout.Infinite) + public bool Shutdown(int timeoutMilliseconds = Timeout.Infinite) { if (timeoutMilliseconds < 0 && timeoutMilliseconds != Timeout.Infinite) { @@ -75,16 +78,18 @@ public void Shutdown(int timeoutMilliseconds = Timeout.Infinite) if (Interlocked.Increment(ref this.shutdownCount) > 1) { - return; // shutdown already called + return false; // shutdown already called } try { this.OnShutdown(timeoutMilliseconds); + return true; // TODO: update exporter.OnShutdown to return boolean } catch (Exception ex) { OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Shutdown), ex); + return false; } } diff --git a/src/OpenTelemetry/Trace/ActivityProcessor.cs b/src/OpenTelemetry/Trace/ActivityProcessor.cs index dbdf89a636a..3224adb9823 100644 --- a/src/OpenTelemetry/Trace/ActivityProcessor.cs +++ b/src/OpenTelemetry/Trace/ActivityProcessor.cs @@ -101,6 +101,9 @@ public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite) /// The number of milliseconds to wait, or Timeout.Infinite to /// wait indefinitely. /// + /// + /// Returns true when shutdown succeeded; otherwise, false. + /// /// /// Thrown when the timeoutMilliseconds is smaller than -1. /// @@ -108,7 +111,7 @@ public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite) /// This function guarantees thread-safety. Only the first call will /// win, subsequent calls will be no-op. /// - public void Shutdown(int timeoutMilliseconds = Timeout.Infinite) + public bool Shutdown(int timeoutMilliseconds = Timeout.Infinite) { if (timeoutMilliseconds < 0 && timeoutMilliseconds != Timeout.Infinite) { @@ -117,16 +120,17 @@ public void Shutdown(int timeoutMilliseconds = Timeout.Infinite) if (Interlocked.Increment(ref this.shutdownCount) > 1) { - return; // shutdown already called + return false; // shutdown already called } try { - this.OnShutdown(timeoutMilliseconds); + return this.OnShutdown(timeoutMilliseconds); } catch (Exception ex) { OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Shutdown), ex); + return false; } } @@ -166,13 +170,17 @@ protected virtual bool OnForceFlush(int timeoutMilliseconds) /// The number of milliseconds to wait, or Timeout.Infinite to /// wait indefinitely. /// + /// + /// Returns true when shutdown succeeded; otherwise, false. + /// /// /// This function is called synchronously on the thread which made the /// first call to Shutdown. This function should not throw /// exceptions. /// - protected virtual void OnShutdown(int timeoutMilliseconds) + protected virtual bool OnShutdown(int timeoutMilliseconds) { + return true; } /// diff --git a/src/OpenTelemetry/Trace/BaseExportActivityProcessor.cs b/src/OpenTelemetry/Trace/BaseExportActivityProcessor.cs index c8ee5ebf510..9b463eb86cf 100644 --- a/src/OpenTelemetry/Trace/BaseExportActivityProcessor.cs +++ b/src/OpenTelemetry/Trace/BaseExportActivityProcessor.cs @@ -46,9 +46,9 @@ public sealed override void OnStart(Activity activity) public abstract override void OnEnd(Activity activity); /// - protected override void OnShutdown(int timeoutMilliseconds) + protected override bool OnShutdown(int timeoutMilliseconds) { - this.exporter.Shutdown(timeoutMilliseconds); + return this.exporter.Shutdown(timeoutMilliseconds); } /// diff --git a/src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs b/src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs index e78aa9d961e..82c20c93329 100644 --- a/src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs +++ b/src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs @@ -174,7 +174,7 @@ protected override bool OnForceFlush(int timeoutMilliseconds) } /// - protected override void OnShutdown(int timeoutMilliseconds) + protected override bool OnShutdown(int timeoutMilliseconds) { this.shutdownDrainTarget = this.circularBuffer.AddedCount; this.shutdownTrigger.Set(); @@ -182,20 +182,18 @@ protected override void OnShutdown(int timeoutMilliseconds) if (timeoutMilliseconds == Timeout.Infinite) { this.exporterThread.Join(); - this.exporter.Shutdown(); - return; + return this.exporter.Shutdown(); } if (timeoutMilliseconds == 0) { - this.exporter.Shutdown(0); - return; + return this.exporter.Shutdown(0); } var sw = Stopwatch.StartNew(); this.exporterThread.Join(timeoutMilliseconds); var timeout = (long)timeoutMilliseconds - sw.ElapsedMilliseconds; - this.exporter.Shutdown((int)Math.Max(timeout, 0)); + return this.exporter.Shutdown((int)Math.Max(timeout, 0)); } private void ExporterProc() diff --git a/src/OpenTelemetry/Trace/CompositeActivityProcessor.cs b/src/OpenTelemetry/Trace/CompositeActivityProcessor.cs index 9ff54069592..5f476f21d8a 100644 --- a/src/OpenTelemetry/Trace/CompositeActivityProcessor.cs +++ b/src/OpenTelemetry/Trace/CompositeActivityProcessor.cs @@ -129,28 +129,30 @@ protected override bool OnForceFlush(int timeoutMilliseconds) } /// - protected override void OnShutdown(int timeoutMilliseconds) + protected override bool OnShutdown(int timeoutMilliseconds) { var cur = this.head; - + var result = true; var sw = Stopwatch.StartNew(); while (cur != null) { if (timeoutMilliseconds == Timeout.Infinite) { - cur.Value.Shutdown(Timeout.Infinite); + result = cur.Value.Shutdown(Timeout.Infinite) && result; } else { var timeout = (long)timeoutMilliseconds - sw.ElapsedMilliseconds; // notify all the processors, even if we run overtime - cur.Value.Shutdown((int)Math.Max(timeout, 0)); + result = cur.Value.Shutdown((int)Math.Max(timeout, 0)) && result; } cur = cur.Next; } + + return result; } protected override void Dispose(bool disposing) diff --git a/test/OpenTelemetry.Tests/Shared/TestActivityProcessor.cs b/test/OpenTelemetry.Tests/Shared/TestActivityProcessor.cs index 53387cbe3c4..e312a1b709d 100644 --- a/test/OpenTelemetry.Tests/Shared/TestActivityProcessor.cs +++ b/test/OpenTelemetry.Tests/Shared/TestActivityProcessor.cs @@ -57,9 +57,10 @@ protected override bool OnForceFlush(int timeoutMilliseconds) return true; } - protected override void OnShutdown(int timeoutMilliseconds) + protected override bool OnShutdown(int timeoutMilliseconds) { this.ShutdownCalled = true; + return true; } protected override void Dispose(bool disposing)