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

Refactor exporter - step 3 #1083

Merged
merged 2 commits into from
Aug 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

namespace OpenTelemetry.Exporter.Console
{
public class ConsoleExporter : ActivityExporter
public class ConsoleExporter : ActivityExporterSync
{
private readonly JsonSerializerOptions serializerOptions;
private bool displayAsJson;
private readonly bool displayAsJson;

public ConsoleExporter(ConsoleExporterOptions options)
{
Expand All @@ -46,9 +46,9 @@ public ConsoleExporter(ConsoleExporterOptions options)
this.serializerOptions.Converters.Add(new ActivityTraceIdConverter());
}

public override Task<ExportResult> ExportAsync(IEnumerable<Activity> activityBatch, CancellationToken cancellationToken)
public override ExportResultSync Export(IEnumerable<Activity> batch)
{
foreach (var activity in activityBatch)
foreach (var activity in batch)
{
if (this.displayAsJson)
{
Expand Down Expand Up @@ -127,12 +127,7 @@ public override Task<ExportResult> ExportAsync(IEnumerable<Activity> activityBat
}
}

return Task.FromResult(ExportResult.Success);
}

public override Task ShutdownAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
return ExportResultSync.Success;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static TracerProviderBuilder AddConsoleExporter(this TracerProviderBuilde

var options = new ConsoleExporterOptions();
configure?.Invoke(options);
return builder.AddProcessor(new SimpleActivityProcessor(new ConsoleExporter(options)));
return builder.AddProcessor(new SimpleExportActivityProcessor(new ConsoleExporter(options)));
}
}
}
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Trace/ActivityExporterSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class ActivityExporterSync : IDisposable
/// </summary>
/// <param name="batch">Batch of activities to export.</param>
/// <returns>Result of export.</returns>
public abstract ExportResult Export(IEnumerable<Activity> batch);
public abstract ExportResultSync Export(IEnumerable<Activity> batch);

/// <summary>
/// Shuts down the exporter.
Expand Down
93 changes: 93 additions & 0 deletions src/OpenTelemetry/Trace/ReentrantExportActivityProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// <copyright file="ReentrantExportActivityProcessor.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Trace
{
/// <summary>
/// Implements activity processor that exports <see cref="Activity"/> at each OnEnd call without synchronization.
/// </summary>
public class ReentrantExportActivityProcessor : ActivityProcessor
{
private readonly ActivityExporterSync exporter;
private bool stopped;

/// <summary>
/// Initializes a new instance of the <see cref="ReentrantExportActivityProcessor"/> class.
/// </summary>
/// <param name="exporter">Activity exporter instance.</param>
public ReentrantExportActivityProcessor(ActivityExporterSync exporter)
{
this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
}

/// <inheritdoc />
public override void OnEnd(Activity activity)
{
try
{
// TODO: avoid heap allocation
_ = this.exporter.Export(new[] { activity });
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.OnEnd), ex);
}
}

/// <inheritdoc />
public override Task ShutdownAsync(CancellationToken cancellationToken)
{
if (!this.stopped)
{
this.exporter.Shutdown();
this.stopped = true;
}

#if NET452
return Task.FromResult(0);
#else
return Task.CompletedTask;
#endif
}

/// <summary>
/// Releases the unmanaged resources used by this class and optionally releases the managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
Copy link
Member

Choose a reason for hiding this comment

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

@reyang Just noticed you're not calling Shutdown from Dispose. You did that on purpose, right?

You know, TracerProviderSdk doesn't seem to call Shutdown on anything, only Dispose. Why don't we just remove Shutdown methods across the board? I know the spec says we need it, but we have Dispose pattern in .NET for shutdown. Breaking with the spec here (in name only) might actually lead to a simpler, more easy-to-use & implement library?

Copy link
Member Author

Choose a reason for hiding this comment

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

Tracked by #1024.

Yep, I think Dispose is essentially the language idiomatic version of Shutdown. Dispose doesn't take any parameter, Shutdown might potentially take a timeout value, and that can be engineered as a property of the object instead of an argument to the function.

base.Dispose(disposing);

if (disposing)
{
try
{
this.exporter.Dispose();
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex);
}
}
}
}
}
55 changes: 6 additions & 49 deletions src/OpenTelemetry/Trace/SimpleExportActivityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,70 +23,27 @@
namespace OpenTelemetry.Trace
{
/// <summary>
/// Implements simple activity processor that exports activities in OnEnd call without batching.
/// Implements activity processor that exports <see cref="Activity"/> at each OnEnd call.
/// </summary>
public class SimpleExportActivityProcessor : ActivityProcessor
public class SimpleExportActivityProcessor : ReentrantExportActivityProcessor
{
private readonly ActivityExporterSync exporter;
private bool stopped;
private readonly object lck = new object();
Copy link
Member

Choose a reason for hiding this comment

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

nit: Looks like "ick" 😄 Probably syncObject or lockObject is more common?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not a C# developer, borrowed/learned this from @cijothomas 😺

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

:D Thanks for fixing the names throughout.


/// <summary>
/// Initializes a new instance of the <see cref="SimpleExportActivityProcessor"/> class.
/// </summary>
/// <param name="exporter">Activity exporter instance.</param>
public SimpleExportActivityProcessor(ActivityExporterSync exporter)
: base(exporter)
{
this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
}

/// <inheritdoc />
public override void OnEnd(Activity activity)
{
try
lock (this.lck)
{
// TODO: avoid heap allocation
_ = this.exporter.Export(new[] { activity });
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.OnEnd), ex);
}
}

/// <inheritdoc />
public override Task ShutdownAsync(CancellationToken cancellationToken)
{
if (!this.stopped)
{
this.exporter.Shutdown();
this.stopped = true;
}

#if NET452
return Task.FromResult(0);
#else
return Task.CompletedTask;
#endif
}

/// <summary>
/// Releases the unmanaged resources used by this class and optionally releases the managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (disposing)
{
try
{
this.exporter.Dispose();
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex);
}
base.OnEnd(activity);
}
}
}
Expand Down