From f9d7c8fd484efb48df636753833b5d13686f932f Mon Sep 17 00:00:00 2001 From: amidofu Date: Wed, 10 May 2023 14:26:10 +0800 Subject: [PATCH 1/2] set auth region when service url is used --- src/AWS.Logger.Core/Core/AWSLoggerCore.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/AWS.Logger.Core/Core/AWSLoggerCore.cs b/src/AWS.Logger.Core/Core/AWSLoggerCore.cs index c696f72..499a054 100644 --- a/src/AWS.Logger.Core/Core/AWSLoggerCore.cs +++ b/src/AWS.Logger.Core/Core/AWSLoggerCore.cs @@ -92,6 +92,7 @@ public AWSLoggerCore(AWSLoggerConfig config, string logType) { awsConfig.UseHttp = true; } + awsConfig.AuthenticationRegion = _config.Region; } else { From 2edc6c5c013bda9b159b25d786bc7e4db5fba317 Mon Sep 17 00:00:00 2001 From: amidofu Date: Thu, 11 May 2023 18:32:00 +0900 Subject: [PATCH 2/2] do not resend logs that cause exceptions (#1) * drop logs if exceptions happen * avoid 24 hours rule * remove test code * combine exceptions --- src/AWS.Logger.Core/Core/AWSLoggerCore.cs | 38 +++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/AWS.Logger.Core/Core/AWSLoggerCore.cs b/src/AWS.Logger.Core/Core/AWSLoggerCore.cs index 499a054..f5aecb7 100644 --- a/src/AWS.Logger.Core/Core/AWSLoggerCore.cs +++ b/src/AWS.Logger.Core/Core/AWSLoggerCore.cs @@ -397,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); } } } @@ -413,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; @@ -627,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();