From 00e38a6c2bde1c2bb25b630c6144e2ebc5396924 Mon Sep 17 00:00:00 2001 From: yeqown Date: Fri, 22 Apr 2022 20:06:29 +0800 Subject: [PATCH] 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 --- transport/grpc/resolver/discovery/builder.go | 39 ++++++++++++------- transport/grpc/resolver/discovery/resolver.go | 11 ++++-- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/transport/grpc/resolver/discovery/builder.go b/transport/grpc/resolver/discovery/builder.go index d0acf646d..5c98e5192 100644 --- a/transport/grpc/resolver/discovery/builder.go +++ b/transport/grpc/resolver/discovery/builder.go @@ -8,6 +8,7 @@ import ( "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/registry" + "google.golang.org/grpc/resolver" ) @@ -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) @@ -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 diff --git a/transport/grpc/resolver/discovery/resolver.go b/transport/grpc/resolver/discovery/resolver.go index fbc7eca83..79d5da1cd 100644 --- a/transport/grpc/resolver/discovery/resolver.go +++ b/transport/grpc/resolver/discovery/resolver.go @@ -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" ) @@ -21,7 +22,8 @@ type discoveryResolver struct { ctx context.Context cancel context.CancelFunc - insecure bool + insecure bool + debugLogDisabled bool } func (r *discoveryResolver) watch() { @@ -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() {