Skip to content

Commit

Permalink
Add omitempty yaml tag to deprecated log.Wrapper fields
Browse files Browse the repository at this point in the history
Also implement log.Wrapper.MarshalYAML for nil values.

Also add `yaml:"-"` explicitly for config struct fields that's not
yaml-unmarshal-able.
  • Loading branch information
fishy committed Aug 15, 2024
1 parent 8264c73 commit 6c818f5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion filewatcher/filewatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type Config struct {
// call, which will be returned directly.
//
// Deprecated: Errors will be logged via slog at error level instead.
Logger log.Wrapper `yaml:"logger"`
Logger log.Wrapper `yaml:"logger,omitempty"`

// Optional. When <=0 DefaultMaxFileSize will be used instead.
//
Expand Down
12 changes: 12 additions & 0 deletions log/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ func (w *Wrapper) UnmarshalText(text []byte) error {
return nil
}

// MarshalYAML implements yaml.Marshaler (both v2 and v3).
//
// It only support nil values, as it's almost impossible to detect which non-nil
// value it really is to marshal to some meaningful value that can be
// unmarshaled.
func (w Wrapper) MarshalYAML() (any, error) {
if w == nil {
return nil, nil
}
return nil, errors.New("log.Wrapper.MarshalYAML only supports nil value")
}

var _ encoding.TextUnmarshaler = (*Wrapper)(nil)

// WrapToThriftLogger wraps a Wrapper into thrift.Logger.
Expand Down
28 changes: 28 additions & 0 deletions log/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/go-kit/kit/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/reddit/baseplate.go/log"
"gopkg.in/yaml.v2"
)

func TestLogWrapperNilSafe(t *testing.T) {
Expand Down Expand Up @@ -102,6 +103,33 @@ func TestLogWrapperUnmarshalText(t *testing.T) {
}
}

func TestWrapperMarshalYAML(t *testing.T) {
type foo struct {
Log log.Wrapper `yaml:"log,omitempty"`
}
t.Run("nil", func(t *testing.T) {
bar := foo{Log: nil}
var sb strings.Builder
if err := yaml.NewEncoder(&sb).Encode(bar); err != nil {
t.Fatal(err)
}
if got, want := strings.TrimSpace(sb.String()), `log: null`; got != want {
t.Errorf("yaml got %q want %q", got, want)
}
})
t.Run("non-nil", func(t *testing.T) {
bar := foo{Log: log.NopWrapper}
var sb strings.Builder
if err := yaml.NewEncoder(&sb).Encode(bar); err == nil {
t.Errorf("Expected marshal error, got yaml %q", sb.String())
}
})
}

var (
_ yaml.Marshaler = (log.Wrapper)(nil)
)

var (
_ log.Counter = (prometheus.Counter)(nil)
_ log.Counter = (metrics.Counter)(nil)
Expand Down
8 changes: 4 additions & 4 deletions thriftbp/client_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type ClientPoolConfig struct {
InitialConnectionsFallback bool `yaml:"initialConnectionsFallback"`
// Deprecated: Individual connection errors during initialization is always
// logged via zap logger on warning level.
InitialConnectionsFallbackLogger log.Wrapper `yaml:"initialConnectionsFallbackLogger"`
InitialConnectionsFallbackLogger log.Wrapper `yaml:"initialConnectionsFallbackLogger,omitempty"`

// MaxConnections is the maximum number of thrift connections the client
// pool can maintain.
Expand Down Expand Up @@ -171,7 +171,7 @@ type ClientPoolConfig struct {
// retry.Attempts(1). This sets up the retry middleware but does not
// automatically retry any requests. You can set retry behavior per-call by
// using retrybp.WithOptions.
DefaultRetryOptions []retry.Option
DefaultRetryOptions []retry.Option `yaml:"-"`

// ReportPoolStats signals to the ClientPool that it should report
// statistics on the underlying clientpool.Pool in a background
Expand Down Expand Up @@ -206,7 +206,7 @@ type ClientPoolConfig struct {
// See MonitorClientArgs.ErrorSpanSuppressor for more details.
//
// This is optional. If it's not set IDLExceptionSuppressor will be used.
ErrorSpanSuppressor errorsbp.Suppressor
ErrorSpanSuppressor errorsbp.Suppressor `yaml:"-"`

// When BreakerConfig is non-nil,
// a breakerbp.FailureRatioBreaker will be created for the pool,
Expand All @@ -216,7 +216,7 @@ type ClientPoolConfig struct {
// The edge context implementation. Optional.
//
// If it's not set, the global one from ecinterface.Get will be used instead.
EdgeContextImpl ecinterface.Interface
EdgeContextImpl ecinterface.Interface `yaml:"-"`

// The name for the server to identify this client,
// via the "User-Agent" (HeaderUserAgent) THeader.
Expand Down

0 comments on commit 6c818f5

Please sign in to comment.