-
Notifications
You must be signed in to change notification settings - Fork 772
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
{ | ||
base.Dispose(disposing); | ||
|
||
if (disposing) | ||
{ | ||
try | ||
{ | ||
this.exporter.Dispose(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex); | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Looks like "ick" 😄 Probably syncObject or lockObject is more common? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a C# developer, borrowed/learned this from @cijothomas 😺 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 ofShutdown
.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.