From 002eb4bb1981558fa5e614aed22d322f0f45d7ea Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Fri, 13 Nov 2020 13:42:40 +0300 Subject: Move all interfaces to the separate folder [RPC, METRICS, LOGGER] RPC for the metrics update to the working state RCP interface renamed to the RPCer --- interfaces/log/interface.go | 16 ++++++++++++ interfaces/log/zap_adapter.go | 56 +++++++++++++++++++++++++++++++++++++++++ interfaces/metrics/interface.go | 9 +++++++ interfaces/rpc/interface.go | 7 ++++++ 4 files changed, 88 insertions(+) create mode 100644 interfaces/log/interface.go create mode 100644 interfaces/log/zap_adapter.go create mode 100644 interfaces/metrics/interface.go create mode 100644 interfaces/rpc/interface.go (limited to 'interfaces') diff --git a/interfaces/log/interface.go b/interfaces/log/interface.go new file mode 100644 index 00000000..e2f2ce31 --- /dev/null +++ b/interfaces/log/interface.go @@ -0,0 +1,16 @@ +package log + +type ( + // Logger is an general RR log interface + Logger interface { + Debug(msg string, keyvals ...interface{}) + Info(msg string, keyvals ...interface{}) + Warn(msg string, keyvals ...interface{}) + Error(msg string, keyvals ...interface{}) + } +) + +// With creates a child logger and adds structured context to it +type WithLogger interface { + With(keyvals ...interface{}) Logger +} diff --git a/interfaces/log/zap_adapter.go b/interfaces/log/zap_adapter.go new file mode 100644 index 00000000..65f8d04b --- /dev/null +++ b/interfaces/log/zap_adapter.go @@ -0,0 +1,56 @@ +package log + +import ( + "fmt" + + "go.uber.org/zap" +) + +type ZapAdapter struct { + zl *zap.Logger +} + +// Create NewZapAdapter which uses general log interface +func NewZapAdapter(zapLogger *zap.Logger) *ZapAdapter { + return &ZapAdapter{ + zl: zapLogger.WithOptions(zap.AddCallerSkip(1)), + } +} + +func (log *ZapAdapter) fields(keyvals []interface{}) []zap.Field { + // we should have even number of keys and values + if len(keyvals)%2 != 0 { + return []zap.Field{zap.Error(fmt.Errorf("odd number of keyvals pairs: %v", keyvals))} + } + + var fields []zap.Field + for i := 0; i < len(keyvals); i += 2 { + key, ok := keyvals[i].(string) + if !ok { + key = fmt.Sprintf("%v", keyvals[i]) + } + fields = append(fields, zap.Any(key, keyvals[i+1])) + } + + return fields +} + +func (log *ZapAdapter) Debug(msg string, keyvals ...interface{}) { + log.zl.Debug(msg, log.fields(keyvals)...) +} + +func (log *ZapAdapter) Info(msg string, keyvals ...interface{}) { + log.zl.Info(msg, log.fields(keyvals)...) +} + +func (log *ZapAdapter) Warn(msg string, keyvals ...interface{}) { + log.zl.Warn(msg, log.fields(keyvals)...) +} + +func (log *ZapAdapter) Error(msg string, keyvals ...interface{}) { + log.zl.Error(msg, log.fields(keyvals)...) +} + +func (log *ZapAdapter) With(keyvals ...interface{}) Logger { + return NewZapAdapter(log.zl.With(log.fields(keyvals)...)) +} diff --git a/interfaces/metrics/interface.go b/interfaces/metrics/interface.go new file mode 100644 index 00000000..8207fb51 --- /dev/null +++ b/interfaces/metrics/interface.go @@ -0,0 +1,9 @@ +package metrics + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +type StatProvider interface { + MetricsCollector() prometheus.Collector +} diff --git a/interfaces/rpc/interface.go b/interfaces/rpc/interface.go new file mode 100644 index 00000000..7fd2668c --- /dev/null +++ b/interfaces/rpc/interface.go @@ -0,0 +1,7 @@ +package rpc + +// RPCer declares the ability to create set of public RPC methods. +type RPCer interface { + // Provides RPC methods for the given service. + RPC() (interface{}, error) +} -- cgit v1.2.3 From 6eefd067f4c08ed51834926abd1a4c60ec55b56d Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Fri, 13 Nov 2020 16:29:08 +0300 Subject: Update tests --- interfaces/rpc/interface.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'interfaces') diff --git a/interfaces/rpc/interface.go b/interfaces/rpc/interface.go index 7fd2668c..683fd2ec 100644 --- a/interfaces/rpc/interface.go +++ b/interfaces/rpc/interface.go @@ -3,5 +3,5 @@ package rpc // RPCer declares the ability to create set of public RPC methods. type RPCer interface { // Provides RPC methods for the given service. - RPC() (interface{}, error) + RPC() interface{} } -- cgit v1.2.3 From d40ff179e43a02726bfa4298e523a16c79a88cea Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 16 Nov 2020 15:11:27 +0300 Subject: Rename app->server Rename Config -> PoolConfig --- interfaces/server/interface.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 interfaces/server/interface.go (limited to 'interfaces') diff --git a/interfaces/server/interface.go b/interfaces/server/interface.go new file mode 100644 index 00000000..51d172cb --- /dev/null +++ b/interfaces/server/interface.go @@ -0,0 +1,17 @@ +package server + +import ( + "context" + "os/exec" + + "github.com/spiral/roadrunner/v2" +) + +type Env map[string]string + +// WorkerFactory creates workers for the application. +type WorkerFactory interface { + CmdFactory(env Env) (func() *exec.Cmd, error) + NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, error) + NewWorkerPool(ctx context.Context, opt roadrunner.PoolConfig, env Env) (roadrunner.Pool, error) +} -- cgit v1.2.3 From f592c6e410fd1f8075c8b75597eb8e5dc7ba9cf1 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 16 Nov 2020 15:36:52 +0300 Subject: Metrics collector returns slice of values --- interfaces/metrics/interface.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'interfaces') diff --git a/interfaces/metrics/interface.go b/interfaces/metrics/interface.go index 8207fb51..505c3d7b 100644 --- a/interfaces/metrics/interface.go +++ b/interfaces/metrics/interface.go @@ -5,5 +5,5 @@ import ( ) type StatProvider interface { - MetricsCollector() prometheus.Collector + MetricsCollector() []prometheus.Collector } -- cgit v1.2.3