Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d24d165
feat: add Batch Processor for Logs
Flash0ver Jun 26, 2025
aad0599
test: Batch Processor for Logs
Flash0ver Jun 26, 2025
76fcc1b
docs: Batch Processor for Logs
Flash0ver Jun 26, 2025
2ad33f6
test: fix unavailable API on TargetFramework=net48
Flash0ver Jun 27, 2025
38e1c04
test: run all Logs tests on full framework
Flash0ver Jun 27, 2025
f7a43b8
ref: remove usage of System.Threading.Lock
Flash0ver Jun 27, 2025
e6b0b74
ref: rename members for clarity
Flash0ver Jun 30, 2025
a84b78f
Merge branch 'feat/logs' into feat/logs-buffering
Flash0ver Jun 30, 2025
53c90ea
ref: delete Timer-Abstraction and change to System.Threading.Timer
Flash0ver Jul 1, 2025
6580632
ref: delete .ctor only called from tests
Flash0ver Jul 1, 2025
6e2ee9b
ref: switch Buffer-Processor to be lock-free but discarding
Flash0ver Jul 2, 2025
0774709
test: fix BatchBuffer and Tests
Flash0ver Jul 3, 2025
d9ae794
fix: flushing buffer on Timeout
Flash0ver Jul 8, 2025
7e1f5ea
feat: add Backpressure-ClientReport
Flash0ver Jul 10, 2025
365a2fb
ref: make BatchProcessor more resilient
Flash0ver Jul 11, 2025
c478391
Format code
getsentry-bot Jul 11, 2025
211beea
Merge branch 'feat/logs' into feat/logs-buffering
Flash0ver Jul 11, 2025
c699c2d
test: fix on .NET Framework
Flash0ver Jul 11, 2025
b21b537
fix: BatchBuffer flushed on Shutdown/Dispose
Flash0ver Jul 14, 2025
e8850db
ref: minimize locking
Flash0ver Jul 22, 2025
57f9ccc
Merge branch 'feat/logs' into feat/logs-buffering
Flash0ver Jul 22, 2025
c63bc53
ref: rename BatchProcessor to StructuredLogBatchProcessor
Flash0ver Jul 22, 2025
f28cc6d
ref: rename BatchBuffer to StructuredLogBatchBuffer
Flash0ver Jul 22, 2025
79ce02e
ref: remove internal options
Flash0ver Jul 23, 2025
0702796
test: ref
Flash0ver Jul 23, 2025
4e5f097
perf: update Benchmark result
Flash0ver Jul 23, 2025
1276725
ref: make SentryStructuredLogger. Flush abstract
Flash0ver Jul 24, 2025
3816fab
ref: guard an invariant of the Flush-Scope
Flash0ver Jul 24, 2025
28b6654
ref: remove unused values
Flash0ver Jul 24, 2025
1eef330
docs: improve comments
Flash0ver Jul 24, 2025
d72ca5c
perf: update Benchmark after signature change
Flash0ver Jul 25, 2025
49fefc1
ref: discard logs gracefully when Hub is (being) disposed
Flash0ver Jul 28, 2025
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
Prev Previous commit
Next Next commit
ref: switch Buffer-Processor to be lock-free but discarding
  • Loading branch information
Flash0ver committed Jul 2, 2025
commit 6e2ee9b7252913c294beba206cbe4db69b5f4849
73 changes: 42 additions & 31 deletions src/Sentry/Internal/BatchBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,88 @@ namespace Sentry.Internal;
/// A slim wrapper over an <see cref="System.Array"/>,
/// intended for buffering.
/// </summary>
/// <remarks>
/// <para><see cref="Capacity"/> is thread-safe.</para>
/// <para><see cref="TryAdd(T, out int)"/> is thread-safe.</para>
/// <para><see cref="ToArrayAndClear()"/> is not thread-safe.</para>
/// <para><see cref="ToArrayAndClear(int)"/> is not thread-safe.</para>
/// </remarks>
internal sealed class BatchBuffer<T>
{
private readonly T[] _array;
private int _count;
private int _additions;

public BatchBuffer(int capacity)
{
ThrowIfNegativeOrZero(capacity, nameof(capacity));
ThrowIfLessThanTwo(capacity, nameof(capacity));

_array = new T[capacity];
_count = 0;
_additions = 0;
}

internal int Count => _count;
//internal int Count => _count;
internal int Capacity => _array.Length;
internal bool IsEmpty => _count == 0 && _array.Length != 0;
internal bool IsFull => _count == _array.Length;
internal bool IsEmpty => _additions == 0;
internal bool IsFull => _additions >= _array.Length;

internal bool TryAdd(T item)
internal bool TryAdd(T item, out int count)
{
if (_count < _array.Length)
count = Interlocked.Increment(ref _additions);

if (count <= _array.Length)
{
_array[_count] = item;
_count++;
_array[count - 1] = item;
return true;
}

return false;
}

internal T[] ToArray()
internal T[] ToArrayAndClear()
{
return ToArrayAndClear(_additions);
}

internal T[] ToArrayAndClear(int length)
{
var array = ToArray(length);
Clear(length);
return array;
}

private T[] ToArray(int length)
{
if (_count == 0)
if (length == 0)
{
return Array.Empty<T>();
}

var array = new T[_count];
Array.Copy(_array, array, _count);
var array = new T[length];
Array.Copy(_array, array, length);
return array;
}

internal void Clear()
private void Clear(int length)
{
if (_count == 0)
if (length == 0)
{
return;
}

var count = _count;
_count = 0;
Array.Clear(_array, 0, count);
}

internal T[] ToArrayAndClear()
{
var array = ToArray();
Clear();
return array;
_additions = 0;
Array.Clear(_array, 0, length);
}

private static void ThrowIfNegativeOrZero(int capacity, string paramName)
private static void ThrowIfLessThanTwo(int capacity, string paramName)
{
if (capacity <= 0)
if (capacity < 2)
{
ThrowNegativeOrZero(capacity, paramName);
ThrowLessThanTwo(capacity, paramName);
}
}

private static void ThrowNegativeOrZero(int capacity, string paramName)
private static void ThrowLessThanTwo(int capacity, string paramName)
{
throw new ArgumentOutOfRangeException(paramName, capacity, "Argument must neither be negative nor zero.");
throw new ArgumentOutOfRangeException(paramName, capacity, "Argument must be at least two.");
}
}
73 changes: 50 additions & 23 deletions src/Sentry/Internal/BatchProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ internal sealed class BatchProcessor : IDisposable
private readonly IHub _hub;
private readonly TimeSpan _batchInterval;
private readonly IDiagnosticLogger? _diagnosticLogger;

private readonly Timer _timer;
private readonly BatchBuffer<SentryLog> _logs;
private readonly object _lock;
private readonly object _timerCallbackLock;
private readonly BatchBuffer<SentryLog> _buffer1;
private readonly BatchBuffer<SentryLog> _buffer2;
private volatile BatchBuffer<SentryLog> _activeBuffer;

private DateTime _lastFlush = DateTime.MinValue;

Expand All @@ -31,51 +34,75 @@ public BatchProcessor(IHub hub, int batchCount, TimeSpan batchInterval, IDiagnos
_diagnosticLogger = diagnosticLogger;

_timer = new Timer(OnIntervalElapsed, this, Timeout.Infinite, Timeout.Infinite);
_timerCallbackLock = new object();

_logs = new BatchBuffer<SentryLog>(batchCount);
_lock = new object();
_buffer1 = new BatchBuffer<SentryLog>(batchCount);
_buffer2 = new BatchBuffer<SentryLog>(batchCount);
_activeBuffer = _buffer1;
}

internal void Enqueue(SentryLog log)
{
lock (_lock)
var activeBuffer = _activeBuffer;

if (!TryEnqueue(activeBuffer, log))
{
EnqueueCore(log);
activeBuffer = activeBuffer == _buffer1 ? _buffer2 : _buffer1;
if (!TryEnqueue(activeBuffer, log))
{
_diagnosticLogger?.LogInfo("Log Buffer full ... dropping log");
}
}
}

private void EnqueueCore(SentryLog log)
private bool TryEnqueue(BatchBuffer<SentryLog> buffer, SentryLog log)
{
var isFirstLog = _logs.IsEmpty;
var added = _logs.TryAdd(log);
Debug.Assert(added, $"Since we currently have no lock-free scenario, it's unexpected to exceed the {nameof(BatchBuffer<SentryLog>)}'s capacity.");

if (isFirstLog && !_logs.IsFull)
{
EnableTimer();
}
else if (_logs.IsFull)
if (buffer.TryAdd(log, out var count))
{
DisableTimer();
Flush();
if (count == 1) // is first of buffer
{
EnableTimer();
}

if (count == buffer.Capacity) // is buffer full
{
// swap active buffer atomically
var currentActiveBuffer = _activeBuffer;
var newActiveBuffer = ReferenceEquals(currentActiveBuffer, _buffer1) ? _buffer2 : _buffer1;
if (Interlocked.CompareExchange(ref _activeBuffer, newActiveBuffer, currentActiveBuffer) == currentActiveBuffer)
{
DisableTimer();
Flush(buffer, count);
}
}

return true;
}

return false;
}

private void Flush()
private void Flush(BatchBuffer<SentryLog> buffer, int count)
{
_lastFlush = DateTime.UtcNow;

var logs = _logs.ToArrayAndClear();
var logs = buffer.ToArrayAndClear(count);
_ = _hub.CaptureEnvelope(Envelope.FromLog(new StructuredLog(logs)));
}

internal void OnIntervalElapsed(object? state)
{
lock (_lock)
lock (_timerCallbackLock)
{
if (!_logs.IsEmpty && DateTime.UtcNow > _lastFlush)
var currentActiveBuffer = _activeBuffer;

if (!currentActiveBuffer.IsEmpty && DateTime.UtcNow > _lastFlush)
{
Flush();
var newActiveBuffer = ReferenceEquals(currentActiveBuffer, _buffer1) ? _buffer2 : _buffer1;
if (Interlocked.CompareExchange(ref _activeBuffer, newActiveBuffer, currentActiveBuffer) == currentActiveBuffer)
{
Flush(currentActiveBuffer, -1);
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/Sentry.Tests/Internals/BatchProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ public void Enqueue_BothTimeoutAndSizeReached_CaptureEnvelopes()
AssertEnvelopes(["one"], ["two", "three"]);
}

[Fact]
public async Task Enqueue_Concurrency_CaptureEnvelopes()
{
using var processor = new BatchProcessor(_hub, 100, Timeout.InfiniteTimeSpan, _diagnosticLogger);
using var sync = new ManualResetEvent(false);

var tasks = new Task[5];
for (var i = 0; i < tasks.Length; i++)
{
tasks[i] = Task.Factory.StartNew(static state =>
{
var (sync, taskIndex, processor) = ((ManualResetEvent, int, BatchProcessor))state!;
sync.WaitOne(5_000);
for (var i = 0; i < 100; i++)
{
processor.Enqueue(CreateLog($"{taskIndex}-{i}"));
}
}, (sync, i, processor));
}

sync.Set();
await Task.WhenAll(tasks);

AssertCaptureEnvelope(5 * 100 / 100);
}

private static SentryLog CreateLog(string message)
{
return new SentryLog(DateTimeOffset.MinValue, SentryId.Empty, SentryLogLevel.Trace, message);
Expand Down
Loading