From 8c6a7c2781abbfb6a28b36a289a25b87022cbbb9 Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Thu, 18 Feb 2021 17:21:39 +0100 Subject: [PATCH 1/5] Add ForceFlush to TracerProvider --- .../.publicApi/net452/PublicAPI.Unshipped.txt | 1 + .../.publicApi/net46/PublicAPI.Unshipped.txt | 1 + .../.publicApi/net461/PublicAPI.Unshipped.txt | 1 + .../netstandard2.0/PublicAPI.Unshipped.txt | 1 + src/OpenTelemetry/CHANGELOG.md | 2 + .../Trace/TracerProviderExtensions.cs | 47 +++++++++++++++++++ src/OpenTelemetry/Trace/TracerProviderSdk.cs | 5 ++ .../Trace/TracerProviderSdkTest.cs | 19 ++++++++ 8 files changed, 77 insertions(+) diff --git a/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt index e69de29bb2d..8b90281a056 100644 --- a/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool \ No newline at end of file diff --git a/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt index e69de29bb2d..6779d236a29 100644 --- a/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt index e69de29bb2d..6779d236a29 100644 --- a/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index e69de29bb2d..6779d236a29 100644 --- a/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index a0b04e6c57e..190d9560158 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -9,6 +9,8 @@ please check the latest changes ## Unreleased +* Added `ForceFlush` method `TracerProviderExtensions`. ([#1837](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1837)) + ## 1.0.1 Released 2021-Feb-10 diff --git a/src/OpenTelemetry/Trace/TracerProviderExtensions.cs b/src/OpenTelemetry/Trace/TracerProviderExtensions.cs index 302f85d5173..3e5b23ca78d 100644 --- a/src/OpenTelemetry/Trace/TracerProviderExtensions.cs +++ b/src/OpenTelemetry/Trace/TracerProviderExtensions.cs @@ -43,6 +43,53 @@ public static TracerProvider AddProcessor(this TracerProvider provider, BaseProc return provider; } + /// + /// Attempts to force flush the TracerProviderSdk, blocks the current thread until + /// force flush completed or timed out. + /// + /// TracerProviderSdk instance on which ForceFlush will be called. + /// + /// The number of milliseconds to wait, or Timeout.Infinite to + /// wait indefinitely. + /// + /// + /// Returns true when force flush succeeded; otherwise, false. + /// + /// + /// Thrown when the timeoutMilliseconds is smaller than -1. + /// + /// + /// This function guarantees thread-safety. Only the first call will + /// win, subsequent calls will be no-op. + /// + public static bool ForceFlush(this TracerProvider provider, int timeoutMilliseconds = Timeout.Infinite) + { + if (provider == null) + { + throw new ArgumentNullException(nameof(provider)); + } + + if (provider is TracerProviderSdk tracerProviderSdk) + { + if (timeoutMilliseconds < 0 && timeoutMilliseconds != Timeout.Infinite) + { + throw new ArgumentOutOfRangeException(nameof(timeoutMilliseconds), timeoutMilliseconds, "timeoutMilliseconds should be non-negative."); + } + + try + { + return tracerProviderSdk.OnForceFlush(timeoutMilliseconds); + } + catch (Exception ex) + { + OpenTelemetrySdkEventSource.Log.TracerProviderException(nameof(tracerProviderSdk.OnForceFlush), ex); + return false; + } + } + + return true; + } + /// /// Attempts to shutdown the TracerProviderSdk, blocks the current thread until /// shutdown completed or timed out. diff --git a/src/OpenTelemetry/Trace/TracerProviderSdk.cs b/src/OpenTelemetry/Trace/TracerProviderSdk.cs index d0759533a5e..a2e6dff3ba4 100644 --- a/src/OpenTelemetry/Trace/TracerProviderSdk.cs +++ b/src/OpenTelemetry/Trace/TracerProviderSdk.cs @@ -207,6 +207,11 @@ internal TracerProviderSdk AddProcessor(BaseProcessor processor) return this; } + internal bool OnForceFlush(int timeoutMilliseconds) + { + return this.processor?.ForceFlush(timeoutMilliseconds) ?? true; + } + /// /// Called by Shutdown. This function should block the current /// thread until shutdown completed or timed out. diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs index e193cb9b5b6..d0e3765d1da 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs @@ -398,6 +398,25 @@ public void TracerProviderSdkBuildsWithSDKResource() Assert.Single(versionAttribute); } + [Fact] + public void TracerProviderSdkFlushesProcessorForcibly() + { + using TestActivityProcessor testActivityProcessor = new TestActivityProcessor(); + + using var tracerProvider = Sdk.CreateTracerProviderBuilder() + .AddProcessor(testActivityProcessor) + .Build(); + + Activity activity = new Activity("test"); + activity.Start(); + activity.Stop(); + + var isFlushed = tracerProvider.ForceFlush(); + + Assert.True(isFlushed); + Assert.True(testActivityProcessor.ForceFlushCalled); + } + public void Dispose() { GC.SuppressFinalize(this); From 442d9dd6244ed1c1c2785eb7d61e09a49853b1e6 Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Thu, 18 Feb 2021 17:34:37 +0100 Subject: [PATCH 2/5] Fixed inlinedoc --- src/OpenTelemetry/Trace/TracerProviderExtensions.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderExtensions.cs b/src/OpenTelemetry/Trace/TracerProviderExtensions.cs index 3e5b23ca78d..24732ba5c3a 100644 --- a/src/OpenTelemetry/Trace/TracerProviderExtensions.cs +++ b/src/OpenTelemetry/Trace/TracerProviderExtensions.cs @@ -59,8 +59,7 @@ public static TracerProvider AddProcessor(this TracerProvider provider, BaseProc /// Thrown when the timeoutMilliseconds is smaller than -1. /// /// - /// This function guarantees thread-safety. Only the first call will - /// win, subsequent calls will be no-op. + /// This function guarantees thread-safety. /// public static bool ForceFlush(this TracerProvider provider, int timeoutMilliseconds = Timeout.Infinite) { From 1908ed90997eaaef2a2c105fe6574d726fdebcea Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Thu, 18 Feb 2021 17:49:15 +0100 Subject: [PATCH 3/5] Change the wording --- src/OpenTelemetry/Trace/TracerProviderExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry/Trace/TracerProviderExtensions.cs b/src/OpenTelemetry/Trace/TracerProviderExtensions.cs index 24732ba5c3a..cfa331e179d 100644 --- a/src/OpenTelemetry/Trace/TracerProviderExtensions.cs +++ b/src/OpenTelemetry/Trace/TracerProviderExtensions.cs @@ -44,8 +44,8 @@ public static TracerProvider AddProcessor(this TracerProvider provider, BaseProc } /// - /// Attempts to force flush the TracerProviderSdk, blocks the current thread until - /// force flush completed or timed out. + /// Flushes the all the processors at TracerProviderSdk, blocks the current thread until flush + /// completed, shutdown signaled or timed out. /// /// TracerProviderSdk instance on which ForceFlush will be called. /// From e13cd502df54510e1b7c09581971a6de889e1591 Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Mon, 22 Feb 2021 09:58:28 +0100 Subject: [PATCH 4/5] Remove creating activity --- test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs index d0e3765d1da..64891ae7098 100644 --- a/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs @@ -407,10 +407,6 @@ public void TracerProviderSdkFlushesProcessorForcibly() .AddProcessor(testActivityProcessor) .Build(); - Activity activity = new Activity("test"); - activity.Start(); - activity.Stop(); - var isFlushed = tracerProvider.ForceFlush(); Assert.True(isFlushed); From aceb0d59e07d56fa0fe1b3f5e45ab0c0e565c94b Mon Sep 17 00:00:00 2001 From: Kirill Ivanov Date: Tue, 23 Feb 2021 21:03:39 +0100 Subject: [PATCH 5/5] Update src/OpenTelemetry/CHANGELOG.md Co-authored-by: Cijo Thomas --- src/OpenTelemetry/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 190d9560158..e3770cadff8 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -9,7 +9,7 @@ please check the latest changes ## Unreleased -* Added `ForceFlush` method `TracerProviderExtensions`. ([#1837](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1837)) +* Added `ForceFlush` to `TracerProvider`. ([#1837](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1837)) ## 1.0.1