Skip to content

Commit

Permalink
fix: datarace in zerolog to logr.LogSink wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ngergs committed Feb 9, 2024
1 parent c71fe95 commit 9621201
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
23 changes: 19 additions & 4 deletions cmd/ingress/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/go-logr/logr"
"github.com/rs/zerolog"
"maps"
)

// set logger for operator sdk
Expand Down Expand Up @@ -32,26 +33,40 @@ func (l *logWrapper) Error(err error, msg string, keysAndValues ...interface{})
l.handleKeyValsMsg(event, msg, keysAndValues)
}

// WithValues returns a logger with the given values sets. The internal ap of values is shallow copied.
// nolint: ireturn // needed to implement the logr.LogSink interface
func (l *logWrapper) WithValues(keysAndValues ...interface{}) logr.LogSink {
result := l.shallowCopy()
if len(keysAndValues)%2 != 0 {
l.Logger.Warn().Msgf("could not parse additional key/values, array has odd length, dropped: %v", keysAndValues[len(keysAndValues)-1])
keysAndValues = keysAndValues[:len(keysAndValues)-2]
}
for i := 0; i < len(keysAndValues); i += 2 {
if key, ok := keysAndValues[i].(string); ok {
l.additionalValues[key] = keysAndValues[i+1]
result.additionalValues[key] = keysAndValues[i+1]
} else {
l.Logger.Warn().Msgf("could not parse additional keys for log message, key is not of type string: %v", keysAndValues[i])
}
}
return l
return result
}

// WithName returns a logger with the given values sets. The internal ap of values is shallow copied.
// nolint: ireturn // needed to implement the logr.LogSink interface
func (l *logWrapper) WithName(name string) logr.LogSink {
l.additionalValues["name"] = name
return l
result := l.shallowCopy()
result.additionalValues["name"] = name
return result
}

// shallowCopy returns a copy of this logger, the additionalValues as well as the referenced upstream logger are shallow copied.
func (l *logWrapper) shallowCopy() *logWrapper {
result := &logWrapper{
Logger: l.Logger,
additionalValues: make(map[string]interface{}),
}
maps.Copy(result.additionalValues, l.additionalValues)
return result
}

// handleKeyValsMsg handles the passed msg and the generic list of key value pairs
Expand Down
4 changes: 2 additions & 2 deletions cmd/ingress/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

const (
testTimeout = 60 * time.Second
testContainer = "docker.io/rancher/k3s:v1.28.2-k3s1"
testContainer = "docker.io/rancher/k3s:v1.29.1-k3s2"
svcName = "app"
svcPort = 8081
namespace = "test"
Expand Down Expand Up @@ -76,7 +76,7 @@ LOOP:
case <-ctx.Done():
t.Error("error waiting for non HTTP 503 response from the reverse proxy")
case <-ticker.C:
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+net.JoinHostPort(host, strconv.Itoa(httpsTestPort)), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://"+net.JoinHostPort(host, strconv.Itoa(httpsTestPort)), nil)
require.NoError(t, err)
r, err = httpClient.Do(req)
if err == nil {
Expand Down

0 comments on commit 9621201

Please sign in to comment.