Use batch logic to push logs#129
Conversation
53a63b9 to
ba3ccb1
Compare
Codecov Report
@@ Coverage Diff @@
## main #129 +/- ##
==========================================
- Coverage 63.85% 63.62% -0.24%
==========================================
Files 164 164
Lines 10312 10347 +35
==========================================
- Hits 6585 6583 -2
- Misses 3009 3040 +31
- Partials 718 724 +6
Flags with carried forward coverage won't be shown. Click here to find out more.
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
76852c9 to
be60ffb
Compare
|
|
||
| type Storage interface { | ||
| StoreLog(ctx xcontext.Context, entry Log) error | ||
| StoreLogs(ctx xcontext.Context, batch LogBatch) error |
There was a problem hiding this comment.
why do we need a LogBatch type? Why not to just []Log?
There was a problem hiding this comment.
to make it easy to add any metadata -in the futuer- along with the batch.
There was a problem hiding this comment.
valid point, but what kind of metadata do you expect?
| func (lb *LogBatch) ToStorageBatch() storage.LogBatch { | ||
| var logBatch storage.LogBatch | ||
| for _, log := range lb.Logs { | ||
| logBatch.Logs = append(logBatch.Logs, log.ToStorageLog()) | ||
| } | ||
| return logBatch | ||
| } |
There was a problem hiding this comment.
Quick question: should this function be thread-safe?
There was a problem hiding this comment.
I can't see why, the logBatch will not be shared across threads.
| @@ -169,6 +209,7 @@ func initRouter(ctx xcontext.Context, rh RouteHandler, middlewares []gin.Handler | |||
|
|
|||
| r.GET("/status", rh.status) | |||
| r.POST("/log", rh.addLog) | |||
There was a problem hiding this comment.
I also expected that batching will apply to /log API, so the local server will hold sending the logs further. @mimir-d what do you think?
There was a problem hiding this comment.
I can merge the two endpoints, by always sending an array of logs.
There was a problem hiding this comment.
It is ok to have two separate, but inside use batch for everything
There was a problem hiding this comment.
it's fine to change the endpoint now, but keep in mind that this is a breaking change and wont work after this starts to be used
| MaxBatchSize = 500000 // size in bytes | ||
| MaxBatchLength = 100 | ||
| BatchRetries = 4 | ||
| BatchRetryDelay = 100 * time.Millisecond |
There was a problem hiding this comment.
Having good defaults is great, but I would also add overriding with command line parameters
There was a problem hiding this comment.
yeah, i guess it will be better to add it in another PR.
|
|
||
| func (hh *HttpHook) postBatch() error { | ||
| logs := hh.logBatch | ||
| hh.logBatch = make([]server.Log, 0) |
There was a problem hiding this comment.
in context with retry hh.logBatch will be 0 after first try and if it failed, they will be lost by the time of the second try
| fmt.Fprintf(os.Stderr, "Http Logger Err: %v", err) | ||
| hh.batchMux.Unlock() | ||
| case <-hh.sendTicker.C: | ||
| hh.batchMux.Lock() |
There was a problem hiding this comment.
As far as I understand logHandler works in a single goroutine, so if batch variables are not used elsewhere, having mutexes is not needed.
Also block everything during retries (that have Sleep) is not great, probably a good way is to either launch a goroutine that will try to post or to add retries in the current loop. Probably retrying at the next tick is ok.
6a97c5d to
7f9fa92
Compare
| if len(hh.logBatch) > MaxBatchLength || hh.batchSize > uint64(MaxBatchSize) { | ||
| err := hh.postBatch() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Send Batch faild: %v", err) |
| break | ||
| } | ||
| // if the batch is sent | ||
| hh.logBatch = make([]server.Log, 0) |
There was a problem hiding this comment.
nit
Just hh.logBatch = nil
rihter007
left a comment
There was a problem hiding this comment.
Overall looks good, just left some final comments
7f9fa92 to
f49e533
Compare
mimir-d
left a comment
There was a problem hiding this comment.
the mandatory change is to address non-empty current batch when closing; rest are recommendations
f49e533 to
7e34e60
Compare
Signed-off-by: Mohamed Abokammer <mahmednabil109@gmail.com>
7e34e60 to
5119b40
Compare
|
|
||
| // Batch defines a log batch that handles the size in bytes of the logs | ||
| type Batch struct { | ||
| addr string |
There was a problem hiding this comment.
this isnt realy a property of the batch, but i can ignore this; you shouldve had it as a param in send
Signed-off-by: Mohamed Abokammer mahmednabil109@gmail.com