Skip to content

Commit

Permalink
feat(discovery): provide an option to disable discovery debug log (#1942
Browse files Browse the repository at this point in the history
)

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

* styl: re-sort imports

* styl: rename option function name and comment
  • Loading branch information
yeqown committed Apr 22, 2022
1 parent d0b704b commit fbf7855
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
39 changes: 25 additions & 14 deletions transport/grpc/resolver/discovery/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"

"google.golang.org/grpc/resolver"
)

Expand Down Expand Up @@ -37,20 +38,29 @@ func WithInsecure(insecure bool) Option {
}
}

// DisableDebugLog disables update instances log.
func DisableDebugLog() Option {
return func(b *builder) {
b.debugLogDisabled = true
}
}

type builder struct {
discoverer registry.Discovery
logger log.Logger
timeout time.Duration
insecure bool
discoverer registry.Discovery
logger log.Logger
timeout time.Duration
insecure bool
debugLogDisabled bool
}

// NewBuilder creates a builder which is used to factory registry resolvers.
func NewBuilder(d registry.Discovery, opts ...Option) resolver.Builder {
b := &builder{
discoverer: d,
logger: log.GetLogger(),
timeout: time.Second * 10,
insecure: false,
discoverer: d,
logger: log.GetLogger(),
timeout: time.Second * 10,
insecure: false,
debugLogDisabled: false,
}
for _, o := range opts {
o(b)
Expand Down Expand Up @@ -79,12 +89,13 @@ func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, opts res
return nil, err
}
r := &discoveryResolver{
w: w,
cc: cc,
ctx: ctx,
cancel: cancel,
log: log.NewHelper(b.logger),
insecure: b.insecure,
w: w,
cc: cc,
ctx: ctx,
cancel: cancel,
log: log.NewHelper(b.logger),
insecure: b.insecure,
debugLogDisabled: b.debugLogDisabled,
}
go r.watch()
return r, nil
Expand Down
11 changes: 8 additions & 3 deletions transport/grpc/resolver/discovery/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/go-kratos/kratos/v2/internal/endpoint"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"

"google.golang.org/grpc/attributes"
"google.golang.org/grpc/resolver"
)
Expand All @@ -21,7 +22,8 @@ type discoveryResolver struct {
ctx context.Context
cancel context.CancelFunc

insecure bool
insecure bool
debugLogDisabled bool
}

func (r *discoveryResolver) watch() {
Expand Down Expand Up @@ -77,8 +79,11 @@ func (r *discoveryResolver) update(ins []*registry.ServiceInstance) {
if err != nil {
r.log.Errorf("[resolver] failed to update state: %s", err)
}
b, _ := json.Marshal(ins)
r.log.Infof("[resolver] update instances: %s", b)

if !r.debugLogDisabled {
b, _ := json.Marshal(ins)
r.log.Infof("[resolver] update instances: %s", b)
}
}

func (r *discoveryResolver) Close() {
Expand Down

0 comments on commit fbf7855

Please sign in to comment.