Skip to content

Commit

Permalink
sort keys so output is deterministic
Browse files Browse the repository at this point in the history
- separate buildRequest method
- increase timeout for cancel context test
  • Loading branch information
barrettj12 committed Aug 3, 2023
1 parent d06ec67 commit ce15c66
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
43 changes: 27 additions & 16 deletions internals/overlord/logstate/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"net/http"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -70,24 +71,9 @@ func (c *lokiClient) Flush(ctx context.Context) error {
if c.numEntries() == 0 {
return nil // no-op
}

defer c.emptyBuffer()

// Build request
req := lokiRequest{}
for service, entries := range c.entries {
if len(entries) == 0 {
continue
}
stream := lokiStream{
Labels: map[string]string{
"pebble_service": service,
},
Entries: entries,
}
req.Streams = append(req.Streams, stream)
}

req := c.buildRequest()
jsonReq, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("encoding request to JSON: %v", err)
Expand Down Expand Up @@ -119,6 +105,31 @@ func (c *lokiClient) Flush(ctx context.Context) error {
return nil
}

func (c *lokiClient) buildRequest() (req lokiRequest) {
// Sort keys to guarantee deterministic output
var services []string
for svc, entries := range c.entries {
if len(entries) == 0 {
delete(c.entries, svc)
continue
}
services = append(services, svc)
}
sort.Strings(services)

for _, service := range services {
entries := c.entries[service]
stream := lokiStream{
Labels: map[string]string{
"pebble_service": service,
},
Entries: entries,
}
req.Streams = append(req.Streams, stream)
}
return
}

func (c *lokiClient) emptyBuffer() {
for svc := range c.entries {
c.entries[svc] = c.entries[svc][:0]
Expand Down
4 changes: 2 additions & 2 deletions internals/overlord/logstate/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (*lokiSuite) TestLokiFlushCancelContext(c *C) {
c.Assert(err, IsNil)

// Cancel the Flush context quickly
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
defer cancel()

flushReturned := make(chan struct{})
Expand All @@ -157,7 +157,7 @@ func (*lokiSuite) TestLokiFlushCancelContext(c *C) {
// Check Flush returns quickly after context timeout
select {
case <-flushReturned:
case <-time.After(2 * time.Millisecond):
case <-time.After(20 * time.Millisecond):
c.Fatal("lokiClient.Flush took too long to return after context timeout")
}
}
Expand Down

0 comments on commit ce15c66

Please sign in to comment.