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: delete Timer-Abstraction and change to System.Threading.Timer
  • Loading branch information
Flash0ver committed Jul 1, 2025
commit 53c90ea23e5eb003ce2fdb6cdc5abd26d18f203e
42 changes: 24 additions & 18 deletions src/Sentry/Internal/BatchProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Timers;
using Sentry.Extensibility;
using Sentry.Protocol;
using Sentry.Protocol.Envelopes;

Expand All @@ -16,23 +16,21 @@ namespace Sentry.Internal;
internal sealed class BatchProcessor : IDisposable
{
private readonly IHub _hub;
private readonly BatchProcessorTimer _timer;
private readonly TimeSpan _batchInterval;
private readonly IDiagnosticLogger? _diagnosticLogger;
private readonly Timer _timer;
private readonly BatchBuffer<SentryLog> _logs;
private readonly object _lock;

private DateTime _lastFlush = DateTime.MinValue;

public BatchProcessor(IHub hub, int batchCount, TimeSpan batchInterval)
: this(hub, batchCount, new TimersBatchProcessorTimer(batchInterval))
{
}

public BatchProcessor(IHub hub, int batchCount, BatchProcessorTimer timer)
public BatchProcessor(IHub hub, int batchCount, TimeSpan batchInterval, IDiagnosticLogger? diagnosticLogger)
{
_hub = hub;
_batchInterval = batchInterval;
_diagnosticLogger = diagnosticLogger;

_timer = timer;
_timer.Elapsed += OnIntervalElapsed;
_timer = new Timer(OnIntervalElapsed, this, Timeout.Infinite, Timeout.Infinite);

_logs = new BatchBuffer<SentryLog>(batchCount);
_lock = new object();
Expand All @@ -54,11 +52,11 @@ private void EnqueueCore(SentryLog log)

if (isFirstLog && !_logs.IsFull)
{
_timer.Enabled = true;
EnableTimer();
}
else if (_logs.IsFull)
{
_timer.Enabled = false;
DisableTimer();
Flush();
}
}
Expand All @@ -71,23 +69,31 @@ private void Flush()
_ = _hub.CaptureEnvelope(Envelope.FromLog(new StructuredLog(logs)));
}

private void OnIntervalElapsed(object? sender, ElapsedEventArgs e)
internal void OnIntervalElapsed(object? state)
{
_timer.Enabled = false;

lock (_lock)
{
if (!_logs.IsEmpty && e.SignalTime > _lastFlush)
if (!_logs.IsEmpty && DateTime.UtcNow > _lastFlush)
{
Flush();
}
}
}

private void EnableTimer()
{
var updated = _timer.Change(_batchInterval, Timeout.InfiniteTimeSpan);
Debug.Assert(updated, "Timer was not successfully enabled.");
}

private void DisableTimer()
{
var updated = _timer.Change(Timeout.Infinite, Timeout.Infinite);
Debug.Assert(updated, "Timer was not successfully disabled.");
}

public void Dispose()
{
_timer.Enabled = false;
_timer.Elapsed -= OnIntervalElapsed;
_timer.Dispose();
}
}
61 changes: 0 additions & 61 deletions src/Sentry/Internal/BatchProcessorTimer.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Sentry/Internal/DefaultSentryStructuredLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal DefaultSentryStructuredLogger(IHub hub, SentryOptions options, ISystemC
_options = options;
_clock = clock;

_batchProcessor = new BatchProcessor(hub, ClampBatchCount(options.Experimental.InternalBatchSize), ClampBatchInterval(options.Experimental.InternalBatchTimeout));
_batchProcessor = new BatchProcessor(hub, ClampBatchCount(options.Experimental.InternalBatchSize), ClampBatchInterval(options.Experimental.InternalBatchTimeout), _options.DiagnosticLogger);
}

private static int ClampBatchCount(int batchCount)
Expand Down
53 changes: 18 additions & 35 deletions test/Sentry.Tests/Internals/BatchProcessorTests.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
#nullable enable

using System.Timers;

namespace Sentry.Tests.Internals;

public class BatchProcessorTests
public class BatchProcessorTests : IDisposable
{
private readonly IHub _hub;
private readonly FakeBatchProcessorTimer _timer;
private readonly InMemoryDiagnosticLogger _diagnosticLogger;
private readonly List<Envelope> _envelopes;

public BatchProcessorTests()
{
_hub = Substitute.For<IHub>();
_timer = new FakeBatchProcessorTimer();
_diagnosticLogger = new InMemoryDiagnosticLogger();

_envelopes = [];
_hub.CaptureEnvelope(Arg.Do<Envelope>(arg => _envelopes.Add(arg)));
}

[Theory]
[Theory(Skip = "May no longer be required after feedback.")]
[InlineData(-1)]
[InlineData(0)]
public void Ctor_CountOutOfRange_Throws(int count)
{
var ctor = () => new BatchProcessor(_hub, count, TimeSpan.FromMilliseconds(10));
var ctor = () => new BatchProcessor(_hub, count, TimeSpan.FromMilliseconds(10), _diagnosticLogger);

Assert.Throws<ArgumentOutOfRangeException>(ctor);
}

[Theory]
[Theory(Skip = "May no longer be required after feedback.")]
[InlineData(-1)]
[InlineData(0)]
[InlineData(int.MaxValue + 1.0)]
public void Ctor_IntervalOutOfRange_Throws(double interval)
{
var ctor = () => new BatchProcessor(_hub, 1, TimeSpan.FromMilliseconds(interval));
var ctor = () => new BatchProcessor(_hub, 1, TimeSpan.FromMilliseconds(interval), _diagnosticLogger);

Assert.Throws<ArgumentException>(ctor);
}

[Fact]
public void Enqueue_NeitherSizeNorTimeoutReached_DoesNotCaptureEnvelope()
{
using var processor = new BatchProcessor(_hub, 2, _timer);
using var processor = new BatchProcessor(_hub, 2, Timeout.InfiniteTimeSpan, _diagnosticLogger);

processor.Enqueue(CreateLog("one"));

Expand All @@ -54,7 +52,7 @@ public void Enqueue_NeitherSizeNorTimeoutReached_DoesNotCaptureEnvelope()
[Fact]
public void Enqueue_SizeReached_CaptureEnvelope()
{
using var processor = new BatchProcessor(_hub, 2, _timer);
using var processor = new BatchProcessor(_hub, 2, Timeout.InfiniteTimeSpan, _diagnosticLogger);

processor.Enqueue(CreateLog("one"));
processor.Enqueue(CreateLog("two"));
Expand All @@ -66,11 +64,11 @@ public void Enqueue_SizeReached_CaptureEnvelope()
[Fact]
public void Enqueue_TimeoutReached_CaptureEnvelope()
{
using var processor = new BatchProcessor(_hub, 2, _timer);
using var processor = new BatchProcessor(_hub, 2, Timeout.InfiniteTimeSpan, _diagnosticLogger);

processor.Enqueue(CreateLog("one"));

_timer.InvokeElapsed(DateTime.Now);
processor.OnIntervalElapsed(null);

AssertCaptureEnvelope(1);
AssertEnvelope("one");
Expand All @@ -79,11 +77,11 @@ public void Enqueue_TimeoutReached_CaptureEnvelope()
[Fact]
public void Enqueue_BothSizeAndTimeoutReached_CaptureEnvelopeOnce()
{
using var processor = new BatchProcessor(_hub, 2, _timer);
using var processor = new BatchProcessor(_hub, 2, Timeout.InfiniteTimeSpan, _diagnosticLogger);

processor.Enqueue(CreateLog("one"));
processor.Enqueue(CreateLog("two"));
_timer.InvokeElapsed(DateTime.Now);
processor.OnIntervalElapsed(null);

AssertCaptureEnvelope(1);
AssertEnvelope("one", "two");
Expand All @@ -92,11 +90,11 @@ public void Enqueue_BothSizeAndTimeoutReached_CaptureEnvelopeOnce()
[Fact]
public void Enqueue_BothTimeoutAndSizeReached_CaptureEnvelopes()
{
using var processor = new BatchProcessor(_hub, 2, _timer);
using var processor = new BatchProcessor(_hub, 2, Timeout.InfiniteTimeSpan, _diagnosticLogger);

_timer.InvokeElapsed(DateTime.Now);
processor.OnIntervalElapsed(null);
processor.Enqueue(CreateLog("one"));
_timer.InvokeElapsed(DateTime.Now);
processor.OnIntervalElapsed(null);
processor.Enqueue(CreateLog("two"));
processor.Enqueue(CreateLog("three"));

Expand Down Expand Up @@ -149,24 +147,9 @@ private static void AssertEnvelope(Envelope envelope, string[] expected)
Assert.NotNull(log);
Assert.Equal(expected, log.Items.ToArray().Select(static item => item.Message));
}
}

internal sealed class FakeBatchProcessorTimer : BatchProcessorTimer
{
public override bool Enabled { get; set; }

public override event EventHandler<ElapsedEventArgs> Elapsed = null!;

internal void InvokeElapsed(DateTime signalTime)
public void Dispose()
{
#if NET9_0_OR_GREATER
var e = new ElapsedEventArgs(signalTime);
#else
var type = typeof(ElapsedEventArgs);
var ctor = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance;
var instance = Activator.CreateInstance(type, ctor, null, [signalTime], null);
var e = (ElapsedEventArgs)instance!;
#endif
Elapsed.Invoke(this, e);
Assert.Empty(_diagnosticLogger.Entries);
}
}
2 changes: 1 addition & 1 deletion test/Sentry.Tests/SentryStructuredLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void Log_InvalidBeforeSendLog_DoesNotCaptureEnvelope()
entry.Args.Should().BeEmpty();
}

[Fact]
[Fact(Skip = "May no longer be required after feedback.")]
public void Dispose_Log_Throws()
{
_fixture.Options.Experimental.EnableLogs = true;
Expand Down
Loading