Skip to content

Commit

Permalink
Add ForceFlush to TracerProvider (#1837)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
kipwoker and cijothomas committed Feb 23, 2021
1 parent 1e89b39 commit cb066cb
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool
1 change: 1 addition & 0 deletions src/OpenTelemetry/.publicApi/net46/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool
2 changes: 2 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions src/OpenTelemetry/Trace/TracerProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,52 @@ public static TracerProvider AddProcessor(this TracerProvider provider, BaseProc
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.
Expand Down
5 changes: 5 additions & 0 deletions src/OpenTelemetry/Trace/TracerProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ internal TracerProviderSdk AddProcessor(BaseProcessor<Activity> processor)
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.
Expand Down
15 changes: 15 additions & 0 deletions test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,21 @@ public void TracerProviderSdkBuildsWithSDKResource()
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);
Expand Down

0 comments on commit cb066cb

Please sign in to comment.