Skip to content

Commit

Permalink
Merge pull request #143 from VadymLevkovskyi/remove_sequence_token
Browse files Browse the repository at this point in the history
Removed sequenceToken from PeriodicBatchingSinkImplementationCallback
  • Loading branch information
wparad authored Jul 23, 2024
2 parents c97a866 + 94d4c8d commit 9a5e6da
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 238 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
env:
NUGET_KEY: ${{ secrets.NUGET_KEY }}
run: |
buildNumber="4.2.${GITHUB_RUN_NUMBER}"
buildNumber="4.3.${GITHUB_RUN_NUMBER}"
sed "s/0.0.1/${buildNumber}/g" src/Serilog.Sinks.AwsCloudWatch/*.csproj -i
dotnet pack -c Release -o artifacts || exit 1
dotnet nuget push artifacts/Serilog.Sinks.AwsCloudWatch.${buildNumber}.nupkg -s "https://api.nuget.org/v3/index.json" -k "$NUGET_KEY"
Expand Down
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 @@ -254,40 +234,6 @@ private async Task PublishBatchAsync(List<InputLogEvent> batch)
await CreateLogGroupAsync();
await CreateLogStreamAsync();
}
catch (DataAlreadyAcceptedException e)
{
Debugging.SelfLog.WriteLine("Data already accepted. Attempt: {0} Error: {1}", attemptIndex, e);
try
{
await UpdateLogStreamSequenceTokenAsync();
}
catch (Exception ex)
{
Debugging.SelfLog.WriteLine("Unable to update log stream sequence. Attempt: {0} Error: {1}", attemptIndex, ex);

// try again with a different log stream
UpdateLogStreamName();
await CreateLogStreamAsync();
}
attemptIndex++;
}
catch (InvalidSequenceTokenException e)
{
Debugging.SelfLog.WriteLine("Invalid sequence token. Attempt: {0} Error: {1}", attemptIndex, e);
try
{
await UpdateLogStreamSequenceTokenAsync();
}
catch (Exception ex)
{
Debugging.SelfLog.WriteLine("Unable to update log stream sequence. Attempt: {0} Error: {1}", attemptIndex, ex);

// try again with a different log stream
UpdateLogStreamName();
await CreateLogStreamAsync();
}
attemptIndex++;
}
catch (Exception e)
{
Debugging.SelfLog.WriteLine("Unhandled exception. Error: {0}", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>A Serilog sink that logs to AWS CloudWatch</Description>
<AssemblyTitle>AWS Cloud Watch Serilog Sink</AssemblyTitle>
Expand Down Expand Up @@ -28,7 +28,7 @@
<None Include="../../readme.md" pack="true" PackagePath="." />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.CloudWatchLogs" Version="[3.7.1,)" />
<PackageReference Include="AWSSDK.CloudWatchLogs" Version="[3.7.102.27,)" />
<!-- This has to be here to prevent breaking Serilog.Formatting.Json.JSonFormatter, see https://github.com/Cimpress-MCP/serilog-sinks-awscloudwatch/issues/131 -->
<PackageReference Include="Serilog" Version="[3.0.0,)" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="[3.1.0,)" />
Expand Down
Loading

0 comments on commit 9a5e6da

Please sign in to comment.