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

Removed sequenceToken from PeriodicBatchingSinkImplementationCallback #143

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Threading.Tasks;
using Amazon.CloudWatchLogs;
using Amazon.CloudWatchLogs.Model;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.PeriodicBatching;
using LogEvent = Serilog.Events.LogEvent;
Expand All @@ -20,7 +19,6 @@ internal class PeriodicBatchingSinkImplementationCallback: IBatchedLogEventSink
private readonly ICloudWatchSinkOptions options;
private bool hasInit;
private string logStreamName;
private string nextSequenceToken;
private readonly ITextFormatter textFormatter;

private readonly SemaphoreSlim syncObject = new SemaphoreSlim(1);
Expand Down Expand Up @@ -108,7 +106,6 @@ private async Task CreateLogGroupAsync()
private void UpdateLogStreamName()
{
logStreamName = options.LogStreamNameProvider.GetLogStreamName();
nextSequenceToken = null; // always reset on a new stream
}

/// <summary>
Expand All @@ -129,19 +126,6 @@ private async Task CreateLogStreamAsync()
};
var createLogStreamResponse = await cloudWatchClient.CreateLogStreamAsync(createLogStreamRequest);
}
else
{
nextSequenceToken = logStream.UploadSequenceToken;
}
}

/// <summary>
/// Updates the log stream sequence token.
/// </summary>
private async Task UpdateLogStreamSequenceTokenAsync()
{
var logStream = await GetLogStreamAsync();
nextSequenceToken = logStream?.UploadSequenceToken;
}

/// <summary>
Expand Down Expand Up @@ -226,16 +210,12 @@ private async Task PublishBatchAsync(List<InputLogEvent> batch)
{
LogGroupName = options.LogGroupName,
LogStreamName = logStreamName,
SequenceToken = nextSequenceToken,
LogEvents = batch
};

// actually upload the event to CloudWatch
var putLogEventsResponse = await cloudWatchClient.PutLogEventsAsync(putLogEventsRequest);

// remember the next sequence token, which is required
nextSequenceToken = putLogEventsResponse.NextSequenceToken;

success = true;
}
catch (ServiceUnavailableException e)
Expand All @@ -259,7 +239,7 @@ private async Task PublishBatchAsync(List<InputLogEvent> batch)
Debugging.SelfLog.WriteLine("Data already accepted. Attempt: {0} Error: {1}", attemptIndex, e);
try
{
await UpdateLogStreamSequenceTokenAsync();
await GetLogStreamAsync();
}
catch (Exception ex)
{
Expand All @@ -276,7 +256,7 @@ private async Task PublishBatchAsync(List<InputLogEvent> batch)
Debugging.SelfLog.WriteLine("Invalid sequence token. Attempt: {0} Error: {1}", attemptIndex, e);
try
{
await UpdateLogStreamSequenceTokenAsync();
await GetLogStreamAsync();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,21 +379,13 @@ public async Task MultipleDays()

Assert.Equal(options.LogGroupName, request.LogGroupName);
Assert.Equal(events.Length / putLogEventsCalls.Count, request.LogEvents.Count);
Assert.Null(request.SequenceToken);

// make sure the events are ordered
for (var index = 1; index < call.Request.LogEvents.Count; index++)
{
Assert.True(call.Request.LogEvents.ElementAt(index).Timestamp >= call.Request.LogEvents.ElementAt(index - 1).Timestamp);
}

if (i == 0) // first call
{
Assert.Null(request.SequenceToken);
}
else
{
Assert.NotNull(request.SequenceToken);
}
}

client.VerifyAll();
Expand Down Expand Up @@ -450,15 +442,14 @@ public async Task MoreThanMaxBatchCount()
var request = call.Request;

Assert.Equal(options.LogGroupName, request.LogGroupName);
Assert.Null(request.SequenceToken);

if (i == 0) // first call
{
Assert.Null(request.SequenceToken);
Assert.Equal(CloudWatchLogSink.MaxLogEventBatchCount, request.LogEvents.Count);
}
else
{
Assert.NotNull(request.SequenceToken);
Assert.Single(request.LogEvents);
}
}
Expand Down Expand Up @@ -517,15 +508,14 @@ public async Task MoreThanMaxBatchSize()
var request = call.Request;

Assert.Equal(options.LogGroupName, request.LogGroupName);
Assert.Null(request.SequenceToken);

if (i == 0) // first call
{
Assert.Null(request.SequenceToken);
Assert.Equal(203, request.LogEvents.Count); // expect 203 of the 256 messages in the first batch
}
else
{
Assert.NotNull(request.SequenceToken);
Assert.Equal(53, request.LogEvents.Count); // expect 53 of the 256 messages in the second batch
}
}
Expand Down