From dddc70022781c7f5cb11f89591d5c51ef0d5461b Mon Sep 17 00:00:00 2001 From: Trock Date: Wed, 17 Mar 2021 10:18:25 +0800 Subject: [PATCH 01/11] feat: add CronLogAdapter --- otcron/log.go | 21 +++++++++++++++++++++ serve.go | 4 +++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 otcron/log.go diff --git a/otcron/log.go b/otcron/log.go new file mode 100644 index 00000000..90cfb97a --- /dev/null +++ b/otcron/log.go @@ -0,0 +1,21 @@ +package otcron + +import ( + "github.com/go-kit/kit/log" + "github.com/go-kit/kit/log/level" +) + +// CronLogAdapter is an adapter between kitlog and cron logger interface +type CronLogAdapter struct { + Logging log.Logger +} + +// Info implements cron.Logger +func (r CronLogAdapter) Info(msg string, keysAndValues ...interface{}) { + _ = level.Info(r.Logging).Log("msg", msg, keysAndValues) +} + +// Error implements cron.Logger +func (r CronLogAdapter) Error(err error, msg string, keysAndValues ...interface{}) { + _ = level.Error(r.Logging).Log("msg", msg, "error", err, keysAndValues) +} diff --git a/serve.go b/serve.go index cd9f9343..05037b74 100644 --- a/serve.go +++ b/serve.go @@ -3,6 +3,7 @@ package core import ( "context" "github.com/DoNewsCode/core/events" + "github.com/DoNewsCode/core/otcron" "net" "net/http" "os" @@ -123,8 +124,9 @@ func newServeCmd(p serveIn) *cobra.Command { // Start cron runner if !p.Config.Bool("cron.disable") { if p.Cron == nil { - p.Cron = cron.New() + p.Cron = cron.New(cron.WithLogger(otcron.CronLogAdapter{Logging: l})) } + p.Container.ApplyCron(p.Cron) g.Add(func() error { l.Info("cron runner started") From 7c72f508e95270e6947f6a11c43aa770e8b07fed Mon Sep 17 00:00:00 2001 From: Trock Date: Wed, 17 Mar 2021 14:13:47 +0800 Subject: [PATCH 02/11] fix: otcron.CronLogAdapter key change --- otcron/log.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/otcron/log.go b/otcron/log.go index 90cfb97a..db4ef7b4 100644 --- a/otcron/log.go +++ b/otcron/log.go @@ -17,5 +17,5 @@ func (r CronLogAdapter) Info(msg string, keysAndValues ...interface{}) { // Error implements cron.Logger func (r CronLogAdapter) Error(err error, msg string, keysAndValues ...interface{}) { - _ = level.Error(r.Logging).Log("msg", msg, "error", err, keysAndValues) + _ = level.Error(r.Logging).Log("msg", msg, "err", err, keysAndValues) } From d9e0b9af4fa815aca982bbffa093f91573565a6b Mon Sep 17 00:00:00 2001 From: Trock Date: Wed, 17 Mar 2021 14:28:34 +0800 Subject: [PATCH 03/11] fix: otcron log error and add test --- otcron/log.go | 4 ++-- otcron/log_test.go | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 otcron/log_test.go diff --git a/otcron/log.go b/otcron/log.go index db4ef7b4..b04e2735 100644 --- a/otcron/log.go +++ b/otcron/log.go @@ -12,10 +12,10 @@ type CronLogAdapter struct { // Info implements cron.Logger func (r CronLogAdapter) Info(msg string, keysAndValues ...interface{}) { - _ = level.Info(r.Logging).Log("msg", msg, keysAndValues) + _ = level.Info(r.Logging).Log(append([]interface{}{"msg", msg}, keysAndValues...)...) } // Error implements cron.Logger func (r CronLogAdapter) Error(err error, msg string, keysAndValues ...interface{}) { - _ = level.Error(r.Logging).Log("msg", msg, "err", err, keysAndValues) + _ = level.Error(r.Logging).Log(append([]interface{}{"msg", msg, "err", err}, keysAndValues...)...) } diff --git a/otcron/log_test.go b/otcron/log_test.go new file mode 100644 index 00000000..ba80a29d --- /dev/null +++ b/otcron/log_test.go @@ -0,0 +1,23 @@ +package otcron + +import ( + "bytes" + "github.com/go-kit/kit/log" + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCronLogAdapter_Info(t *testing.T) { + var buf bytes.Buffer + l := CronLogAdapter{Logging: log.NewLogfmtLogger(&buf)} + l.Info("msg", "key","value") + assert.Equal(t, "level=info msg=msg key=value\n", buf.String()) +} + +func TestCronLogAdapter_Error(t *testing.T) { + var buf bytes.Buffer + l := CronLogAdapter{Logging: log.NewLogfmtLogger(&buf)} + l.Error(errors.New("err"),"msg", "key","value") + assert.Equal(t, "level=error msg=msg err=err key=value\n",buf.String()) +} \ No newline at end of file From d6b6d6c87b67fc9616f569929ae6f94724c64dc1 Mon Sep 17 00:00:00 2001 From: Trock Date: Wed, 17 Mar 2021 16:08:14 +0800 Subject: [PATCH 04/11] fix: otcron rename to cronopts --- {otcron => cronopts}/log.go | 2 +- {otcron => cronopts}/log_test.go | 2 +- serve.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename {otcron => cronopts}/log.go (97%) rename {otcron => cronopts}/log_test.go (97%) diff --git a/otcron/log.go b/cronopts/log.go similarity index 97% rename from otcron/log.go rename to cronopts/log.go index b04e2735..870c9662 100644 --- a/otcron/log.go +++ b/cronopts/log.go @@ -1,4 +1,4 @@ -package otcron +package cronopts import ( "github.com/go-kit/kit/log" diff --git a/otcron/log_test.go b/cronopts/log_test.go similarity index 97% rename from otcron/log_test.go rename to cronopts/log_test.go index ba80a29d..aa47e5a7 100644 --- a/otcron/log_test.go +++ b/cronopts/log_test.go @@ -1,4 +1,4 @@ -package otcron +package cronopts import ( "bytes" diff --git a/serve.go b/serve.go index 05037b74..329263cc 100644 --- a/serve.go +++ b/serve.go @@ -3,7 +3,7 @@ package core import ( "context" "github.com/DoNewsCode/core/events" - "github.com/DoNewsCode/core/otcron" + "github.com/DoNewsCode/core/cronopts" "net" "net/http" "os" @@ -124,7 +124,7 @@ func newServeCmd(p serveIn) *cobra.Command { // Start cron runner if !p.Config.Bool("cron.disable") { if p.Cron == nil { - p.Cron = cron.New(cron.WithLogger(otcron.CronLogAdapter{Logging: l})) + p.Cron = cron.New(cron.WithLogger(cronopts.CronLogAdapter{Logging: l})) } p.Container.ApplyCron(p.Cron) From 41b010eaeb82a72208114f72a4f0cb6d3d43536c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=B7=E6=BA=AA?= Date: Wed, 17 Mar 2021 17:11:31 +0800 Subject: [PATCH 05/11] feat(otes): allow users to specify extra options (#97) --- otes/provider.go | 6 ++++-- otes/provider_test.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/otes/provider.go b/otes/provider.go index cee04f1e..4f996db6 100644 --- a/otes/provider.go +++ b/otes/provider.go @@ -62,8 +62,9 @@ type in struct { Logger log.Logger Conf contract.ConfigAccessor - Interceptor EsConfigInterceptor `optional:"true"` - Tracer opentracing.Tracer `optional:"true"` + Interceptor EsConfigInterceptor `optional:"true"` + Tracer opentracing.Tracer `optional:"true"` + Options []elastic.ClientOptionFunc `optional:"true"` } // out is the result of Provide. @@ -125,6 +126,7 @@ func provideEsFactory(p in) (out, func()) { elastic.SetErrorLog(esLogAdapter{level.Error(logger)}), elastic.SetTraceLog(esLogAdapter{level.Debug(logger)}), ) + options = append(options, p.Options...) client, err := elastic.NewClient(options...) if err != nil { diff --git a/otes/provider_test.go b/otes/provider_test.go index e1899bf0..fca29044 100644 --- a/otes/provider_test.go +++ b/otes/provider_test.go @@ -5,6 +5,7 @@ package otes import ( "github.com/DoNewsCode/core/config" "github.com/go-kit/kit/log" + "github.com/olivere/elastic/v7" esConfig "github.com/olivere/elastic/v7/config" "github.com/stretchr/testify/assert" "testing" @@ -29,6 +30,28 @@ func TestNewEsFactory(t *testing.T) { cleanup() } +func TestNewEsFactoryWithOptions(t *testing.T) { + var called bool + esFactory, cleanup := provideEsFactory(in{ + Conf: config.MapAdapter{"es": map[string]esConfig.Config{ + "default": {URL: "http://localhost:9200"}, + }}, + Logger: log.NewNopLogger(), + Options: []elastic.ClientOptionFunc{ + func(client *elastic.Client) error { + called = true + return nil + }, + }, + Tracer: nil, + }) + def, err := esFactory.Maker.Make("default") + assert.NoError(t, err) + assert.NotNil(t, def) + assert.True(t, called) + cleanup() +} + func TestProvideConfigs(t *testing.T) { c := provideConfig() assert.NotEmpty(t, c.Config) From 4c30692c945b34f7ff60a46ec8f2dfc9689b7231 Mon Sep 17 00:00:00 2001 From: Trock Date: Wed, 17 Mar 2021 10:18:25 +0800 Subject: [PATCH 06/11] feat: add CronLogAdapter --- otcron/log.go | 21 +++++++++++++++++++++ serve.go | 4 +++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 otcron/log.go diff --git a/otcron/log.go b/otcron/log.go new file mode 100644 index 00000000..90cfb97a --- /dev/null +++ b/otcron/log.go @@ -0,0 +1,21 @@ +package otcron + +import ( + "github.com/go-kit/kit/log" + "github.com/go-kit/kit/log/level" +) + +// CronLogAdapter is an adapter between kitlog and cron logger interface +type CronLogAdapter struct { + Logging log.Logger +} + +// Info implements cron.Logger +func (r CronLogAdapter) Info(msg string, keysAndValues ...interface{}) { + _ = level.Info(r.Logging).Log("msg", msg, keysAndValues) +} + +// Error implements cron.Logger +func (r CronLogAdapter) Error(err error, msg string, keysAndValues ...interface{}) { + _ = level.Error(r.Logging).Log("msg", msg, "error", err, keysAndValues) +} diff --git a/serve.go b/serve.go index cd9f9343..05037b74 100644 --- a/serve.go +++ b/serve.go @@ -3,6 +3,7 @@ package core import ( "context" "github.com/DoNewsCode/core/events" + "github.com/DoNewsCode/core/otcron" "net" "net/http" "os" @@ -123,8 +124,9 @@ func newServeCmd(p serveIn) *cobra.Command { // Start cron runner if !p.Config.Bool("cron.disable") { if p.Cron == nil { - p.Cron = cron.New() + p.Cron = cron.New(cron.WithLogger(otcron.CronLogAdapter{Logging: l})) } + p.Container.ApplyCron(p.Cron) g.Add(func() error { l.Info("cron runner started") From b44b9b8ad66e891c0b0ec07425080b443f80d6c0 Mon Sep 17 00:00:00 2001 From: Trock Date: Wed, 17 Mar 2021 14:13:47 +0800 Subject: [PATCH 07/11] fix: otcron.CronLogAdapter key change --- otcron/log.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/otcron/log.go b/otcron/log.go index 90cfb97a..db4ef7b4 100644 --- a/otcron/log.go +++ b/otcron/log.go @@ -17,5 +17,5 @@ func (r CronLogAdapter) Info(msg string, keysAndValues ...interface{}) { // Error implements cron.Logger func (r CronLogAdapter) Error(err error, msg string, keysAndValues ...interface{}) { - _ = level.Error(r.Logging).Log("msg", msg, "error", err, keysAndValues) + _ = level.Error(r.Logging).Log("msg", msg, "err", err, keysAndValues) } From 6205ed7289e906863638164e403b89dc9016ffa9 Mon Sep 17 00:00:00 2001 From: Trock Date: Wed, 17 Mar 2021 14:28:34 +0800 Subject: [PATCH 08/11] fix: otcron log error and add test --- otcron/log.go | 4 ++-- otcron/log_test.go | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 otcron/log_test.go diff --git a/otcron/log.go b/otcron/log.go index db4ef7b4..b04e2735 100644 --- a/otcron/log.go +++ b/otcron/log.go @@ -12,10 +12,10 @@ type CronLogAdapter struct { // Info implements cron.Logger func (r CronLogAdapter) Info(msg string, keysAndValues ...interface{}) { - _ = level.Info(r.Logging).Log("msg", msg, keysAndValues) + _ = level.Info(r.Logging).Log(append([]interface{}{"msg", msg}, keysAndValues...)...) } // Error implements cron.Logger func (r CronLogAdapter) Error(err error, msg string, keysAndValues ...interface{}) { - _ = level.Error(r.Logging).Log("msg", msg, "err", err, keysAndValues) + _ = level.Error(r.Logging).Log(append([]interface{}{"msg", msg, "err", err}, keysAndValues...)...) } diff --git a/otcron/log_test.go b/otcron/log_test.go new file mode 100644 index 00000000..ba80a29d --- /dev/null +++ b/otcron/log_test.go @@ -0,0 +1,23 @@ +package otcron + +import ( + "bytes" + "github.com/go-kit/kit/log" + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCronLogAdapter_Info(t *testing.T) { + var buf bytes.Buffer + l := CronLogAdapter{Logging: log.NewLogfmtLogger(&buf)} + l.Info("msg", "key","value") + assert.Equal(t, "level=info msg=msg key=value\n", buf.String()) +} + +func TestCronLogAdapter_Error(t *testing.T) { + var buf bytes.Buffer + l := CronLogAdapter{Logging: log.NewLogfmtLogger(&buf)} + l.Error(errors.New("err"),"msg", "key","value") + assert.Equal(t, "level=error msg=msg err=err key=value\n",buf.String()) +} \ No newline at end of file From 4bb8e6dd089a59cc960381f1d0470de05833bee8 Mon Sep 17 00:00:00 2001 From: Trock Date: Wed, 17 Mar 2021 16:08:14 +0800 Subject: [PATCH 09/11] fix: otcron rename to cronopts --- {otcron => cronopts}/log.go | 2 +- {otcron => cronopts}/log_test.go | 2 +- serve.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename {otcron => cronopts}/log.go (97%) rename {otcron => cronopts}/log_test.go (97%) diff --git a/otcron/log.go b/cronopts/log.go similarity index 97% rename from otcron/log.go rename to cronopts/log.go index b04e2735..870c9662 100644 --- a/otcron/log.go +++ b/cronopts/log.go @@ -1,4 +1,4 @@ -package otcron +package cronopts import ( "github.com/go-kit/kit/log" diff --git a/otcron/log_test.go b/cronopts/log_test.go similarity index 97% rename from otcron/log_test.go rename to cronopts/log_test.go index ba80a29d..aa47e5a7 100644 --- a/otcron/log_test.go +++ b/cronopts/log_test.go @@ -1,4 +1,4 @@ -package otcron +package cronopts import ( "bytes" diff --git a/serve.go b/serve.go index 05037b74..329263cc 100644 --- a/serve.go +++ b/serve.go @@ -3,7 +3,7 @@ package core import ( "context" "github.com/DoNewsCode/core/events" - "github.com/DoNewsCode/core/otcron" + "github.com/DoNewsCode/core/cronopts" "net" "net/http" "os" @@ -124,7 +124,7 @@ func newServeCmd(p serveIn) *cobra.Command { // Start cron runner if !p.Config.Bool("cron.disable") { if p.Cron == nil { - p.Cron = cron.New(cron.WithLogger(otcron.CronLogAdapter{Logging: l})) + p.Cron = cron.New(cron.WithLogger(cronopts.CronLogAdapter{Logging: l})) } p.Container.ApplyCron(p.Cron) From 0952d03aa2df9a5f52fba9cfd8a86fa511832a46 Mon Sep 17 00:00:00 2001 From: Trock Date: Wed, 17 Mar 2021 17:30:26 +0800 Subject: [PATCH 10/11] docs: fix gorm doc error --- otgorm/doc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/otgorm/doc.go b/otgorm/doc.go index 50067ed8..3f91ed4e 100644 --- a/otgorm/doc.go +++ b/otgorm/doc.go @@ -22,7 +22,7 @@ Then you can invoke gorm from the application. // use client }) -Sometimes there are valid reasons to connect to more than one mongo server. +Sometimes there are valid reasons to connect to more than one mysql server. Inject otgorm.Maker to factory a *gorm.DB with a specific configuration entry. c.Invoke(function(maker otgorm.Maker) { From b5eefd456c0b6147a222bfa12640d1fb88635e03 Mon Sep 17 00:00:00 2001 From: Trock Date: Wed, 17 Mar 2021 17:37:08 +0800 Subject: [PATCH 11/11] fix: rename r to c --- cronopts/log.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cronopts/log.go b/cronopts/log.go index 870c9662..ce680055 100644 --- a/cronopts/log.go +++ b/cronopts/log.go @@ -11,11 +11,11 @@ type CronLogAdapter struct { } // Info implements cron.Logger -func (r CronLogAdapter) Info(msg string, keysAndValues ...interface{}) { - _ = level.Info(r.Logging).Log(append([]interface{}{"msg", msg}, keysAndValues...)...) +func (c CronLogAdapter) Info(msg string, keysAndValues ...interface{}) { + _ = level.Info(c.Logging).Log(append([]interface{}{"msg", msg}, keysAndValues...)...) } // Error implements cron.Logger -func (r CronLogAdapter) Error(err error, msg string, keysAndValues ...interface{}) { - _ = level.Error(r.Logging).Log(append([]interface{}{"msg", msg, "err", err}, keysAndValues...)...) +func (c CronLogAdapter) Error(err error, msg string, keysAndValues ...interface{}) { + _ = level.Error(c.Logging).Log(append([]interface{}{"msg", msg, "err", err}, keysAndValues...)...) }