Skip to content

Commit

Permalink
pkg/auth/badgerauth: fix incorrect logging of clocks
Browse files Browse the repository at this point in the history
Clocks were logged in random order. This change fixes this.

Closes #352

Change-Id: I321ef03565717a0850b790cc70a7d2c38fbe8470
  • Loading branch information
amwolff committed Jul 26, 2023
1 parent 168fb47 commit c891d95
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
19 changes: 7 additions & 12 deletions pkg/auth/badgerauth/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,18 +675,13 @@ func IgnoreDialFailures(err error) error {
type clocksLogObject map[string]uint64

func (o clocksLogObject) MarshalLogObject(enc zapcore.ObjectEncoder) error {
var (
ids []string
vals []uint64
)
for id, val := range o {
ids, vals = append(ids, id), append(vals, val)
}
f := func(i, j int) bool { return ids[i] < ids[j] }
sort.Slice(ids, f)
sort.Slice(vals, f)
for i, id := range ids {
enc.AddUint64(id, vals[i])
var ids []string
for id := range o {
ids = append(ids, id)
}
sort.Strings(ids)
for _, id := range ids {
enc.AddUint64(id, o[id])
}
return nil
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/auth/badgerauth/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"

"storj.io/common/testcontext"
"storj.io/gateway-mt/pkg/auth/badgerauth/pb"
)

Expand Down Expand Up @@ -64,3 +68,37 @@ func TestRecordsEqual(t *testing.T) {
r2.ExpiresAtUnix = time.Now().Unix()
assert.False(t, recordsEqual(&r1, &r2))
}

func TestMarshalLogObjectCorrectness(t *testing.T) {
t.Parallel()

ctx := testcontext.New(t)
defer ctx.Cleanup()

observedZapCore, observedLogs := observer.New(zap.InfoLevel)
observedLogger := zap.New(observedZapCore)
defer ctx.Check(observedLogger.Sync)

clocks := make(clocksLogObject)
clocks["app11"] = 123
clocks["app12"] = 456
clocks["app21"] = 789
clocks["app22"] = 999999
clocks["app31"] = 0

observedLogger.Info("test", zap.Object("clocks", clocks))

filter := observedLogs.FilterLevelExact(zap.InfoLevel)
filter = filter.FilterMessage("test")
filter = filter.FilterFieldKey("clocks")

require.Equal(t, 1, filter.Len())

assert.EqualValues(t, map[string]uint64{
"app11": 123,
"app12": 456,
"app21": 789,
"app22": 999999,
"app31": 0,
}, filter.All()[0].Context[0].Interface)
}

0 comments on commit c891d95

Please sign in to comment.