diff options
author | Valery Piashchynski <[email protected]> | 2020-11-16 17:02:23 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-11-16 17:02:23 +0300 |
commit | 41829c62826cf88530545715d0aac897b1b56f6d (patch) | |
tree | 4d3a1da3cbfeacec420188b308d17e89872ce9d3 /plugins | |
parent | 57ad958acab2d108be0a35547faf6e7a791cf069 (diff) | |
parent | 38f6925db27dd94cfbca873901bf932ed1456906 (diff) |
Merge branch 'plugin/metrics' into plugin/http
# Conflicts:
# plugins/server/plugin.go
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/metrics/plugin.go | 21 | ||||
-rw-r--r-- | plugins/metrics/tests/metrics_test.go | 5 | ||||
-rw-r--r-- | plugins/metrics/tests/plugin1.go | 72 | ||||
-rw-r--r-- | plugins/server/config.go (renamed from plugins/app/config.go) | 10 | ||||
-rw-r--r-- | plugins/server/plugin.go (renamed from plugins/app/plugin.go) | 13 | ||||
-rw-r--r-- | plugins/server/tests/configs/.rr-no-app-section.yaml (renamed from plugins/app/tests/configs/.rr-no-app-section.yaml) | 2 | ||||
-rw-r--r-- | plugins/server/tests/configs/.rr-sockets.yaml (renamed from plugins/app/tests/configs/.rr-sockets.yaml) | 2 | ||||
-rw-r--r-- | plugins/server/tests/configs/.rr-tcp.yaml (renamed from plugins/app/tests/configs/.rr-tcp.yaml) | 2 | ||||
-rw-r--r-- | plugins/server/tests/configs/.rr-wrong-command.yaml (renamed from plugins/app/tests/configs/.rr-wrong-command.yaml) | 2 | ||||
-rw-r--r-- | plugins/server/tests/configs/.rr-wrong-relay.yaml (renamed from plugins/app/tests/configs/.rr-wrong-relay.yaml) | 2 | ||||
-rw-r--r-- | plugins/server/tests/configs/.rr.yaml (renamed from plugins/app/tests/configs/.rr.yaml) | 2 | ||||
-rw-r--r-- | plugins/server/tests/plugin_pipes.go (renamed from plugins/app/tests/plugin_pipes.go) | 9 | ||||
-rw-r--r-- | plugins/server/tests/plugin_sockets.go (renamed from plugins/app/tests/plugin_sockets.go) | 9 | ||||
-rw-r--r-- | plugins/server/tests/plugin_tcp.go (renamed from plugins/app/tests/plugin_tcp.go) | 9 | ||||
-rw-r--r-- | plugins/server/tests/server_test.go (renamed from plugins/app/tests/app_test.go) | 16 | ||||
-rw-r--r-- | plugins/server/tests/socket.php (renamed from plugins/app/tests/socket.php) | 0 | ||||
-rw-r--r-- | plugins/server/tests/tcp.php (renamed from plugins/app/tests/tcp.php) | 0 |
17 files changed, 65 insertions, 111 deletions
diff --git a/plugins/metrics/plugin.go b/plugins/metrics/plugin.go index 8ffd19d5..3fd42ee4 100644 --- a/plugins/metrics/plugin.go +++ b/plugins/metrics/plugin.go @@ -25,8 +25,8 @@ const ( ) type statsProvider struct { - collector prometheus.Collector - name string + collectors []prometheus.Collector + name string } // Plugin to manage application metrics using Prometheus. @@ -73,8 +73,8 @@ func (m *Plugin) Init(cfg config.Configurer, log log.Logger) error { // Register invocation will be later in the Serve method for k, v := range collectors { m.collectors.Store(k, statsProvider{ - collector: v, - name: k, + collectors: []prometheus.Collector{v}, + name: k, }) } return nil @@ -92,10 +92,13 @@ func (m *Plugin) Serve() chan error { // key - name // value - statsProvider struct c := value.(statsProvider) - if err := m.registry.Register(c.collector); err != nil { - errCh <- err - return false + for _, v := range c.collectors { + if err := m.registry.Register(v); err != nil { + errCh <- err + return false + } } + return true }) @@ -207,8 +210,8 @@ func (m *Plugin) Collects() []interface{} { // Collector returns application specific collector by name or nil if collector not found. func (m *Plugin) AddStatProvider(name endure.Named, stat metrics.StatProvider) error { m.collectors.Store(name.Name(), statsProvider{ - collector: stat.MetricsCollector(), - name: name.Name(), + collectors: stat.MetricsCollector(), + name: name.Name(), }) return nil } diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index 7528fc46..ed5d085a 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -157,10 +157,11 @@ func TestMetricsGaugeCollector(t *testing.T) { out, err := get("http://localhost:2112/metrics") assert.NoError(t, err) assert.Contains(t, out, "my_gauge 100") + assert.Contains(t, out, "my_gauge2 100") - genericOut, err := get("http://localhost:2112/metrics") + out, err = get("http://localhost:2112/metrics") assert.NoError(t, err) - assert.Contains(t, genericOut, "go_gc_duration_seconds") + assert.Contains(t, out, "go_gc_duration_seconds") for { select { diff --git a/plugins/metrics/tests/plugin1.go b/plugins/metrics/tests/plugin1.go index 8f1ece52..b48c415d 100644 --- a/plugins/metrics/tests/plugin1.go +++ b/plugins/metrics/tests/plugin1.go @@ -28,77 +28,19 @@ func (p1 *Plugin1) Name() string { return "metrics_test.plugin1" } -func (p1 *Plugin1) MetricsCollector() prometheus.Collector { +func (p1 *Plugin1) MetricsCollector() []prometheus.Collector { collector := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "my_gauge", Help: "My gauge value", }) collector.Set(100) - return collector -} -// ////////////////////////////////////////////////////////////// -type Plugin3 struct { - config config.Configurer -} - -func (p *Plugin3) Init(cfg config.Configurer) error { - p.config = cfg - return nil -} - -func (p *Plugin3) Serve() chan error { - errCh := make(chan error, 1) - return errCh -} - -func (p *Plugin3) Stop() error { - return nil -} - -func (p *Plugin3) Name() string { - return "metrics_test.plugin3" -} - -func (p *Plugin3) MetricsCollector() prometheus.Collector { - var ( - cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "cpu_temperature_celsius", - Help: "Current temperature of the CPU.", - }) - ) - return cpuTemp -} - -type Plugin4 struct { - config config.Configurer -} - -func (p *Plugin4) Init(cfg config.Configurer) error { - p.config = cfg - return nil -} - -func (p *Plugin4) Serve() chan error { - errCh := make(chan error, 1) - return errCh -} - -func (p *Plugin4) Stop() error { - return nil -} - -func (p *Plugin4) Name() string { - return "metrics_test.plugin4" -} + collector2 := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "my_gauge2", + Help: "My gauge2 value", + }) -func (p *Plugin4) MetricsCollector() prometheus.Collector { - var ( - cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "cpu_temperature_celsius", - Help: "Current temperature of the CPU.", - }) - ) - return cpuTemp + collector2.Set(100) + return []prometheus.Collector{collector, collector2} } diff --git a/plugins/app/config.go b/plugins/server/config.go index eaa54e2d..147ae0f7 100644 --- a/plugins/app/config.go +++ b/plugins/server/config.go @@ -1,6 +1,10 @@ -package app +package server -import "time" +import ( + "time" + + "github.com/spiral/roadrunner/v2/interfaces/server" +) // Config config combines factory, pool and cmd configurations. type Config struct { @@ -14,7 +18,7 @@ type Config struct { Group string // Env represents application environment. - Env Env + Env server.Env // Listen defines connection method and factory to be used to connect to workers: // "pipes", "tcp://:6001", "unix://rr.sock" diff --git a/plugins/app/plugin.go b/plugins/server/plugin.go index fbb53c3d..e096708a 100644 --- a/plugins/app/plugin.go +++ b/plugins/server/plugin.go @@ -1,4 +1,4 @@ -package app +package server import ( "context" @@ -10,11 +10,12 @@ import ( "github.com/spiral/errors" "github.com/spiral/roadrunner/v2" "github.com/spiral/roadrunner/v2/interfaces/log" + "github.com/spiral/roadrunner/v2/interfaces/server" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/util" ) -const ServiceName = "app" +const ServiceName = "server" // Plugin manages worker type Plugin struct { @@ -62,7 +63,7 @@ func (app *Plugin) Stop() error { } // CmdFactory provides worker command factory assocated with given context. -func (app *Plugin) CmdFactory(env map[string]string) (func() *exec.Cmd, error) { +func (app *Plugin) CmdFactory(env server.Env) (func() *exec.Cmd, error) { var cmdArgs []string // create command according to the config @@ -88,7 +89,7 @@ func (app *Plugin) CmdFactory(env map[string]string) (func() *exec.Cmd, error) { } // NewWorker issues new standalone worker. -func (app *Plugin) NewWorker(ctx context.Context, env map[string]string) (roadrunner.WorkerBase, error) { +func (app *Plugin) NewWorker(ctx context.Context, env server.Env) (roadrunner.WorkerBase, error) { const op = errors.Op("new worker") spawnCmd, err := app.CmdFactory(env) if err != nil { @@ -106,7 +107,7 @@ func (app *Plugin) NewWorker(ctx context.Context, env map[string]string) (roadru } // NewWorkerPool issues new worker pool. -func (app *Plugin) NewWorkerPool(ctx context.Context, opt roadrunner.PoolConfig, env map[string]string) (roadrunner.Pool, error) { +func (app *Plugin) NewWorkerPool(ctx context.Context, opt roadrunner.PoolConfig, env server.Env) (roadrunner.Pool, error) { spawnCmd, err := app.CmdFactory(env) if err != nil { return nil, err @@ -150,7 +151,7 @@ func (app *Plugin) initFactory() (roadrunner.Factory, error) { } } -func (app *Plugin) setEnv(e map[string]string) []string { +func (app *Plugin) setEnv(e server.Env) []string { env := append(os.Environ(), fmt.Sprintf("RR_RELAY=%s", app.cfg.Relay)) for k, v := range e { env = append(env, fmt.Sprintf("%s=%s", strings.ToUpper(k), v)) diff --git a/plugins/app/tests/configs/.rr-no-app-section.yaml b/plugins/server/tests/configs/.rr-no-app-section.yaml index d129ae8a..b6e3ea93 100644 --- a/plugins/app/tests/configs/.rr-no-app-section.yaml +++ b/plugins/server/tests/configs/.rr-no-app-section.yaml @@ -1,4 +1,4 @@ -upp: +server: command: "php ../../../tests/client.php echo pipes" user: "" group: "" diff --git a/plugins/app/tests/configs/.rr-sockets.yaml b/plugins/server/tests/configs/.rr-sockets.yaml index 9bd62693..ab1239aa 100644 --- a/plugins/app/tests/configs/.rr-sockets.yaml +++ b/plugins/server/tests/configs/.rr-sockets.yaml @@ -1,4 +1,4 @@ -app: +server: command: "php socket.php" user: "" group: "" diff --git a/plugins/app/tests/configs/.rr-tcp.yaml b/plugins/server/tests/configs/.rr-tcp.yaml index c5a26d37..f53bffcc 100644 --- a/plugins/app/tests/configs/.rr-tcp.yaml +++ b/plugins/server/tests/configs/.rr-tcp.yaml @@ -1,4 +1,4 @@ -app: +server: command: "php tcp.php" user: "" group: "" diff --git a/plugins/app/tests/configs/.rr-wrong-command.yaml b/plugins/server/tests/configs/.rr-wrong-command.yaml index 4bd019d3..d2c087a6 100644 --- a/plugins/app/tests/configs/.rr-wrong-command.yaml +++ b/plugins/server/tests/configs/.rr-wrong-command.yaml @@ -1,4 +1,4 @@ -app: +server: command: "php some_absent_file.php" user: "" group: "" diff --git a/plugins/app/tests/configs/.rr-wrong-relay.yaml b/plugins/server/tests/configs/.rr-wrong-relay.yaml index d8ffe8f8..1dd73d73 100644 --- a/plugins/app/tests/configs/.rr-wrong-relay.yaml +++ b/plugins/server/tests/configs/.rr-wrong-relay.yaml @@ -1,4 +1,4 @@ -app: +server: command: "php ../../../tests/client.php echo pipes" user: "" group: "" diff --git a/plugins/app/tests/configs/.rr.yaml b/plugins/server/tests/configs/.rr.yaml index 221aff92..b6e3ea93 100644 --- a/plugins/app/tests/configs/.rr.yaml +++ b/plugins/server/tests/configs/.rr.yaml @@ -1,4 +1,4 @@ -app: +server: command: "php ../../../tests/client.php echo pipes" user: "" group: "" diff --git a/plugins/app/tests/plugin_pipes.go b/plugins/server/tests/plugin_pipes.go index 150ab297..840021eb 100644 --- a/plugins/app/tests/plugin_pipes.go +++ b/plugins/server/tests/plugin_pipes.go @@ -6,8 +6,9 @@ import ( "github.com/spiral/errors" "github.com/spiral/roadrunner/v2" - "github.com/spiral/roadrunner/v2/plugins/app" + "github.com/spiral/roadrunner/v2/interfaces/server" "github.com/spiral/roadrunner/v2/plugins/config" + plugin "github.com/spiral/roadrunner/v2/plugins/server" ) const ConfigSection = "app" @@ -29,11 +30,11 @@ var testPoolConfig = roadrunner.PoolConfig{ type Foo struct { configProvider config.Configurer - wf app.WorkerFactory + wf server.WorkerFactory pool roadrunner.Pool } -func (f *Foo) Init(p config.Configurer, workerFactory app.WorkerFactory) error { +func (f *Foo) Init(p config.Configurer, workerFactory server.WorkerFactory) error { f.configProvider = p f.wf = workerFactory return nil @@ -50,7 +51,7 @@ func (f *Foo) Serve() chan error { errCh := make(chan error, 1) - conf := &app.Config{} + conf := &plugin.Config{} var err error err = f.configProvider.UnmarshalKey(ConfigSection, conf) if err != nil { diff --git a/plugins/app/tests/plugin_sockets.go b/plugins/server/tests/plugin_sockets.go index 585264f6..b12f4ead 100644 --- a/plugins/app/tests/plugin_sockets.go +++ b/plugins/server/tests/plugin_sockets.go @@ -5,17 +5,18 @@ import ( "github.com/spiral/errors" "github.com/spiral/roadrunner/v2" - "github.com/spiral/roadrunner/v2/plugins/app" + "github.com/spiral/roadrunner/v2/interfaces/server" "github.com/spiral/roadrunner/v2/plugins/config" + plugin "github.com/spiral/roadrunner/v2/plugins/server" ) type Foo2 struct { configProvider config.Configurer - wf app.WorkerFactory + wf server.WorkerFactory pool roadrunner.Pool } -func (f *Foo2) Init(p config.Configurer, workerFactory app.WorkerFactory) error { +func (f *Foo2) Init(p config.Configurer, workerFactory server.WorkerFactory) error { f.configProvider = p f.wf = workerFactory return nil @@ -25,7 +26,7 @@ func (f *Foo2) Serve() chan error { const op = errors.Op("serve") var err error errCh := make(chan error, 1) - conf := &app.Config{} + conf := &plugin.Config{} // test payload for echo r := roadrunner.Payload{ diff --git a/plugins/app/tests/plugin_tcp.go b/plugins/server/tests/plugin_tcp.go index 6abc533d..39044577 100644 --- a/plugins/app/tests/plugin_tcp.go +++ b/plugins/server/tests/plugin_tcp.go @@ -5,17 +5,18 @@ import ( "github.com/spiral/errors" "github.com/spiral/roadrunner/v2" - "github.com/spiral/roadrunner/v2/plugins/app" + "github.com/spiral/roadrunner/v2/interfaces/server" "github.com/spiral/roadrunner/v2/plugins/config" + plugin "github.com/spiral/roadrunner/v2/plugins/server" ) type Foo3 struct { configProvider config.Configurer - wf app.WorkerFactory + wf server.WorkerFactory pool roadrunner.Pool } -func (f *Foo3) Init(p config.Configurer, workerFactory app.WorkerFactory) error { +func (f *Foo3) Init(p config.Configurer, workerFactory server.WorkerFactory) error { f.configProvider = p f.wf = workerFactory return nil @@ -25,7 +26,7 @@ func (f *Foo3) Serve() chan error { const op = errors.Op("serve") var err error errCh := make(chan error, 1) - conf := &app.Config{} + conf := &plugin.Config{} // test payload for echo r := roadrunner.Payload{ diff --git a/plugins/app/tests/app_test.go b/plugins/server/tests/server_test.go index 3c416b59..f917df5d 100644 --- a/plugins/app/tests/app_test.go +++ b/plugins/server/tests/server_test.go @@ -7,9 +7,9 @@ import ( "time" "github.com/spiral/endure" - "github.com/spiral/roadrunner/v2/plugins/app" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/logger" + "github.com/spiral/roadrunner/v2/plugins/server" "github.com/stretchr/testify/assert" ) @@ -27,7 +27,7 @@ func TestAppPipes(t *testing.T) { t.Fatal(err) } - err = container.Register(&app.Plugin{}) + err = container.Register(&server.Plugin{}) if err != nil { t.Fatal(err) } @@ -93,7 +93,7 @@ func TestAppSockets(t *testing.T) { t.Fatal(err) } - err = container.Register(&app.Plugin{}) + err = container.Register(&server.Plugin{}) if err != nil { t.Fatal(err) } @@ -159,7 +159,7 @@ func TestAppTCP(t *testing.T) { t.Fatal(err) } - err = container.Register(&app.Plugin{}) + err = container.Register(&server.Plugin{}) if err != nil { t.Fatal(err) } @@ -225,7 +225,7 @@ func TestAppWrongConfig(t *testing.T) { t.Fatal(err) } - err = container.Register(&app.Plugin{}) + err = container.Register(&server.Plugin{}) if err != nil { t.Fatal(err) } @@ -257,7 +257,7 @@ func TestAppWrongRelay(t *testing.T) { t.Fatal(err) } - err = container.Register(&app.Plugin{}) + err = container.Register(&server.Plugin{}) if err != nil { t.Fatal(err) } @@ -295,7 +295,7 @@ func TestAppWrongCommand(t *testing.T) { t.Fatal(err) } - err = container.Register(&app.Plugin{}) + err = container.Register(&server.Plugin{}) if err != nil { t.Fatal(err) } @@ -333,7 +333,7 @@ func TestAppNoAppSectionInConfig(t *testing.T) { t.Fatal(err) } - err = container.Register(&app.Plugin{}) + err = container.Register(&server.Plugin{}) if err != nil { t.Fatal(err) } diff --git a/plugins/app/tests/socket.php b/plugins/server/tests/socket.php index 143c3ce4..143c3ce4 100644 --- a/plugins/app/tests/socket.php +++ b/plugins/server/tests/socket.php diff --git a/plugins/app/tests/tcp.php b/plugins/server/tests/tcp.php index 2d6fb00a..2d6fb00a 100644 --- a/plugins/app/tests/tcp.php +++ b/plugins/server/tests/tcp.php |