Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ForceFlush to TracerProvider #1837

Merged
merged 6 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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` method `TracerProviderExtensions`. ([#1837](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1837))
kipwoker marked this conversation as resolved.
Show resolved Hide resolved

## 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)
reyang marked this conversation as resolved.
Show resolved Hide resolved
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}

if (provider is TracerProviderSdk tracerProviderSdk)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a hack. There should be some public type (class or interface) representing TracerProviderSdk. But I guess it's too late for that 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. :)
I just made it like it was done for Shutdown method. In my opinion, it's better to change nothing at the moment and make architectural changes at separate PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not address any other issue in this PR :)
Happy to address them separate. (or not address :D)

{
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