Skip to content

Commit

Permalink
Add exporter ForceFlush (#2525)
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang authored Oct 27, 2021
1 parent 3aab124 commit 6350288
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 11 deletions.
11 changes: 5 additions & 6 deletions build/Common.prod.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<!-- Temporarily disable public API check till metrics get close to Release
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="$(MicrosoftCodeAnalysisAnalyzersPkgVer)" Condition=" $(OS) == 'Windows_NT'">
<!-- Temporarily disable public API check till metrics get close to Release
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="$(MicrosoftCodeAnalysisAnalyzersPkgVer)" Condition=" $(OS) == 'Windows_NT'">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
-->
</PackageReference>
-->
</ItemGroup>

<ItemGroup Condition="'$(MinVerTagPrefix)' == 'core-' AND '$(CheckAPICompatibility)' == 'true'">
<PackageReference Include="Microsoft.DotNet.ApiCompat" Version="6.0.0-beta.21308.1" PrivateAssets="All" />
Expand Down
5 changes: 3 additions & 2 deletions docs/logs/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ not covered by the built-in exporters:
* Exporters should derive from `OpenTelemetry.BaseExporter<LogRecord>` (which
belongs to the [OpenTelemetry](../../../src/OpenTelemetry/README.md) package)
and implement the `Export` method.
* Exporters can optionally implement the `OnShutdown` method.
* Exporters can optionally implement the `OnForceFlush` and `OnShutdown` method.
* Depending on user's choice and load on the application, `Export` may get
called with one or more log records.
* Exporters will only receive sampled-in log records.
* Exporters should not throw exceptions from `Export` and `OnShutdown`.
* Exporters should not throw exceptions from `Export`, `OnForceFlush` and
`OnShutdown`.
* Exporters should not modify log records they receive (the same log records may
be exported again by different exporter).
* Exporters are responsible for any retry logic needed by the scenario. The SDK
Expand Down
5 changes: 3 additions & 2 deletions docs/trace/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ not covered by the built-in exporters:
* Exporters should derive from `OpenTelemetry.BaseExporter<Activity>` (which
belongs to the [OpenTelemetry](../../../src/OpenTelemetry/README.md) package)
and implement the `Export` method.
* Exporters can optionally implement the `OnShutdown` method.
* Exporters can optionally implement the `OnForceFlush` and `OnShutdown` method.
* Depending on user's choice and load on the application, `Export` may get
called with one or more activities.
* Exporters will only receive sampled-in and ended activities.
* Exporters should not throw exceptions from `Export` and `OnShutdown`.
* Exporters should not throw exceptions from `Export`, `OnForceFlush` and
`OnShutdown`.
* Exporters should not modify activities they receive (the same activity may be
exported again by different exporter).
* Exporters are responsible for any retry logic needed by the scenario. The SDK
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
OpenTelemetry.BaseExporter<T>.ForceFlush(int timeoutMilliseconds = -1) -> bool
OpenTelemetry.Trace.BatchExportActivityProcessorOptions
OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions() -> void
OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions() -> void
override OpenTelemetry.BaseExportProcessor<T>.OnForceFlush(int timeoutMilliseconds) -> bool
override OpenTelemetry.BatchExportProcessor<T>.Dispose(bool disposing) -> void
virtual OpenTelemetry.BaseExporter<T>.OnForceFlush(int timeoutMilliseconds) -> bool
6 changes: 6 additions & 0 deletions src/OpenTelemetry/BaseExportProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ internal override void SetParentProvider(BaseProvider parentProvider)

protected abstract void OnExport(T data);

/// <inheritdoc />
protected override bool OnForceFlush(int timeoutMilliseconds)
{
return this.exporter.ForceFlush(timeoutMilliseconds);
}

/// <inheritdoc />
protected override bool OnShutdown(int timeoutMilliseconds)
{
Expand Down
53 changes: 53 additions & 0 deletions src/OpenTelemetry/BaseExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,38 @@ public abstract class BaseExporter<T> : IDisposable
/// <returns>Result of the export operation.</returns>
public abstract ExportResult Export(in Batch<T> batch);

/// <summary>
/// Flushes the exporter, blocks the current thread until flush
/// completed, shutdown signaled or timed out.
/// </summary>
/// <param name="timeoutMilliseconds">
/// The number (non-negative) of milliseconds to wait, or
/// <c>Timeout.Infinite</c> to wait indefinitely.
/// </param>
/// <returns>
/// Returns <c>true</c> when flush succeeded; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when the <c>timeoutMilliseconds</c> is smaller than -1.
/// </exception>
/// <remarks>
/// This function guarantees thread-safety.
/// </remarks>
public bool ForceFlush(int timeoutMilliseconds = Timeout.Infinite)
{
Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds));

try
{
return this.OnForceFlush(timeoutMilliseconds);
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.ForceFlush), ex);
return false;
}
}

/// <summary>
/// Attempts to shutdown the exporter, blocks the current thread until
/// shutdown completed or timed out.
Expand Down Expand Up @@ -102,6 +134,27 @@ public void Dispose()
GC.SuppressFinalize(this);
}

/// <summary>
/// Called by <c>ForceFlush</c>. This function should block the current
/// thread until flush completed, shutdown signaled or timed out.
/// </summary>
/// <param name="timeoutMilliseconds">
/// The number (non-negative) of milliseconds to wait, or
/// <c>Timeout.Infinite</c> to wait indefinitely.
/// </param>
/// <returns>
/// Returns <c>true</c> when flush succeeded; otherwise, <c>false</c>.
/// </returns>
/// <remarks>
/// This function is called synchronously on the thread which called
/// <c>ForceFlush</c>. This function should be thread-safe, and should
/// not throw exceptions.
/// </remarks>
protected virtual bool OnForceFlush(int timeoutMilliseconds)
{
return true;
}

/// <summary>
/// Called by <c>Shutdown</c>. This function should block the current
/// thread until shutdown completed or timed out.
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
`FormatException` if it fails to parse any of the supported environment
variables.

* Added `BaseExporter.ForceFlush`.
([#2525](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2525))

## 1.2.0-beta1

Released 2021-Oct-08
Expand Down

0 comments on commit 6350288

Please sign in to comment.