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

Fix batching tests and processor improvements #252

Merged
merged 4 commits into from
Oct 9, 2019
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
20 changes: 11 additions & 9 deletions src/OpenTelemetry/Trace/Export/BatchingSpanProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class BatchingSpanProcessor : SpanProcessor, IDisposable
private readonly TimeSpan scheduleDelay;
private readonly Task worker;
private CancellationTokenSource cts;
private int currentQueueSize;
private volatile int currentQueueSize;
private bool stopping = false;

/// <summary>
Expand Down Expand Up @@ -113,6 +113,7 @@ public override void OnEnd(Span span)
}

Interlocked.Increment(ref this.currentQueueSize);

this.exportQueue.Enqueue(span);
}

Expand All @@ -130,9 +131,7 @@ public override async Task ShutdownAsync(CancellationToken cancellationToken)
// if there are more items, continue until cancellation token allows
while (this.currentQueueSize > 0 && !cancellationToken.IsCancellationRequested)
{
Debug.WriteLine($"!!! {DateTime.UtcNow:o} export " + this.currentQueueSize);
await this.ExportBatchAsync(cancellationToken).ConfigureAwait(false);
Debug.WriteLine($"!!! {DateTime.UtcNow:o} export finished" + this.currentQueueSize);
}

// there is no point in waiting for a worker task if cancellation happens
Expand Down Expand Up @@ -161,16 +160,19 @@ private async Task ExportBatchAsync(CancellationToken cancellationToken)
return;
}

var nextBatchSize = Math.Min(this.currentQueueSize, this.maxExportBatchSize);

if (nextBatchSize == 0)
List<Span> batch = null;
if (this.exportQueue.TryDequeue(out var nextSpan))
{
Interlocked.Decrement(ref this.currentQueueSize);
batch = new List<Span> { nextSpan };
}
else
{
// nothing in queue
return;
}

var batch = new List<Span>(nextBatchSize);

while (batch.Count < nextBatchSize && this.exportQueue.TryDequeue(out var nextSpan))
while (batch.Count < this.maxExportBatchSize && this.exportQueue.TryDequeue(out nextSpan))
{
Interlocked.Decrement(ref this.currentQueueSize);
batch.Add(nextSpan);
Expand Down
3 changes: 1 addition & 2 deletions test/OpenTelemetry.Tests/Impl/Testing/Export/TestExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
// limitations under the License.
// </copyright>

using System.Collections.Concurrent;

namespace OpenTelemetry.Testing.Export
{
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Trace;
Expand Down
Loading