feat(discovery): provide an option to disable discovery debug log (#1942)

* feat(discovery): provide an option to disable discovery resolver one debug-like log

* styl: re-sort imports

* styl: rename option function name and comment
status-code-override
yeqown 3 years ago committed by chenzhihui
parent 0dc30dde72
commit 00e38a6c2b
  1. 11
      transport/grpc/resolver/discovery/builder.go
  2. 5
      transport/grpc/resolver/discovery/resolver.go

@ -8,6 +8,7 @@ import (
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
) )
@ -37,11 +38,19 @@ func WithInsecure(insecure bool) Option {
} }
} }
// DisableDebugLog disables update instances log.
func DisableDebugLog() Option {
return func(b *builder) {
b.debugLogDisabled = true
}
}
type builder struct { type builder struct {
discoverer registry.Discovery discoverer registry.Discovery
logger log.Logger logger log.Logger
timeout time.Duration timeout time.Duration
insecure bool insecure bool
debugLogDisabled bool
} }
// NewBuilder creates a builder which is used to factory registry resolvers. // NewBuilder creates a builder which is used to factory registry resolvers.
@ -51,6 +60,7 @@ func NewBuilder(d registry.Discovery, opts ...Option) resolver.Builder {
logger: log.GetLogger(), logger: log.GetLogger(),
timeout: time.Second * 10, timeout: time.Second * 10,
insecure: false, insecure: false,
debugLogDisabled: false,
} }
for _, o := range opts { for _, o := range opts {
o(b) o(b)
@ -85,6 +95,7 @@ func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, opts res
cancel: cancel, cancel: cancel,
log: log.NewHelper(b.logger), log: log.NewHelper(b.logger),
insecure: b.insecure, insecure: b.insecure,
debugLogDisabled: b.debugLogDisabled,
} }
go r.watch() go r.watch()
return r, nil return r, nil

@ -9,6 +9,7 @@ import (
"github.com/go-kratos/kratos/v2/internal/endpoint" "github.com/go-kratos/kratos/v2/internal/endpoint"
"github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"google.golang.org/grpc/attributes" "google.golang.org/grpc/attributes"
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
) )
@ -22,6 +23,7 @@ type discoveryResolver struct {
cancel context.CancelFunc cancel context.CancelFunc
insecure bool insecure bool
debugLogDisabled bool
} }
func (r *discoveryResolver) watch() { func (r *discoveryResolver) watch() {
@ -77,8 +79,11 @@ func (r *discoveryResolver) update(ins []*registry.ServiceInstance) {
if err != nil { if err != nil {
r.log.Errorf("[resolver] failed to update state: %s", err) r.log.Errorf("[resolver] failed to update state: %s", err)
} }
if !r.debugLogDisabled {
b, _ := json.Marshal(ins) b, _ := json.Marshal(ins)
r.log.Infof("[resolver] update instances: %s", b) r.log.Infof("[resolver] update instances: %s", b)
}
} }
func (r *discoveryResolver) Close() { func (r *discoveryResolver) Close() {

Loading…
Cancel
Save