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

Race condition in batching leading to lost logs #117

Open
guifel opened this issue Aug 15, 2022 · 1 comment · May be fixed by #168
Open

Race condition in batching leading to lost logs #117

guifel opened this issue Aug 15, 2022 · 1 comment · May be fixed by #168

Comments

@guifel
Copy link

guifel commented Aug 15, 2022

In batching mode, clearBatch is being called after the post request:

logEntry === undefined && this.clearBatch()

Logs that came in while the post request is pending will be lost. clearBatch needs to be called before.

@guifel
Copy link
Author

guifel commented Aug 14, 2023

A year later the issue is still around. This is the patch I have been using ever since:

diff --git a/node_modules/winston-loki/src/batcher.js b/node_modules/winston-loki/src/batcher.js
index 5659320..879ccae 100644
--- a/node_modules/winston-loki/src/batcher.js
+++ b/node_modules/winston-loki/src/batcher.js
@@ -250,17 +250,18 @@ class Batcher {
           }
         }
 
+        const savedBatchStreams = this.batch.streams
+        logEntry === undefined && this.clearBatch()
+
         // Send the data to Grafana Loki
         req.post(this.url, this.contentType, this.options.headers, reqBody, this.options.timeout)
           .then(() => {            
-            // No need to clear the batch if batching is disabled
-            logEntry === undefined && this.clearBatch()
             this.batchSent()
             resolve()
           })
           .catch(err => {
-            // Clear the batch on error if enabled
-            this.options.clearOnError && this.clearBatch()
+            // Revert clear batch for the logs to be sent on the next iteration
+            this.batch.streams = savedBatchStreams
             
             this.options.onConnectionError !== undefined && this.options.onConnectionError(err)
 

guifel added a commit to guifel/winston-loki that referenced this issue Nov 19, 2024
@guifel guifel linked a pull request Nov 19, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant