Skip to content

Commit

Permalink
Refactor exporter - step 2 (#1081)
Browse files Browse the repository at this point in the history
* skeleton of export processors

* remove unused using statements

* add blank line
  • Loading branch information
reyang authored Aug 14, 2020
1 parent 6fb3c37 commit a4237cf
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 15 deletions.
1 change: 0 additions & 1 deletion docs/trace/building-your-own-processor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Trace;

public class Program
{
Expand Down
2 changes: 0 additions & 2 deletions examples/Console/TestConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System;

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand Down
2 changes: 0 additions & 2 deletions src/OpenTelemetry/Trace/ActivityExporterSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace OpenTelemetry.Trace
{
Expand Down
125 changes: 125 additions & 0 deletions src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// <copyright file="BatchExportActivityProcessor.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 processor that batches activities before calling exporter.
/// </summary>
public class BatchExportActivityProcessor : ActivityProcessor
{
private readonly ActivityExporterSync exporter;
private readonly int maxQueueSize;
private readonly TimeSpan scheduledDelay;
private readonly TimeSpan exporterTimeout;
private readonly int maxExportBatchSize;
private bool disposed;

/// <summary>
/// Initializes a new instance of the <see cref="BatchExportActivityProcessor"/> class with custom settings.
/// </summary>
/// <param name="exporter">Exporter instance.</param>
/// <param name="maxQueueSize">The maximum queue size. After the size is reached data are dropped. The default value is 2048.</param>
/// <param name="scheduledDelayMillis">The delay interval in milliseconds between two consecutive exports. The default value is 5000.</param>
/// <param name="exporterTimeoutMillis">How long the export can run before it is cancelled. The default value is 30000.</param>
/// <param name="maxExportBatchSize">The maximum batch size of every export. It must be smaller or equal to maxQueueSize. The default value is 512.</param>
public BatchExportActivityProcessor(
ActivityExporterSync exporter,
int maxQueueSize = 2048,
int scheduledDelayMillis = 5000,
int exporterTimeoutMillis = 30000,
int maxExportBatchSize = 512)
{
if (maxQueueSize <= 0)
{
throw new ArgumentOutOfRangeException(nameof(maxQueueSize));
}

if (maxExportBatchSize <= 0 || maxExportBatchSize > maxQueueSize)
{
throw new ArgumentOutOfRangeException(nameof(maxExportBatchSize));
}

if (scheduledDelayMillis <= 0)
{
throw new ArgumentOutOfRangeException(nameof(scheduledDelayMillis));
}

if (exporterTimeoutMillis < 0)
{
throw new ArgumentOutOfRangeException(nameof(exporterTimeoutMillis));
}

this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter));
this.maxQueueSize = maxQueueSize;
this.scheduledDelay = TimeSpan.FromMilliseconds(scheduledDelayMillis);
this.exporterTimeout = TimeSpan.FromMilliseconds(exporterTimeoutMillis);
this.maxExportBatchSize = maxExportBatchSize;
}

/// <inheritdoc/>
public override void OnEnd(Activity activity)
{
// TODO
throw new NotImplementedException();
}

/// <inheritdoc/>
/// <exception cref="OperationCanceledException">If the <paramref name="cancellationToken"/> is canceled.</exception>
public override Task ForceFlushAsync(CancellationToken cancellationToken)
{
// TODO
throw new NotImplementedException();
}

/// <inheritdoc/>
/// <exception cref="OperationCanceledException">If the <paramref name="cancellationToken"/> is canceled.</exception>
public override Task ShutdownAsync(CancellationToken cancellationToken)
{
// TODO
throw new NotImplementedException();
}

/// <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 && !this.disposed)
{
try
{
this.exporter.Dispose();
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex);
}

this.disposed = true;
}
}
}
}
93 changes: 93 additions & 0 deletions src/OpenTelemetry/Trace/SimpleExportActivityProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// <copyright file="SimpleExportActivityProcessor.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 simple activity processor that exports activities in OnEnd call without batching.
/// </summary>
public class SimpleExportActivityProcessor : ActivityProcessor
{
private readonly ActivityExporterSync exporter;
private bool stopped;

/// <summary>
/// Initializes a new instance of the <see cref="SimpleExportActivityProcessor"/> class.
/// </summary>
/// <param name="exporter">Activity exporter instance.</param>
public SimpleExportActivityProcessor(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);
}
}
}
}
}
1 change: 0 additions & 1 deletion test/OpenTelemetry.Tests/Resources/ResourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenTelemetry.Resources;
using Xunit;

namespace OpenTelemetry.Resources.Tests
Expand Down
1 change: 0 additions & 1 deletion test/OpenTelemetry.Tests/Resources/ResourcesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenTelemetry.Resources;
using Xunit;

namespace OpenTelemetry.Resources.Tests
Expand Down
1 change: 0 additions & 1 deletion test/OpenTelemetry.Tests/Trace/OpenTelemetrySdkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System.Diagnostics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Xunit;

namespace OpenTelemetry.Trace.Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using OpenTelemetry.Context.Propagation;
using Xunit;

namespace OpenTelemetry.Context.Propagation.Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using OpenTelemetry.Context.Propagation;

namespace OpenTelemetry.Context.Propagation.Tests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTelemetry.Context.Propagation;
using Xunit;

namespace OpenTelemetry.Context.Propagation.Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System.Collections.Generic;
using System.Linq;
using OpenTelemetry.Context.Propagation;
using Xunit;

namespace OpenTelemetry.Context.Propagation.Tests
Expand Down
1 change: 0 additions & 1 deletion test/OpenTelemetry.Tests/Trace/SpanAttributesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// </copyright>

using System;
using System.Collections.Generic;
using Xunit;

namespace OpenTelemetry.Trace.Tests
Expand Down
1 change: 0 additions & 1 deletion test/OpenTelemetry.Tests/Trace/TestSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// </copyright>

using System;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Trace.Tests
{
Expand Down

0 comments on commit a4237cf

Please sign in to comment.