Skip to content

Commit

Permalink
add more logging
Browse files Browse the repository at this point in the history
Signed-off-by: Vasiliy Tolstov <[email protected]>
  • Loading branch information
vtolstov committed Sep 29, 2021
1 parent cb45c68 commit ad6912b
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 68 deletions.
103 changes: 67 additions & 36 deletions consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
rutil "github.com/unistack-org/micro/v3/util/reflect"
)

var (
DefaultStructTag = "consul"
)
var DefaultStructTag = "consul"

type consulConfig struct {
opts config.Options
Expand Down Expand Up @@ -82,50 +80,74 @@ func (c *consulConfig) Init(opts ...config.Option) error {

func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
for _, fn := range c.opts.BeforeLoad {
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
return err
if err := fn(ctx, c); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load err: %v", err)
if !c.opts.AllowFail {
return err
}
}
}

pair, _, err := c.cli.KV().Get(c.path, nil)
if err != nil && !c.opts.AllowFail {
return fmt.Errorf("consul path %s load error: %v", c.path, err)
} else if pair == nil && !c.opts.AllowFail {
return fmt.Errorf("consul path %s not found", c.path)
}

if err == nil && pair != nil {
options := config.NewLoadOptions(opts...)
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
if options.Override {
mopts = append(mopts, mergo.WithOverride)
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load path %s err: %v", c.path, err)
if !c.opts.AllowFail {
return fmt.Errorf("consul path %s load error: %v", c.path, err)
}
if options.Append {
mopts = append(mopts, mergo.WithAppendSlice)
} else if pair == nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load path %s not found", c.path)
if !c.opts.AllowFail {
return fmt.Errorf("consul path %s not found", c.path)
}
}

dst := c.opts.Struct
if options.Struct != nil {
dst = options.Struct
if err != nil || pair == nil {
for _, fn := range c.opts.AfterLoad {
if err := fn(ctx, c); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load err: %v", err)
if !c.opts.AllowFail {
return err
}
}
}
return nil
}

options := config.NewLoadOptions(opts...)
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
if options.Override {
mopts = append(mopts, mergo.WithOverride)
}
if options.Append {
mopts = append(mopts, mergo.WithAppendSlice)
}

dst := c.opts.Struct
if options.Struct != nil {
dst = options.Struct
}

src, err := rutil.Zero(dst)
src, err := rutil.Zero(dst)
if err == nil {
err = c.opts.Codec.Unmarshal(pair.Value, src)
if err == nil {
err = c.opts.Codec.Unmarshal(pair.Value, src)
if err == nil {
err = mergo.Merge(dst, src, mopts...)
}
err = mergo.Merge(dst, src, mopts...)
}
}

if err != nil && !c.opts.AllowFail {
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load err: %v", err)
if !c.opts.AllowFail {
return err
}

}

for _, fn := range c.opts.AfterLoad {
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
return err
if err := fn(ctx, c); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul load err: %v", err)
if !c.opts.AllowFail {
return err
}
}
}

Expand All @@ -134,8 +156,11 @@ func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) erro

func (c *consulConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
for _, fn := range c.opts.BeforeSave {
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
return err
if err := fn(ctx, c); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul save err: %v", err)
if !c.opts.AllowFail {
return err
}
}
}

Expand All @@ -144,13 +169,19 @@ func (c *consulConfig) Save(ctx context.Context, opts ...config.SaveOption) erro
_, err = c.cli.KV().Put(&api.KVPair{Key: c.path, Value: buf}, nil)
}

if err != nil && !c.opts.AllowFail {
return fmt.Errorf("consul path %s save error: %v", c.path, err)
if err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul path %s save err: %v", c.path, err)
if !c.opts.AllowFail {
return fmt.Errorf("consul path %s save error: %v", c.path, err)
}
}

for _, fn := range c.opts.AfterSave {
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
return err
if err := fn(ctx, c); err != nil {
c.opts.Logger.Errorf(c.opts.Context, "consul save err: %v", err)
if !c.opts.AllowFail {
return err
}
}
}

Expand Down
14 changes: 10 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ module github.com/unistack-org/micro-config-consul/v3
go 1.16

require (
github.com/google/btree v1.0.0 // indirect
github.com/hashicorp/consul/api v1.10.1
github.com/armon/go-metrics v0.3.9 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/hashicorp/consul/api v1.11.0
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v0.16.2
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/imdario/mergo v0.3.12
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/unistack-org/micro/v3 v3.7.4
github.com/unistack-org/micro/v3 v3.7.5
)
Loading

0 comments on commit ad6912b

Please sign in to comment.