Skip to content
1 change: 1 addition & 0 deletions OpenTelemetry.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
<File Path="src/Shared/SemanticConventions.cs" />
<File Path="src/Shared/SpanAttributeConstants.cs" />
<File Path="src/Shared/StatusHelper.cs" />
<File Path="src/Shared/StopwatchExtensions.cs" />
<File Path="src/Shared/ThreadSafeRandom.cs" />
</Folder>
<Folder Name="/Shared/Configuration/">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected override bool OnSubmitRequestFailure(byte[] request, int contentLength

protected override void OnShutdown(int timeoutMilliseconds)
{
var sw = timeoutMilliseconds == Timeout.Infinite ? null : Stopwatch.StartNew();
long? timestamp = timeoutMilliseconds == Timeout.Infinite ? null : Stopwatch.GetTimestamp();

try
{
Expand All @@ -65,16 +65,12 @@ protected override void OnShutdown(int timeoutMilliseconds)

this.thread.Join(timeoutMilliseconds);

if (sw != null)
if (timestamp is { } startedAt)
{
var timeout = timeoutMilliseconds - sw.ElapsedMilliseconds;

base.OnShutdown((int)Math.Max(timeout, 0));
}
else
{
base.OnShutdown(timeoutMilliseconds);
timeoutMilliseconds = Stopwatch.Remaining(timeoutMilliseconds, startedAt);
}

base.OnShutdown(timeoutMilliseconds);
}

protected override void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ public bool TrySubmitRequest(byte[] request, int contentLength)
{
var deadlineUtc = DateTime.UtcNow.AddMilliseconds(this.TimeoutMilliseconds);
var response = this.ExportClient.SendExportRequest(request, contentLength, deadlineUtc);
if (response.Success)
{
return true;
}

return this.OnSubmitRequestFailure(request, contentLength, response);
return response.Success || this.OnSubmitRequestFailure(request, contentLength, response);
}
catch (Exception ex)
{
Expand All @@ -65,15 +61,13 @@ public bool Shutdown(int timeoutMilliseconds)
{
Guard.ThrowIfInvalidTimeout(timeoutMilliseconds);

var sw = timeoutMilliseconds == Timeout.Infinite ? null : Stopwatch.StartNew();
long? timestamp = timeoutMilliseconds == Timeout.Infinite ? null : Stopwatch.GetTimestamp();

this.OnShutdown(timeoutMilliseconds);

if (sw != null)
if (timestamp is { } startedAt)
{
var timeout = timeoutMilliseconds - sw.ElapsedMilliseconds;

return this.ExportClient.Shutdown((int)Math.Max(timeout, 0));
timeoutMilliseconds = Stopwatch.Remaining(timeoutMilliseconds, startedAt);
}

return this.ExportClient.Shutdown(timeoutMilliseconds);
Expand Down
22 changes: 5 additions & 17 deletions src/OpenTelemetry/BatchExportProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,12 @@ protected override void OnExport(T data)

/// <inheritdoc/>
protected override bool OnForceFlush(int timeoutMilliseconds)
{
return this.worker.WaitForExport(timeoutMilliseconds);
}
=> this.worker.WaitForExport(timeoutMilliseconds);

/// <inheritdoc/>
protected override bool OnShutdown(int timeoutMilliseconds)
{
Stopwatch? shutdownStopwatch = null;
if (timeoutMilliseconds > 0)
{
shutdownStopwatch = Stopwatch.StartNew();
}
long? timestamp = timeoutMilliseconds == Timeout.Infinite ? null : Stopwatch.GetTimestamp();

var result = this.worker.Shutdown(timeoutMilliseconds);

Expand All @@ -124,18 +118,12 @@ protected override bool OnShutdown(int timeoutMilliseconds)
return this.exporter.Shutdown(0) && result;
}

int remainingTimeout = timeoutMilliseconds;
if (shutdownStopwatch != null)
if (timestamp is { } startedAt)
{
shutdownStopwatch.Stop();
remainingTimeout -= (int)shutdownStopwatch.ElapsedMilliseconds;
if (remainingTimeout < 0)
{
remainingTimeout = 0;
}
timeoutMilliseconds = Stopwatch.Remaining(timeoutMilliseconds, startedAt);
}

return this.exporter.Shutdown(remainingTimeout) && result;
return this.exporter.Shutdown(timeoutMilliseconds) && result;
}

/// <inheritdoc/>
Expand Down
56 changes: 22 additions & 34 deletions src/OpenTelemetry/CompositeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,38 +63,38 @@ public CompositeProcessor<T> AddProcessor(BaseProcessor<T> processor)
/// <inheritdoc/>
public override void OnEnd(T data)
{
for (var cur = this.Head; cur != null; cur = cur.Next)
for (var current = this.Head; current != null; current = current.Next)
{
cur.Value.OnEnd(data);
current.Value.OnEnd(data);
}
}

/// <inheritdoc/>
public override void OnStart(T data)
{
for (var cur = this.Head; cur != null; cur = cur.Next)
for (var current = this.Head; current != null; current = current.Next)
{
cur.Value.OnStart(data);
current.Value.OnStart(data);
}
}

internal override void SetParentProvider(BaseProvider parentProvider)
{
base.SetParentProvider(parentProvider);

for (var cur = this.Head; cur != null; cur = cur.Next)
for (var current = this.Head; current != null; current = current.Next)
{
cur.Value.SetParentProvider(parentProvider);
current.Value.SetParentProvider(parentProvider);
}
}

internal IReadOnlyList<BaseProcessor<T>> ToReadOnlyList()
{
var list = new List<BaseProcessor<T>>();

for (var cur = this.Head; cur != null; cur = cur.Next)
for (var current = this.Head; current != null; current = current.Next)
{
list.Add(cur.Value);
list.Add(current.Value);
}

return list;
Expand All @@ -104,23 +104,17 @@ internal IReadOnlyList<BaseProcessor<T>> ToReadOnlyList()
protected override bool OnForceFlush(int timeoutMilliseconds)
{
var result = true;
var sw = timeoutMilliseconds == Timeout.Infinite
? null
: Stopwatch.StartNew();
long? timestamp = timeoutMilliseconds == Timeout.Infinite ? null : Stopwatch.GetTimestamp();

for (var cur = this.Head; cur != null; cur = cur.Next)
for (var current = this.Head; current != null; current = current.Next)
{
if (sw == null)
if (timestamp is { } startedAt)
{
result = cur.Value.ForceFlush() && result;
timeoutMilliseconds = Stopwatch.Remaining(timeoutMilliseconds, startedAt);
}
else
{
var timeout = timeoutMilliseconds - sw.ElapsedMilliseconds;

// notify all the processors, even if we run overtime
result = cur.Value.ForceFlush((int)Math.Max(timeout, 0)) && result;
}
// Notify all the processors, even if we run overtime
result = current.Value.ForceFlush(timeoutMilliseconds) && result;
}
Comment thread
martincostello marked this conversation as resolved.

return result;
Expand All @@ -130,23 +124,17 @@ protected override bool OnForceFlush(int timeoutMilliseconds)
protected override bool OnShutdown(int timeoutMilliseconds)
{
var result = true;
var sw = timeoutMilliseconds == Timeout.Infinite
? null
: Stopwatch.StartNew();
long? timestamp = timeoutMilliseconds == Timeout.Infinite ? null : Stopwatch.GetTimestamp();

for (var cur = this.Head; cur != null; cur = cur.Next)
for (var current = this.Head; current != null; current = current.Next)
{
if (sw == null)
if (timestamp is { } startedAt)
{
result = cur.Value.Shutdown() && result;
timeoutMilliseconds = Stopwatch.Remaining(timeoutMilliseconds, startedAt);
}
else
{
var timeout = timeoutMilliseconds - sw.ElapsedMilliseconds;

// notify all the processors, even if we run overtime
result = cur.Value.Shutdown((int)Math.Max(timeout, 0)) && result;
}
// Notify all the processors, even if we run overtime
result = current.Value.Shutdown(timeoutMilliseconds) && result;
}
Comment thread
martincostello marked this conversation as resolved.

return result;
Expand All @@ -159,11 +147,11 @@ protected override void Dispose(bool disposing)
{
if (disposing)
{
for (var cur = this.Head; cur != null; cur = cur.Next)
for (var current = this.Head; current != null; current = current.Next)
{
try
{
cur.Value.Dispose();
current.Value.Dispose();
}
catch (Exception ex)
{
Expand Down
30 changes: 11 additions & 19 deletions src/OpenTelemetry/Internal/BatchExportTaskWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public BatchExportTaskWorker(

/// <inheritdoc/>
public override void Start()
{
this.workerTask = Task.Run(this.ExporterProcAsync);
}
=> this.workerTask = Task.Run(this.ExporterProcAsync);

/// <inheritdoc/>
public override bool TriggerExport()
Expand Down Expand Up @@ -97,6 +95,7 @@ public override bool WaitForExport(int timeoutMilliseconds)
return true;
}

// Otherwise we can just wait for the export to complete synchronously
return this.WaitForExportAsync(timeoutMilliseconds, head).GetAwaiter().GetResult();
}

Expand Down Expand Up @@ -125,12 +124,7 @@ public override bool Shutdown(int timeoutMilliseconds)
return true;
}

if (timeoutMilliseconds == 0)
{
return true;
}

return this.workerTask.Wait(timeoutMilliseconds);
return timeoutMilliseconds == 0 || this.workerTask.Wait(timeoutMilliseconds);
}

/// <inheritdoc/>
Expand All @@ -152,9 +146,7 @@ protected override void Dispose(bool disposing)

private async Task<bool> WaitForExportAsync(int timeoutMilliseconds, long targetHead)
{
var sw = timeoutMilliseconds == Timeout.Infinite
? null
: Stopwatch.StartNew();
long? timestamp = timeoutMilliseconds == Timeout.Infinite ? null : Stopwatch.GetTimestamp();

// There is a chance that the export task finished processing all the data from the queue,
// and signaled before we enter wait here, use polling to prevent being blocked indefinitely.
Expand All @@ -163,15 +155,15 @@ private async Task<bool> WaitForExportAsync(int timeoutMilliseconds, long target
while (true)
{
var timeout = pollingMilliseconds;
if (sw != null)

if (timestamp is { } startedAt)
{
var remaining = timeoutMilliseconds - sw.ElapsedMilliseconds;
if (remaining <= 0)
timeoutMilliseconds = Stopwatch.Remaining(timeoutMilliseconds, startedAt);

if (timeoutMilliseconds <= 0)
{
return this.CircularBuffer.RemovedCount >= targetHead;
}

timeout = Math.Min((int)remaining, pollingMilliseconds);
}

try
Expand All @@ -185,7 +177,7 @@ await Task.WhenAny(
this.dataExportedNotification.Task,
this.shutdownCompletionSource.Task,
Task.Delay(timeout, combinedTokenSource.Token)).ConfigureAwait(false);
Comment thread
martincostello marked this conversation as resolved.
#if NET8_0_OR_GREATER
#if NET
await combinedTokenSource.CancelAsync().ConfigureAwait(false);
#else
combinedTokenSource.Cancel();
Expand Down Expand Up @@ -229,7 +221,7 @@ private async Task ExporterProcAsync()
await Task.WhenAny(
this.exportTrigger.WaitAsync(waitAndDelayCts.Token),
Task.Delay(this.ScheduledDelayMilliseconds, waitAndDelayCts.Token)).ConfigureAwait(false);
#if NET8_0_OR_GREATER
#if NET
await waitAndDelayCts.CancelAsync().ConfigureAwait(false);
#else
waitAndDelayCts.Cancel();
Expand Down
Loading
Loading