From 67b4a79da5cbff6677865e4958e666e8bd84a684 Mon Sep 17 00:00:00 2001 From: shtrikulis Date: Sat, 9 Jan 2021 18:17:55 +0300 Subject: [PATCH] Prepare release 2.0.7 --- CHANGELOG.md | 22 ++++++- config/{config.go => app.go} | 10 ++- config/{config_test.go => app_test.go} | 6 +- internal/env/configenv.go | 2 +- internal/env/noconfigenv.go | 2 +- .../metrics => metrics}/checklistener.go | 0 metrics/configure.go | 52 +++++++++++++++ .../exporters/jaegerexporter/configure.go | 6 +- .../exporters/jaegerexporter/httpformat.go | 0 .../exporters/newrelicexporter/configure.go | 4 +- metrics/exporters/newrelicexporter/logger.go | 27 ++++++++ .../exporters/newrelicexporter/provider.go | 28 +------- .../exporters/opencensusexporter/configure.go | 6 +- .../exporters/prometheus/configure.go | 4 +- .../metrics => metrics}/propagation/http.go | 0 {internal/metrics => metrics}/provider.go | 66 ++++++------------- multilog/config.go | 3 +- multilog/log/config.go | 22 +++++-- .../multilogprovider}/provider.go | 43 ++++++++++-- .../multilogprovider}/provider_test.go | 4 +- server/executors.go | 34 +++++++--- server/options.go | 49 ++++++++++++-- server/providers.go | 43 ++++++------ server/run.go | 34 +++++++--- .../events/eventrouter/metrics/builder.go | 0 .../events/eventrouter/metrics/ctx.go | 0 .../events/eventrouter/metrics/handler.go | 0 .../events/eventrouter/metrics/lables.go | 0 .../events/eventrouter/metrics/name.go | 0 .../events/eventrouter/metrics/publisher.go | 0 .../events/eventrouter/metrics/stats.go | 0 .../events/eventrouter/metrics/subscriber.go | 0 transport/events/eventrouter/provider.go | 2 +- transport/grpc/provider.go | 2 +- transport/http/httpclient/provider.go | 2 +- transport/http/httpserver/provider.go | 2 +- transport/mysql/provider.go | 24 +++++-- transport/sql/provider.go | 16 +++-- 38 files changed, 355 insertions(+), 160 deletions(-) rename config/{config.go => app.go} (60%) rename config/{config_test.go => app_test.go} (80%) rename {internal/metrics => metrics}/checklistener.go (100%) create mode 100644 metrics/configure.go rename {internal/metrics => metrics}/exporters/jaegerexporter/configure.go (87%) rename {internal/metrics => metrics}/exporters/jaegerexporter/httpformat.go (100%) rename {internal/metrics => metrics}/exporters/newrelicexporter/configure.go (94%) create mode 100644 metrics/exporters/newrelicexporter/logger.go rename {internal/metrics => metrics}/exporters/newrelicexporter/provider.go (52%) rename {internal/metrics => metrics}/exporters/opencensusexporter/configure.go (88%) rename {internal/metrics => metrics}/exporters/prometheus/configure.go (93%) rename {internal/metrics => metrics}/propagation/http.go (100%) rename {internal/metrics => metrics}/provider.go (51%) rename {internal/multilog => multilog/multilogprovider}/provider.go (66%) rename {internal/multilog => multilog/multilogprovider}/provider_test.go (85%) rename {internal => transport}/events/eventrouter/metrics/builder.go (100%) rename {internal => transport}/events/eventrouter/metrics/ctx.go (100%) rename {internal => transport}/events/eventrouter/metrics/handler.go (100%) rename {internal => transport}/events/eventrouter/metrics/lables.go (100%) rename {internal => transport}/events/eventrouter/metrics/name.go (100%) rename {internal => transport}/events/eventrouter/metrics/publisher.go (100%) rename {internal => transport}/events/eventrouter/metrics/stats.go (100%) rename {internal => transport}/events/eventrouter/metrics/subscriber.go (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9316a43..d265f62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,25 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [2.0.7] - 2021-01-09 +- MultiLog: + - Added `LocalMode` (colored human console) and `DebugMode` (trace log level) to `config.Application` + - Application name in log fields + - By default, console log enabled (added `SilentMode` in `multilog.Config` to turn it off) +- Runner: + - All builtin providers are public + - Added startup options: + - `server.NoDefaultProviders()` - turn off all default providers and configurations + - `server.LocalDebug()` - turn on colored console logging and trace level +- Other: + - `mysql.Provide` now returns `driver.Connector` (and added `mysql.ProvideConnector` for manual registering) + - `sql.Provide` now accepting `driver.Connector` +- Deprecated: + - `server.NoWaitOption()` -> `server.NoWait()` + - `server.EnvPathOption(path)` -> `server.EnvPath(path)` + - `server.ConfigOption(cfg)` -> `server.WithConfig(cfg)` + - Field `MySQL` in `sql.Args` structure + ## [2.0.6] - 2020-11-12 @@ -33,7 +52,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## 1.0.0 - 2020-09-28 -[Unreleased]: https://github.com/vseinstrumentiru/lego/compare/v2.0.6...HEAD +[Unreleased]: https://github.com/vseinstrumentiru/lego/compare/v2.0.7...HEAD +[2.0.7]: https://github.com/vseinstrumentiru/lego/compare/v2.0.6...v2.0.7 [2.0.6]: https://github.com/vseinstrumentiru/lego/compare/v2.0.5...v2.0.6 [2.0.5]: https://github.com/vseinstrumentiru/lego/compare/v2.0.4...v2.0.5 [2.0.4]: https://github.com/vseinstrumentiru/lego/compare/v2.0.3...v2.0.4 diff --git a/config/config.go b/config/app.go similarity index 60% rename from config/config.go rename to config/app.go index ad4beb7..ec6a5e6 100644 --- a/config/config.go +++ b/config/app.go @@ -1,15 +1,19 @@ package config -func Undefined() *Application { +const Undefined = "undefined" + +func UndefinedApplication() *Application { return &Application{ - Name: "undefined", - DataCenter: "undefined", + Name: Undefined, + DataCenter: Undefined, } } type Application struct { Name string DataCenter string + DebugMode bool + LocalMode bool } func (c Application) FullName() string { diff --git a/config/config_test.go b/config/app_test.go similarity index 80% rename from config/config_test.go rename to config/app_test.go index 0bb04d8..f013b7d 100644 --- a/config/config_test.go +++ b/config/app_test.go @@ -36,12 +36,12 @@ func TestUndefined(t *testing.T) { name string want *Application }{ - {"success", &Application{"undefined", "undefined"}}, + {"success", &Application{Name: "undefined", DataCenter: "undefined"}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := Undefined(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("Undefined() = %v, want %v", got, tt.want) + if got := UndefinedApplication(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("UndefinedApplication() = %v, want %v", got, tt.want) } }) } diff --git a/internal/env/configenv.go b/internal/env/configenv.go index e6b007a..871dab2 100644 --- a/internal/env/configenv.go +++ b/internal/env/configenv.go @@ -46,7 +46,7 @@ func (e *configEnv) Configure(cfg Config) error { e.setInstances(parsed) if !parsed.exist(base.Application{}) { - app := base.Undefined() + app := base.UndefinedApplication() if name := e.viper.GetString("name"); name != "" { app = &base.Application{ diff --git a/internal/env/noconfigenv.go b/internal/env/noconfigenv.go index 910eef4..168cb4f 100644 --- a/internal/env/noconfigenv.go +++ b/internal/env/noconfigenv.go @@ -17,7 +17,7 @@ func (e *noConfigEnv) Configure() error { return err } - app := base.Undefined() + app := base.UndefinedApplication() if name := e.viper.GetString("name"); name != "" { app = &base.Application{ diff --git a/internal/metrics/checklistener.go b/metrics/checklistener.go similarity index 100% rename from internal/metrics/checklistener.go rename to metrics/checklistener.go diff --git a/metrics/configure.go b/metrics/configure.go new file mode 100644 index 0000000..df9dbcf --- /dev/null +++ b/metrics/configure.go @@ -0,0 +1,52 @@ +package metrics + +import ( + health "github.com/AppsFlyer/go-sundheit" + "go.opencensus.io/plugin/ocgrpc" + "go.opencensus.io/plugin/ochttp" + "go.opencensus.io/stats/view" + "go.opencensus.io/trace" + "go.uber.org/dig" + + "github.com/vseinstrumentiru/lego/v2/metrics/tracing" +) + +func ConfigureStats() error { + return view.Register( + // Health checks + health.ViewCheckCountByNameAndStatus, + health.ViewCheckStatusByName, + health.ViewCheckExecutionTime, + // HTTP Client + ochttp.ClientCompletedCount, + ochttp.ClientSentBytesDistribution, + ochttp.ClientReceivedBytesDistribution, + ochttp.ClientRoundtripLatencyDistribution, + // GRPC Client + ocgrpc.ClientSentBytesPerRPCView, + ocgrpc.ClientReceivedBytesPerRPCView, + ocgrpc.ClientRoundtripLatencyView, + ocgrpc.ClientRoundtripLatencyView, + ) +} + +type TraceConfigArgs struct { + dig.In + Trace *tracing.Config `optional:"true"` +} + +func ConfigureTrace(in TraceConfigArgs) error { + if in.Trace != nil { + trace.ApplyConfig( + trace.Config{ + DefaultSampler: trace.Sampler(in.Trace.Sampler), + MaxAnnotationEventsPerSpan: in.Trace.MaxAnnotationEventsPerSpan, + MaxMessageEventsPerSpan: in.Trace.MaxMessageEventsPerSpan, + MaxAttributesPerSpan: in.Trace.MaxAttributesPerSpan, + MaxLinksPerSpan: in.Trace.MaxLinksPerSpan, + }, + ) + } + + return nil +} diff --git a/internal/metrics/exporters/jaegerexporter/configure.go b/metrics/exporters/jaegerexporter/configure.go similarity index 87% rename from internal/metrics/exporters/jaegerexporter/configure.go rename to metrics/exporters/jaegerexporter/configure.go index eb61e2a..342485a 100644 --- a/internal/metrics/exporters/jaegerexporter/configure.go +++ b/metrics/exporters/jaegerexporter/configure.go @@ -6,12 +6,12 @@ import ( "go.uber.org/dig" "github.com/vseinstrumentiru/lego/v2/config" - "github.com/vseinstrumentiru/lego/v2/internal/metrics/propagation" "github.com/vseinstrumentiru/lego/v2/metrics/exporters" + "github.com/vseinstrumentiru/lego/v2/metrics/propagation" "github.com/vseinstrumentiru/lego/v2/multilog" ) -type argsIn struct { +type Args struct { dig.In App *config.Application Config *exporters.Jaeger `optional:"true"` @@ -19,7 +19,7 @@ type argsIn struct { Propagation *propagation.HTTPFormatCollection } -func Configure(in argsIn) error { +func Configure(in Args) error { if in.Config == nil { return nil } diff --git a/internal/metrics/exporters/jaegerexporter/httpformat.go b/metrics/exporters/jaegerexporter/httpformat.go similarity index 100% rename from internal/metrics/exporters/jaegerexporter/httpformat.go rename to metrics/exporters/jaegerexporter/httpformat.go diff --git a/internal/metrics/exporters/newrelicexporter/configure.go b/metrics/exporters/newrelicexporter/configure.go similarity index 94% rename from internal/metrics/exporters/newrelicexporter/configure.go rename to metrics/exporters/newrelicexporter/configure.go index 7df6811..39a551d 100644 --- a/internal/metrics/exporters/newrelicexporter/configure.go +++ b/metrics/exporters/newrelicexporter/configure.go @@ -13,7 +13,7 @@ import ( lenewrelic "github.com/vseinstrumentiru/lego/v2/multilog/newrelic" ) -type argsIn struct { +type ConfigArgs struct { dig.In App *config.Application `optional:"true"` Config *exporters.NewRelic `optional:"true"` @@ -21,7 +21,7 @@ type argsIn struct { Log multilog.Logger } -func Configure(in argsIn) error { +func Configure(in ConfigArgs) error { if in.Config == nil || !in.Config.Enabled { return nil } diff --git a/metrics/exporters/newrelicexporter/logger.go b/metrics/exporters/newrelicexporter/logger.go new file mode 100644 index 0000000..32ccbc9 --- /dev/null +++ b/metrics/exporters/newrelicexporter/logger.go @@ -0,0 +1,27 @@ +package newrelicexporter + +import "github.com/vseinstrumentiru/lego/v2/multilog" + +type loggerWrap struct { + multilog.Logger +} + +func (l loggerWrap) Error(msg string, context map[string]interface{}) { + l.Logger.Error(msg, context) +} + +func (l loggerWrap) Warn(msg string, context map[string]interface{}) { + l.Logger.Warn(msg, context) +} + +func (l loggerWrap) Info(msg string, context map[string]interface{}) { + l.Logger.Info(msg, context) +} + +func (l loggerWrap) Debug(msg string, context map[string]interface{}) { + l.Logger.Debug(msg, context) +} + +func (l loggerWrap) DebugEnabled() bool { + return false +} diff --git a/internal/metrics/exporters/newrelicexporter/provider.go b/metrics/exporters/newrelicexporter/provider.go similarity index 52% rename from internal/metrics/exporters/newrelicexporter/provider.go rename to metrics/exporters/newrelicexporter/provider.go index e6b104c..d56ba79 100644 --- a/internal/metrics/exporters/newrelicexporter/provider.go +++ b/metrics/exporters/newrelicexporter/provider.go @@ -9,38 +9,14 @@ import ( "github.com/vseinstrumentiru/lego/v2/multilog" ) -type args struct { +type ProvideArgs struct { dig.In App *config.Application Config *exporters.NewRelic `optional:"true"` Logger multilog.Logger } -type loggerWrap struct { - multilog.Logger -} - -func (l loggerWrap) Error(msg string, context map[string]interface{}) { - l.Logger.Error(msg, context) -} - -func (l loggerWrap) Warn(msg string, context map[string]interface{}) { - l.Logger.Warn(msg, context) -} - -func (l loggerWrap) Info(msg string, context map[string]interface{}) { - l.Logger.Info(msg, context) -} - -func (l loggerWrap) Debug(msg string, context map[string]interface{}) { - l.Logger.Debug(msg, context) -} - -func (l loggerWrap) DebugEnabled() bool { - return false -} - -func Provide(in args) (app *newrelic.Application, err error) { +func Provide(in ProvideArgs) (app *newrelic.Application, err error) { if in.Config == nil { return nil, nil } diff --git a/internal/metrics/exporters/opencensusexporter/configure.go b/metrics/exporters/opencensusexporter/configure.go similarity index 88% rename from internal/metrics/exporters/opencensusexporter/configure.go rename to metrics/exporters/opencensusexporter/configure.go index 5250780..379a791 100644 --- a/internal/metrics/exporters/opencensusexporter/configure.go +++ b/metrics/exporters/opencensusexporter/configure.go @@ -8,12 +8,12 @@ import ( "go.uber.org/dig" "github.com/vseinstrumentiru/lego/v2/config" - "github.com/vseinstrumentiru/lego/v2/internal/metrics/propagation" "github.com/vseinstrumentiru/lego/v2/metrics/exporters" + "github.com/vseinstrumentiru/lego/v2/metrics/propagation" "github.com/vseinstrumentiru/lego/v2/multilog" ) -type argsIn struct { +type Args struct { dig.In App *config.Application Config *exporters.Opencensus `optional:"true"` @@ -21,7 +21,7 @@ type argsIn struct { Propagation *propagation.HTTPFormatCollection } -func Configure(in argsIn) error { +func Configure(in Args) error { if in.Config == nil { return nil } diff --git a/internal/metrics/exporters/prometheus/configure.go b/metrics/exporters/prometheus/configure.go similarity index 93% rename from internal/metrics/exporters/prometheus/configure.go rename to metrics/exporters/prometheus/configure.go index 5127fbd..4a373f5 100644 --- a/internal/metrics/exporters/prometheus/configure.go +++ b/metrics/exporters/prometheus/configure.go @@ -12,7 +12,7 @@ import ( "github.com/vseinstrumentiru/lego/v2/version" ) -type argsIn struct { +type Args struct { dig.In Router *http.ServeMux App *config.Application @@ -20,7 +20,7 @@ type argsIn struct { Version version.Info } -func Configure(in argsIn) error { +func Configure(in Args) error { log := in.Log.WithFields(map[string]interface{}{"component": "exporter.prometheus"}) exp, err := prometheus.NewExporter(prometheus.Options{ diff --git a/internal/metrics/propagation/http.go b/metrics/propagation/http.go similarity index 100% rename from internal/metrics/propagation/http.go rename to metrics/propagation/http.go diff --git a/internal/metrics/provider.go b/metrics/provider.go similarity index 51% rename from internal/metrics/provider.go rename to metrics/provider.go index ac45716..4aa5c8c 100644 --- a/internal/metrics/provider.go +++ b/metrics/provider.go @@ -10,49 +10,54 @@ import ( "github.com/cloudflare/tableflip" "github.com/oklog/run" appkitrun "github.com/sagikazarmark/appkit/run" - "go.opencensus.io/plugin/ocgrpc" - "go.opencensus.io/plugin/ochttp" - "go.opencensus.io/stats/view" - "go.opencensus.io/trace" "go.opencensus.io/zpages" "go.uber.org/dig" - "github.com/vseinstrumentiru/lego/v2/metrics" - "github.com/vseinstrumentiru/lego/v2/metrics/tracing" "github.com/vseinstrumentiru/lego/v2/multilog" "github.com/vseinstrumentiru/lego/v2/version" ) -type args struct { +type HealthArgs struct { dig.In - Config *metrics.Config `optional:"true"` - Trace *tracing.Config `optional:"true"` + Logger multilog.Logger +} + +func ProvideHealthChecker(in HealthArgs) health.Health { + logger := in.Logger.WithFields(map[string]interface{}{"component": "metrics"}) + healthz := health.New() + healthz.WithCheckListener(NewLogCheckListener(logger)) + + return healthz +} + +type ServerArgs struct { + dig.In + Config *Config `optional:"true"` + + Health health.Health Version version.Info Logger multilog.Logger Pipeline *run.Group Upg *tableflip.Upgrader } -func Provide(in args) (*http.ServeMux, health.Health) { +func ProvideMonitoringServer(in ServerArgs) *http.ServeMux { server := http.DefaultServeMux server.Handle("/version", version.Handler(in.Version)) logger := in.Logger.WithFields(map[string]interface{}{"component": "metrics"}) if in.Config == nil { - in.Config = metrics.NewDefaultConfig() + in.Config = NewDefaultConfig() } if in.Config.Debug { zpages.Handle(server, "/debug") } - healthz := health.New() - healthz.WithCheckListener(NewLogCheckListener(logger)) - { - handler := healthhttp.HandleHealthJSON(healthz) + handler := healthhttp.HandleHealthJSON(in.Health) server.Handle("/healthz", handler) // Kubernetes style health checks @@ -62,23 +67,6 @@ func Provide(in args) (*http.ServeMux, health.Health) { server.Handle("/healthz/ready", handler) } - emperror.Panic(view.Register( - // Health checks - health.ViewCheckCountByNameAndStatus, - health.ViewCheckStatusByName, - health.ViewCheckExecutionTime, - // HTTP Client - ochttp.ClientCompletedCount, - ochttp.ClientSentBytesDistribution, - ochttp.ClientReceivedBytesDistribution, - ochttp.ClientRoundtripLatencyDistribution, - // GRPC Client - ocgrpc.ClientSentBytesPerRPCView, - ocgrpc.ClientReceivedBytesPerRPCView, - ocgrpc.ClientRoundtripLatencyView, - ocgrpc.ClientRoundtripLatencyView, - )) - httpLn, err := in.Upg.Listen("tcp", fmt.Sprintf(":%v", in.Config.Port)) emperror.Panic(err) @@ -91,17 +79,5 @@ func Provide(in args) (*http.ServeMux, health.Health) { appkitrun.HTTPServe(srv, httpLn, 0), )) - if in.Trace != nil { - trace.ApplyConfig( - trace.Config{ - DefaultSampler: trace.Sampler(in.Trace.Sampler), - MaxAnnotationEventsPerSpan: in.Trace.MaxAnnotationEventsPerSpan, - MaxMessageEventsPerSpan: in.Trace.MaxMessageEventsPerSpan, - MaxAttributesPerSpan: in.Trace.MaxAttributesPerSpan, - MaxLinksPerSpan: in.Trace.MaxLinksPerSpan, - }, - ) - } - - return server, healthz + return server } diff --git a/multilog/config.go b/multilog/config.go index 702103c..bcf15c0 100644 --- a/multilog/config.go +++ b/multilog/config.go @@ -7,7 +7,8 @@ import ( ) type Config struct { - Level logur.Level + Level logur.Level + SilentMode bool } func (c Config) SetDefaults(env config.Env) { diff --git a/multilog/log/config.go b/multilog/log/config.go index b02899f..3897e7b 100644 --- a/multilog/log/config.go +++ b/multilog/log/config.go @@ -1,18 +1,32 @@ package log import ( + "time" + "logur.dev/logur" "github.com/vseinstrumentiru/lego/v2/config" ) +func DefaultConfig() *Config { + return &Config{ + Color: false, + Stop: false, + Depth: -1, + Level: logur.Trace, + TimeFormat: "15:04:05.000", + } +} + type Config struct { - Color bool - Stop bool - Depth int - Level logur.Level + Color bool + Stop bool + Depth int + Level logur.Level + TimeFormat string } func (c Config) SetDefaults(env config.Env) { env.SetDefault("depth", -1) + env.SetDefault("timeFormat", time.RFC3339Nano) } diff --git a/internal/multilog/provider.go b/multilog/multilogprovider/provider.go similarity index 66% rename from internal/multilog/provider.go rename to multilog/multilogprovider/provider.go index 1fd65d6..4df192b 100644 --- a/internal/multilog/provider.go +++ b/multilog/multilogprovider/provider.go @@ -1,4 +1,4 @@ -package multilog +package multilogprovider import ( "fmt" @@ -10,6 +10,7 @@ import ( zerologadapter "logur.dev/adapter/zerolog" "logur.dev/logur" + "github.com/vseinstrumentiru/lego/v2/config" "github.com/vseinstrumentiru/lego/v2/multilog" "github.com/vseinstrumentiru/lego/v2/multilog/log" "github.com/vseinstrumentiru/lego/v2/multilog/sentry" @@ -17,6 +18,7 @@ import ( type args struct { dig.In + App *config.Application Config *multilog.Config `optional:"true"` Sentry *sentry.Config `optional:"true"` @@ -35,17 +37,41 @@ func withCaller(depth int) ctxOpt { func Provide(in args) multilog.Logger { var opts []multilog.Option + if in.Config == nil { + level := logur.Error + if in.App.DebugMode { + level = logur.Trace + } + + in.Config = &multilog.Config{Level: level} + } + if in.Sentry != nil { opts = append(opts, multilog.WithHandler(sentry.Handler(in.Sentry.Addr, in.Sentry.Level, in.Sentry.Stop))) } var contextOptions []ctxOpt + if in.Log == nil && !in.Config.SilentMode { + in.Log = log.DefaultConfig() + + if in.App.LocalMode { + in.Log.Color = true + } + } + if in.Log != nil { if in.Logger == nil { var writer io.Writer + if in.Log.TimeFormat != "" { + zerolog.TimeFieldFormat = in.Log.TimeFormat + } + if in.Log.Color { zeroWriter := zerolog.NewConsoleWriter() + if in.Log.TimeFormat != "" { + zeroWriter.TimeFormat = in.Log.TimeFormat + } zeroWriter.FormatMessage = func(i interface{}) string { return fmt.Sprintf("%-30s|", i) } @@ -78,9 +104,18 @@ func Provide(in args) multilog.Logger { opts = append(opts, multilog.WithHandler(log.Handler(in.Logger, in.Log.Stop))) } - if in.Config == nil { - in.Config = &multilog.Config{Level: logur.Error} + logger := multilog.New(in.Config.Level, opts...) + + if in.App.Name != config.Undefined && in.App.Name != "" { + fields := make(map[string]interface{}) + fields["app.name"] = in.App.Name + + if in.App.DataCenter != "" && in.App.DataCenter != config.Undefined { + fields["app.dc"] = in.App.DataCenter + } + + logger = logger.WithFields(fields) } - return multilog.New(in.Config.Level, opts...) + return logger } diff --git a/internal/multilog/provider_test.go b/multilog/multilogprovider/provider_test.go similarity index 85% rename from internal/multilog/provider_test.go rename to multilog/multilogprovider/provider_test.go index 91f3575..b8e7441 100644 --- a/internal/multilog/provider_test.go +++ b/multilog/multilogprovider/provider_test.go @@ -1,4 +1,4 @@ -package multilog +package multilogprovider import ( "testing" @@ -8,6 +8,7 @@ import ( zerologadapter "logur.dev/adapter/zerolog" "logur.dev/logur" + "github.com/vseinstrumentiru/lego/v2/config" "github.com/vseinstrumentiru/lego/v2/multilog" "github.com/vseinstrumentiru/lego/v2/multilog/log" @@ -22,6 +23,7 @@ func TestProvide(t *testing.T) { ass.NoError(c.Register(func() logur.Logger { return zerologadapter.New(zerolog.Logger) })) + ass.NoError(c.Instance(config.UndefinedApplication())) ass.NoError(c.Instance(&multilog.Config{Level: logur.Info})) ass.NoError(c.Instance(&log.Config{Stop: false})) ass.NoError(c.Register(Provide)) diff --git a/server/executors.go b/server/executors.go index 69bb8f3..68055e6 100644 --- a/server/executors.go +++ b/server/executors.go @@ -1,17 +1,31 @@ package server import ( - "github.com/vseinstrumentiru/lego/v2/internal/metrics/exporters/jaegerexporter" - "github.com/vseinstrumentiru/lego/v2/internal/metrics/exporters/newrelicexporter" - "github.com/vseinstrumentiru/lego/v2/internal/metrics/exporters/opencensusexporter" - "github.com/vseinstrumentiru/lego/v2/internal/metrics/exporters/prometheus" + "github.com/vseinstrumentiru/lego/v2/metrics" + "github.com/vseinstrumentiru/lego/v2/metrics/exporters/jaegerexporter" + "github.com/vseinstrumentiru/lego/v2/metrics/exporters/newrelicexporter" + "github.com/vseinstrumentiru/lego/v2/metrics/exporters/opencensusexporter" + "github.com/vseinstrumentiru/lego/v2/metrics/exporters/prometheus" ) -func executors() []interface{} { - return []interface{}{ - jaegerexporter.Configure, - prometheus.Configure, - opencensusexporter.Configure, - newrelicexporter.Configure, +func executors(r *Runtime) (exec []interface{}) { + execList := map[string][]interface{}{ + optUseJaeger: {jaegerexporter.Configure}, + optUsePrometheus: {prometheus.Configure}, + optUseOpencensus: {opencensusexporter.Configure}, + optUseNewRelic: {newrelicexporter.Configure}, + optUseTrace: {metrics.ConfigureTrace}, + optUseStats: {metrics.ConfigureStats}, + optUseMonitoring: {metrics.ConfigureStats}, } + + ignoreProviders := r.Is(optWithoutProviders) + + for key, cfg := range execList { + if !ignoreProviders || r.Is(key) { + exec = append(exec, cfg...) + } + } + + return exec } diff --git a/server/options.go b/server/options.go index 6e470cb..6dc41e1 100644 --- a/server/options.go +++ b/server/options.go @@ -1,24 +1,63 @@ package server const ( - flagNoWait = "flag_nowait" + optNoWait = "opt_no_wait" optEnvPath = "opt_env_path" + + optLocalDebug = "opt_local_debug" + + optWithoutProviders = "opt_without_providers" + + optUseJaeger = "opt_use_jaeger" + optUsePrometheus = "opt_use_prometheus" + optUseOpencensus = "opt_use_opencensus" + optUseNewRelic = "opt_use_newrelic" + optUseTrace = "opt_use_trace" + optUseStats = "opt_use_stats" + optUseMonitoring = "opt_use_monitoring" ) -func NoWaitOption() Option { +func LocalDebug() Option { return func(r *Runtime) { - r.opts.Set(flagNoWait, true) + r.opts.Set(optLocalDebug, true) } } -func EnvPathOption(path string) Option { +func NoDefaultProviders() Option { + return func(r *Runtime) { + r.opts.Set(optWithoutProviders, true) + } +} + +func NoWait() Option { + return func(r *Runtime) { + r.opts.Set(optNoWait, true) + } +} + +// Deprecated: use NoWait +func NoWaitOption() Option { + return NoWait() +} + +func EnvPath(path string) Option { return func(r *Runtime) { r.opts.Set(optEnvPath, path) } } -func ConfigOption(cfg interface{}) Option { +// Deprecated: use EnvPath +func EnvPathOption(path string) Option { + return EnvPath(path) +} + +func WithConfig(cfg interface{}) Option { return func(r *Runtime) { r.cfg = cfg } } + +// Deprecated: use WithConfig +func ConfigOption(cfg interface{}) Option { + return WithConfig(cfg) +} diff --git a/server/providers.go b/server/providers.go index 77f8eff..f67b378 100644 --- a/server/providers.go +++ b/server/providers.go @@ -1,42 +1,47 @@ package server import ( - "github.com/vseinstrumentiru/lego/v2/internal/metrics" - "github.com/vseinstrumentiru/lego/v2/internal/metrics/exporters/newrelicexporter" - "github.com/vseinstrumentiru/lego/v2/internal/metrics/propagation" - events2 "github.com/vseinstrumentiru/lego/v2/transport/events" - eventrouter2 "github.com/vseinstrumentiru/lego/v2/transport/events/eventrouter" + "github.com/vseinstrumentiru/lego/v2/metrics" + "github.com/vseinstrumentiru/lego/v2/metrics/exporters/newrelicexporter" + "github.com/vseinstrumentiru/lego/v2/metrics/propagation" + "github.com/vseinstrumentiru/lego/v2/transport/events" + "github.com/vseinstrumentiru/lego/v2/transport/events/eventrouter" "github.com/vseinstrumentiru/lego/v2/transport/grpc" "github.com/vseinstrumentiru/lego/v2/transport/http/httpclient" "github.com/vseinstrumentiru/lego/v2/transport/http/httpserver" - mysql2 "github.com/vseinstrumentiru/lego/v2/transport/mysql" - nats2 "github.com/vseinstrumentiru/lego/v2/transport/nats" + "github.com/vseinstrumentiru/lego/v2/transport/mysql" + "github.com/vseinstrumentiru/lego/v2/transport/nats" "github.com/vseinstrumentiru/lego/v2/transport/sql" - stan2 "github.com/vseinstrumentiru/lego/v2/transport/stan" + "github.com/vseinstrumentiru/lego/v2/transport/stan" ) -func providers() []interface{} { +func providers(runtime *Runtime) []interface{} { + if runtime.Is(optWithoutProviders) { + return []interface{}{} + } + return []interface{}{ // metric providers propagation.ProvideHTTP, newrelicexporter.Provide, - metrics.Provide, + metrics.ProvideHealthChecker, + metrics.ProvideMonitoringServer, // http / grpc httpserver.Provide, httpclient.Provide, httpclient.ConstructorProvider, grpc.Provide, // database - mysql2.Provide, + mysql.Provide, sql.Provide, // events - nats2.Provide, - stan2.Provide, - eventrouter2.Provide, - events2.ProvideKafkaPublisher, - events2.ProvideKafkaSubscriber, - events2.ProvideNatsSubscriber, - events2.ProvideNatsPublisher, - events2.ProvideChannel, + nats.Provide, + stan.Provide, + eventrouter.Provide, + events.ProvideKafkaPublisher, + events.ProvideKafkaSubscriber, + events.ProvideNatsSubscriber, + events.ProvideNatsPublisher, + events.ProvideChannel, } } diff --git a/server/run.go b/server/run.go index 5786cc7..9a987e6 100644 --- a/server/run.go +++ b/server/run.go @@ -16,8 +16,8 @@ import ( di "github.com/vseinstrumentiru/lego/v2/internal/container" "github.com/vseinstrumentiru/lego/v2/internal/deprecated" environment "github.com/vseinstrumentiru/lego/v2/internal/env" - multilogProvider "github.com/vseinstrumentiru/lego/v2/internal/multilog" "github.com/vseinstrumentiru/lego/v2/multilog" + "github.com/vseinstrumentiru/lego/v2/multilog/multilogprovider" "github.com/vseinstrumentiru/lego/v2/server/shutdown" "github.com/vseinstrumentiru/lego/v2/version" ) @@ -41,8 +41,16 @@ func Run(app interface{}, opts ...Option) { container.register(func() environment.Config { return cfg }) }) + container.make(env) + + if runtime.Is(optLocalDebug) { + container.execute(func(cfg *baseCfg.Application) { + cfg.LocalMode = true + cfg.DebugMode = true + }) + } + container. - make(env). execute(func(cfg *baseCfg.Application) { ver = version.New(cfg) }). @@ -54,15 +62,19 @@ func Run(app interface{}, opts ...Option) { } // logger var logger multilog.Logger - container.register(multilogProvider.Provide). + container.register(multilogprovider.Provide). execute(func(l multilog.Logger) { logger = l }) + logger = logger.WithFields(map[string]interface{}{"component": "runtime"}) + logger.Trace("starting application") + defer emperror.HandleRecover(logger.WithFields(map[string]interface{}{"type": "panic"})) multilog.SetStandardLogger(logger.WithFields(map[string]interface{}{"type": "standard"})) // pipeline configuration + logger.Trace("pipeline configuration") closer := new(shutdown.CloseGroup) container.instance(closer) defer func() { @@ -89,22 +101,28 @@ func Run(app interface{}, opts ...Option) { pipeline.Add(run.SignalHandler(ctx, syscall.SIGINT, syscall.SIGTERM)) pipeline.Add(appkitrun.GracefulRestart(ctx, upg)) - deprecated.Container = container.i + logger.Trace("pipeline configuration", map[string]interface{}{"status": "completed"}) - for _, provider := range providers() { + // constructing application + logger.Trace("constructing application") + deprecated.Container = container.i + for _, provider := range providers(runtime) { container.register(provider) } - // constructing application + container.make(app) - for _, exec := range executors() { + for _, exec := range executors(runtime) { container.execute(exec) } + logger.Trace("constructing application", map[string]interface{}{"status": "completed"}) - if runtime.Not(flagNoWait) { + if runtime.Not(optNoWait) { + logger.Trace("starting pipeline") // running application if err := pipeline.Run(); err != nil { logger.WithErrFilter(match.As(&run.SignalError{}).MatchError).Notify(err) } } + logger.Trace("application stopped") } diff --git a/internal/events/eventrouter/metrics/builder.go b/transport/events/eventrouter/metrics/builder.go similarity index 100% rename from internal/events/eventrouter/metrics/builder.go rename to transport/events/eventrouter/metrics/builder.go diff --git a/internal/events/eventrouter/metrics/ctx.go b/transport/events/eventrouter/metrics/ctx.go similarity index 100% rename from internal/events/eventrouter/metrics/ctx.go rename to transport/events/eventrouter/metrics/ctx.go diff --git a/internal/events/eventrouter/metrics/handler.go b/transport/events/eventrouter/metrics/handler.go similarity index 100% rename from internal/events/eventrouter/metrics/handler.go rename to transport/events/eventrouter/metrics/handler.go diff --git a/internal/events/eventrouter/metrics/lables.go b/transport/events/eventrouter/metrics/lables.go similarity index 100% rename from internal/events/eventrouter/metrics/lables.go rename to transport/events/eventrouter/metrics/lables.go diff --git a/internal/events/eventrouter/metrics/name.go b/transport/events/eventrouter/metrics/name.go similarity index 100% rename from internal/events/eventrouter/metrics/name.go rename to transport/events/eventrouter/metrics/name.go diff --git a/internal/events/eventrouter/metrics/publisher.go b/transport/events/eventrouter/metrics/publisher.go similarity index 100% rename from internal/events/eventrouter/metrics/publisher.go rename to transport/events/eventrouter/metrics/publisher.go diff --git a/internal/events/eventrouter/metrics/stats.go b/transport/events/eventrouter/metrics/stats.go similarity index 100% rename from internal/events/eventrouter/metrics/stats.go rename to transport/events/eventrouter/metrics/stats.go diff --git a/internal/events/eventrouter/metrics/subscriber.go b/transport/events/eventrouter/metrics/subscriber.go similarity index 100% rename from internal/events/eventrouter/metrics/subscriber.go rename to transport/events/eventrouter/metrics/subscriber.go diff --git a/transport/events/eventrouter/provider.go b/transport/events/eventrouter/provider.go index 6c04ec8..cb5c7ad 100644 --- a/transport/events/eventrouter/provider.go +++ b/transport/events/eventrouter/provider.go @@ -11,8 +11,8 @@ import ( watermilllog "logur.dev/integration/watermill" "github.com/vseinstrumentiru/lego/v2/events" - "github.com/vseinstrumentiru/lego/v2/internal/events/eventrouter/metrics" "github.com/vseinstrumentiru/lego/v2/multilog" + "github.com/vseinstrumentiru/lego/v2/transport/events/eventrouter/metrics" ) type Args struct { diff --git a/transport/grpc/provider.go b/transport/grpc/provider.go index c5f982f..772aa7d 100644 --- a/transport/grpc/provider.go +++ b/transport/grpc/provider.go @@ -13,7 +13,7 @@ import ( "go.uber.org/dig" "google.golang.org/grpc" - "github.com/vseinstrumentiru/lego/v2/internal/metrics/propagation" + "github.com/vseinstrumentiru/lego/v2/metrics/propagation" "github.com/vseinstrumentiru/lego/v2/metrics/tracing" "github.com/vseinstrumentiru/lego/v2/multilog" "github.com/vseinstrumentiru/lego/v2/transport/http/middleware" diff --git a/transport/http/httpclient/provider.go b/transport/http/httpclient/provider.go index 9628466..603c8e1 100644 --- a/transport/http/httpclient/provider.go +++ b/transport/http/httpclient/provider.go @@ -6,7 +6,7 @@ import ( "go.opencensus.io/plugin/ochttp" "go.opencensus.io/trace" - "github.com/vseinstrumentiru/lego/v2/internal/metrics/propagation" + "github.com/vseinstrumentiru/lego/v2/metrics/propagation" httpTransport "github.com/vseinstrumentiru/lego/v2/transport/http" ) diff --git a/transport/http/httpserver/provider.go b/transport/http/httpserver/provider.go index a467dda..40aac5c 100644 --- a/transport/http/httpserver/provider.go +++ b/transport/http/httpserver/provider.go @@ -17,7 +17,7 @@ import ( "go.opencensus.io/trace" "go.uber.org/dig" - "github.com/vseinstrumentiru/lego/v2/internal/metrics/propagation" + "github.com/vseinstrumentiru/lego/v2/metrics/propagation" "github.com/vseinstrumentiru/lego/v2/metrics/tracing" "github.com/vseinstrumentiru/lego/v2/multilog" httpcfg "github.com/vseinstrumentiru/lego/v2/transport/http" diff --git a/transport/mysql/provider.go b/transport/mysql/provider.go index 3f3446d..2d96561 100644 --- a/transport/mysql/provider.go +++ b/transport/mysql/provider.go @@ -1,6 +1,8 @@ package mysql import ( + "database/sql/driver" + "contrib.go.opencensus.io/integrations/ocsql" "emperror.dev/errors" "github.com/go-sql-driver/mysql" @@ -16,7 +18,17 @@ type Args struct { Logger multilog.Logger } -func Provide(in Args) (*Connector, error) { +func ProvideConnector(in Args) (*Connector, error) { + conn, err := Provide(in) + + if err != nil { + return nil, err + } + + return &Connector{conn}, nil +} + +func Provide(in Args) (driver.Connector, error) { connector, err := mysql.NewConnector(&in.Config.Config) if err != nil { @@ -26,10 +38,8 @@ func Provide(in Args) (*Connector, error) { logger := in.Logger.WithFields(map[string]interface{}{"component": "mysql"}) _ = mysql.SetLogger(logur.NewErrorPrintLogger(logger)) - return &Connector{ - Connector: ocsql.WrapConnector( - connector, - ocsql.WithOptions(in.Config.Trace), - ), - }, nil + return ocsql.WrapConnector( + connector, + ocsql.WithOptions(in.Config.Trace), + ), nil } diff --git a/transport/sql/provider.go b/transport/sql/provider.go index 4abedcc..98bd6ed 100644 --- a/transport/sql/provider.go +++ b/transport/sql/provider.go @@ -17,6 +17,8 @@ import ( type Args struct { dig.In + Connector driver.Connector `optional:"true"` + // Deprecated: use driver.Connector MySQL *mysql.Connector `optional:"true"` Closer *shutdown.CloseGroup @@ -24,15 +26,15 @@ type Args struct { } func Provide(in Args) (*sql.DB, error) { - var connector driver.Connector - - if in.MySQL != nil { - connector = in.MySQL - } else { - return nil, errors.New("connector not found. you must provide connector") + if in.Connector == nil { + if in.MySQL != nil { + in.Connector = in.MySQL.Connector + } else { + return nil, errors.New("connector not found. you must provide `driver.Connector`") + } } - conn := sql.OpenDB(connector) + conn := sql.OpenDB(in.Connector) stopStats := ocsql.RecordStats(conn, 5*time.Second) err := in.Health.RegisterCheck(&health.Config{