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

Update Processor.Shutdown to meet with the latest spec #1282

Merged
merged 2 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/trace/extending-the-sdk/MyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/OpenTelemetry/Trace/ActivityExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ public abstract class ActivityExporter : IDisposable
/// The number of milliseconds to wait, or <c>Timeout.Infinite</c> to
/// wait indefinitely.
/// </param>
/// <returns>
/// Returns <c>true</c> when shutdown 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. Only the first call will
/// win, subsequent calls will be no-op.
/// </remarks>
public void Shutdown(int timeoutMilliseconds = Timeout.Infinite)
public bool Shutdown(int timeoutMilliseconds = Timeout.Infinite)
{
if (timeoutMilliseconds < 0 && timeoutMilliseconds != Timeout.Infinite)
{
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

Put this return true to the end of this function (right before })?

Copy link
Member Author

Choose a reason for hiding this comment

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

The way I look at try { return } catch() { return } is similar to if() { return } else { return }. Do you see some benefit of putting it outside?

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought normal execution flow would end at }, besides that, I don't see any advantage to move return true to the end of the function.

}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Shutdown), ex);
return false;
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/OpenTelemetry/Trace/ActivityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,17 @@ public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite)
/// The number of milliseconds to wait, or <c>Timeout.Infinite</c> to
/// wait indefinitely.
/// </param>
/// <returns>
/// Returns <c>true</c> when shutdown 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. Only the first call will
/// win, subsequent calls will be no-op.
/// </remarks>
public void Shutdown(int timeoutMilliseconds = Timeout.Infinite)
public bool Shutdown(int timeoutMilliseconds = Timeout.Infinite)
{
if (timeoutMilliseconds < 0 && timeoutMilliseconds != Timeout.Infinite)
{
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -166,13 +170,17 @@ protected virtual bool OnForceFlush(int timeoutMilliseconds)
/// The number of milliseconds to wait, or <c>Timeout.Infinite</c> to
/// wait indefinitely.
/// </param>
/// <returns>
/// Returns <c>true</c> when shutdown succeeded; otherwise, <c>false</c>.
/// </returns>
/// <remarks>
/// This function is called synchronously on the thread which made the
/// first call to <c>Shutdown</c>. This function should not throw
/// exceptions.
/// </remarks>
protected virtual void OnShutdown(int timeoutMilliseconds)
protected virtual bool OnShutdown(int timeoutMilliseconds)
{
return true;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry/Trace/BaseExportActivityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public sealed override void OnStart(Activity activity)
public abstract override void OnEnd(Activity activity);

/// <inheritdoc />
protected override void OnShutdown(int timeoutMilliseconds)
protected override bool OnShutdown(int timeoutMilliseconds)
{
this.exporter.Shutdown(timeoutMilliseconds);
return this.exporter.Shutdown(timeoutMilliseconds);
}

/// <inheritdoc/>
Expand Down
10 changes: 4 additions & 6 deletions src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,26 @@ protected override bool OnForceFlush(int timeoutMilliseconds)
}

/// <inheritdoc/>
protected override void OnShutdown(int timeoutMilliseconds)
protected override bool OnShutdown(int timeoutMilliseconds)
{
this.shutdownDrainTarget = this.circularBuffer.AddedCount;
this.shutdownTrigger.Set();

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()
Expand Down
10 changes: 6 additions & 4 deletions src/OpenTelemetry/Trace/CompositeActivityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,28 +129,30 @@ protected override bool OnForceFlush(int timeoutMilliseconds)
}

/// <inheritdoc/>
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;
reyang marked this conversation as resolved.
Show resolved Hide resolved
}
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)
Expand Down
3 changes: 2 additions & 1 deletion test/OpenTelemetry.Tests/Shared/TestActivityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down