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
fix: flushing buffer on Timeout
  • Loading branch information
Flash0ver committed Jul 8, 2025
commit d9ae794c1243408fa0d0e58b60e9cde1642ff504
55 changes: 51 additions & 4 deletions src/Sentry/Internal/BatchBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ namespace Sentry.Internal;
/// <para>Requires a minimum capacity of 2.</para>
/// </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>
/// Not all members are thread-safe.
/// See individual members for notes on thread safety.
/// </remarks>
internal sealed class BatchBuffer<T>
{
private readonly T[] _array;
private int _additions;

/// <summary>
/// Create a new buffer.
/// </summary>
/// <param name="capacity">Length of new the buffer.</param>
/// <exception cref="ArgumentOutOfRangeException">When <paramref name="capacity"/> is less than <see langword="2"/>.</exception>
public BatchBuffer(int capacity)
{
ThrowIfLessThanTwo(capacity, nameof(capacity));
Expand All @@ -23,10 +26,39 @@ public BatchBuffer(int capacity)
_additions = 0;
}

/// <summary>
/// Maximum number of elements that can be added to the buffer.
/// </summary>
/// <remarks>
/// This property is thread-safe.
/// </remarks>
internal int Capacity => _array.Length;

/// <summary>
/// Have any elements been added to the buffer?
/// </summary>
/// <remarks>
/// This property is not thread-safe.
/// </remarks>
internal bool IsEmpty => _additions == 0;

/// <summary>
/// Have <see cref="Capacity"/> number of elements been added to the buffer?
/// </summary>
/// <remarks>
/// This property is not thread-safe.
/// </remarks>
internal bool IsFull => _additions >= _array.Length;

/// <summary>
/// Attempt to atomically add one element to the buffer.
/// </summary>
/// <param name="item">Element attempted to be added atomically.</param>
/// <param name="count">When this method returns <see langword="true"/>, is set to the Length at which the <paramref name="item"/> was added at.</param>
/// <returns><see langword="true"/> when <paramref name="item"/> was added atomically; <see langword="false"/> when <paramref name="item"/> was not added.</returns>
/// <remarks>
/// This method is thread-safe.
/// </remarks>
internal bool TryAdd(T item, out int count)
{
count = Interlocked.Increment(ref _additions);
Expand All @@ -40,6 +72,13 @@ internal bool TryAdd(T item, out int count)
return false;
}

/// <summary>
/// Returns a new Array consisting of the elements successfully added.
/// </summary>
/// <returns>An Array with Length of successful additions.</returns>
/// <remarks>
/// This method is not thread-safe.
/// </remarks>
internal T[] ToArrayAndClear()
{
var additions = _additions;
Expand All @@ -51,6 +90,14 @@ internal T[] ToArrayAndClear()
return ToArrayAndClear(length);
}

/// <summary>
/// Returns a new Array consisting of <paramref name="length"/> elements successfully added.
/// </summary>
/// <param name="length">The Length of the buffer a new Array is created from.</param>
/// <returns>An Array with Length of <paramref name="length"/>.</returns>
/// <remarks>
/// This method is not thread-safe.
/// </remarks>
internal T[] ToArrayAndClear(int length)
{
var array = ToArray(length);
Expand Down
12 changes: 10 additions & 2 deletions src/Sentry/Internal/BatchProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private bool TryEnqueue(BatchBuffer<SentryLog> buffer, SentryLog log)
{
if (buffer.TryAdd(log, out var count))
{
if (count == 1) // is first of buffer
if (count == 1) // is first element added to buffer after flushed
{
EnableTimer();
}
Expand All @@ -82,6 +82,14 @@ private bool TryEnqueue(BatchBuffer<SentryLog> buffer, SentryLog log)
return false;
}

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

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

private void Flush(BatchBuffer<SentryLog> buffer, int count)
{
_lastFlush = DateTime.UtcNow;
Expand All @@ -101,7 +109,7 @@ internal void OnIntervalElapsed(object? state)
var newActiveBuffer = ReferenceEquals(currentActiveBuffer, _buffer1) ? _buffer2 : _buffer1;
if (Interlocked.CompareExchange(ref _activeBuffer, newActiveBuffer, currentActiveBuffer) == currentActiveBuffer)
{
Flush(currentActiveBuffer, -1);
Flush(currentActiveBuffer);
}
}
}
Expand Down