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

Avoid infinite log resending if exceptions happen during the log-sending process #219

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
39 changes: 36 additions & 3 deletions src/AWS.Logger.Core/Core/AWSLoggerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public AWSLoggerCore(AWSLoggerConfig config, string logType)
{
awsConfig.UseHttp = true;
}
awsConfig.AuthenticationRegion = _config.Region;
}
else
{
Expand Down Expand Up @@ -396,9 +397,30 @@ private async Task Monitor(CancellationToken token)
}
catch (Exception ex)
{
//drop logs in sending batch since those logs may cause exceptions
_repo.Reset(null);
// We don't want to kill the main monitor loop. We will simply log the error, then continue.
// If it is an OperationCancelledException, die
LogLibraryServiceError(ex);
LogLibraryServiceError(new Exception("Logs in the sending batch are dropped because of exceptions", ex));
}
}
}

private void PrepareLogEventBatchForSending()
{
//Make sure the log events are in the right order.
_repo._request.LogEvents.Sort((ev1, ev2) => ev1.Timestamp.CompareTo(ev2.Timestamp));
if (_repo._request.LogEvents.Count > 0)
{
DateTime latestLogDateTime = _repo._request.LogEvents.Last().Timestamp;

//avoid the error that the log events should be in a 24 hours range
//https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs/client/put_log_events.html#put-log-events
while (_repo._request.LogEvents.Count > 0 &&
(latestLogDateTime - _repo._request.LogEvents.First().Timestamp > TimeSpan.FromHours(24))
)
{
_repo.RemoveMessageAt(0);
}
}
}
Expand All @@ -412,8 +434,7 @@ private async Task SendMessages(CancellationToken token)
{
try
{
//Make sure the log events are in the right order.
_repo._request.LogEvents.Sort((ev1, ev2) => ev1.Timestamp.CompareTo(ev2.Timestamp));
PrepareLogEventBatchForSending();
var response = await _client.PutLogEventsAsync(_repo._request, token).ConfigureAwait(false);
_repo.Reset(response.NextSequenceToken);
_requestCount = 5;
Expand Down Expand Up @@ -626,6 +647,18 @@ public void AddMessage(InputLogEvent ev)
_request.LogEvents.Add(ev);
}

public void RemoveMessageAt(int index)
{
if (index < 0 || index >= _request.LogEvents.Count)
{
return;
}
Encoding unicode = Encoding.Unicode;
InputLogEvent ev = _request.LogEvents[index];
_totalMessageSize -= unicode.GetMaxByteCount(ev.Message.Length);
_request.LogEvents.RemoveAt(index);
}

public void Reset(string SeqToken)
{
_request.LogEvents.Clear();
Expand Down