Add ForceFlush to TracerProvider (#1837)

* Add ForceFlush to TracerProvider

* Fixed inlinedoc

* Change the wording

* Remove creating activity

* Update src/OpenTelemetry/CHANGELOG.md

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
This commit is contained in:
Kirill Ivanov 2021-02-23 21:34:22 +01:00 committed by GitHub
parent 1e89b39d72
commit cb066cb5c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1 @@
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool

View File

@ -0,0 +1 @@
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool

View File

@ -0,0 +1 @@
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool

View File

@ -0,0 +1 @@
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool

View File

@ -9,6 +9,8 @@ please check the latest changes
## Unreleased
* Added `ForceFlush` to `TracerProvider`. ([#1837](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1837))
## 1.0.1
Released 2021-Feb-10

View File

@ -43,6 +43,52 @@ namespace OpenTelemetry.Trace
return provider;
}
/// <summary>
/// Flushes the all the processors at TracerProviderSdk, blocks the current thread until flush
/// completed, shutdown signaled or timed out.
/// </summary>
/// <param name="provider">TracerProviderSdk instance on which ForceFlush will be called.</param>
/// <param name="timeoutMilliseconds">
/// The number of milliseconds to wait, or <c>Timeout.Infinite</c> to
/// wait indefinitely.
/// </param>
/// <returns>
/// Returns <c>true</c> when force flush succeeded; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown when the <c>timeoutMilliseconds</c> is smaller than -1.
/// </exception>
/// <remarks>
/// This function guarantees thread-safety.
/// </remarks>
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;
}
/// <summary>
/// Attempts to shutdown the TracerProviderSdk, blocks the current thread until
/// shutdown completed or timed out.

View File

@ -207,6 +207,11 @@ namespace OpenTelemetry.Trace
return this;
}
internal bool OnForceFlush(int timeoutMilliseconds)
{
return this.processor?.ForceFlush(timeoutMilliseconds) ?? true;
}
/// <summary>
/// Called by <c>Shutdown</c>. This function should block the current
/// thread until shutdown completed or timed out.

View File

@ -398,6 +398,21 @@ namespace OpenTelemetry.Trace.Tests
Assert.Single(versionAttribute);
}
[Fact]
public void TracerProviderSdkFlushesProcessorForcibly()
{
using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddProcessor(testActivityProcessor)
.Build();
var isFlushed = tracerProvider.ForceFlush();
Assert.True(isFlushed);
Assert.True(testActivityProcessor.ForceFlushCalled);
}
public void Dispose()
{
GC.SuppressFinalize(this);