Skip to content

Commit

Permalink
calculate num of buffered entries correctly
Browse files Browse the repository at this point in the history
- clear buffer after flush
  • Loading branch information
barrettj12 committed Aug 2, 2023
1 parent d8bc188 commit 22a6968
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions internals/overlord/logstate/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
Expand All @@ -46,12 +45,20 @@ func newLokiClient(target *plan.LogTarget) logClient {

func (c *lokiClient) Write(ctx context.Context, entry servicelog.Entry) error {
c.entries[entry.Service] = append(c.entries[entry.Service], asLokiEntry(entry))
if len(c.entries) >= 10 {
if c.numEntries() >= 10 {
return c.Flush(ctx)
}
return nil
}

func (c *lokiClient) numEntries() int {
sum := 0
for _, l := range c.entries {
sum += len(l)
}
return sum
}

func asLokiEntry(entry servicelog.Entry) lokiEntry {
return lokiEntry{
strconv.FormatInt(entry.Time.UnixNano(), 10),
Expand All @@ -61,7 +68,9 @@ func asLokiEntry(entry servicelog.Entry) lokiEntry {

func (c *lokiClient) Flush(ctx context.Context) error {
defer func() {
// TODO: clear entries
for svc := range c.entries {
c.entries[svc] = c.entries[svc][:0]
}
}()

// Build request
Expand Down Expand Up @@ -96,7 +105,7 @@ func (c *lokiClient) Flush(ctx context.Context) error {
// Check response status code to see if it was successful
if sc := resp.StatusCode; sc < 200 || sc >= 300 {
// Request to Loki failed
b, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1024))
b, err := io.ReadAll(io.LimitReader(resp.Body, 1024))
if err != nil {
b = append(b, []byte("//couldn't read response body: ")...)
b = append(b, []byte(err.Error())...)
Expand Down

0 comments on commit 22a6968

Please sign in to comment.