diff options
author | Valery Piashchynski <[email protected]> | 2020-11-14 17:18:28 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-11-14 17:18:28 +0300 |
commit | e45155e77e99e85e4ee9cd8863365bf6d599796e (patch) | |
tree | fe22608b4206fcab3219b824b5bd8cfaf5cfb85f | |
parent | e83f7d824e5f491fb50b4a9024ef7862aee9b6ca (diff) |
More tests for the Metrics
-rwxr-xr-x | go.mod | 5 | ||||
-rw-r--r-- | plugins/metrics/plugin.go | 7 | ||||
-rw-r--r-- | plugins/metrics/plugin_test.go | 91 | ||||
-rw-r--r-- | plugins/metrics/rpc.go | 9 | ||||
-rw-r--r-- | plugins/metrics/rpc_test.go | 228 | ||||
-rw-r--r-- | plugins/metrics/tests/metrics_test.go | 467 | ||||
-rw-r--r-- | plugins/metrics/tests/plugin2.go | 45 |
7 files changed, 460 insertions, 392 deletions
@@ -8,7 +8,6 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.7.1 github.com/shirou/gopsutil v3.20.10+incompatible - github.com/sirupsen/logrus v1.6.0 github.com/spf13/viper v1.7.1 github.com/spiral/endure v1.0.0-beta18 github.com/spiral/errors v1.0.4 @@ -21,3 +20,7 @@ require ( golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 ) + +replace ( + github.com/spiral/endure v1.0.0-beta18 => /home/valery/Projects/opensource/spiral/endure +)
\ No newline at end of file diff --git a/plugins/metrics/plugin.go b/plugins/metrics/plugin.go index ff075bc6..8ffd19d5 100644 --- a/plugins/metrics/plugin.go +++ b/plugins/metrics/plugin.go @@ -1,7 +1,5 @@ package metrics -// todo: declare metric at runtime - import ( "context" "crypto/tls" @@ -222,5 +220,8 @@ func (m *Plugin) Name() string { // RPC interface satisfaction func (m *Plugin) RPC() interface{} { - return &rpcServer{svc: m} + return &rpcServer{ + svc: m, + log: m.log, + } } diff --git a/plugins/metrics/plugin_test.go b/plugins/metrics/plugin_test.go index 34f5869d..fa622811 100644 --- a/plugins/metrics/plugin_test.go +++ b/plugins/metrics/plugin_test.go @@ -1,96 +1,5 @@ package metrics -// type testCfg struct { -// rpcCfg string -// metricsCfg string -// target string -// } -// -// func (cfg *testCfg) Get(name string) service.Config { -// if name == ID { -// return &testCfg{target: cfg.metricsCfg} -// } -// -// if name == rpc.ID { -// return &testCfg{target: cfg.rpcCfg} -// } -// -// return nil -// } -// -// func (cfg *testCfg) Unmarshal(out interface{}) error { -// j := json.ConfigCompatibleWithStandardLibrary -// err := j.Unmarshal([]byte(cfg.target), out) -// return err -// } -// -// func TestService_Serve(t *testing.T) { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Plugin{}) -// -// assert.NoError(t, c.Init(&testCfg{metricsCfg: `{ -// "address": "localhost:2116" -// }`})) -// -// s, _ := c.Get(ID) -// assert.NotNil(t, s) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("error during the Serve: error %v", err) -// } -// }() -// time.Sleep(time.Millisecond * 100) -// defer c.Stop() -// -// out, _, err := get("http://localhost:2116/metrics") -// assert.NoError(t, err) -// -// assert.Contains(t, out, "go_gc_duration_seconds") -// } -// -// func Test_ServiceCustomMetric(t *testing.T) { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Plugin{}) -// -// assert.NoError(t, c.Init(&testCfg{metricsCfg: `{ -// "address": "localhost:2115" -// }`})) -// -// s, _ := c.Get(ID) -// assert.NotNil(t, s) -// -// collector := prometheus.NewGauge(prometheus.GaugeOpts{ -// Name: "my_gauge", -// Help: "My gauge value", -// }) -// -// assert.NoError(t, s.(*Plugin).Register(collector)) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("error during the Serve: error %v", err) -// } -// }() -// time.Sleep(time.Millisecond * 100) -// defer c.Stop() -// -// collector.Set(100) -// -// out, _, err := get("http://localhost:2115/metrics") -// assert.NoError(t, err) -// -// assert.Contains(t, out, "my_gauge 100") -// } -// // func Test_ServiceCustomMetricMust(t *testing.T) { // logger, _ := test.NewNullLogger() // logger.SetLevel(logrus.DebugLevel) diff --git a/plugins/metrics/rpc.go b/plugins/metrics/rpc.go index 9799db3f..d0f071ef 100644 --- a/plugins/metrics/rpc.go +++ b/plugins/metrics/rpc.go @@ -3,10 +3,12 @@ package metrics import ( "github.com/prometheus/client_golang/prometheus" "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2/interfaces/log" ) type rpcServer struct { svc *Plugin + log log.Logger } // Metric represent single metric produced by the application. @@ -24,9 +26,11 @@ type Metric struct { // Add new metric to the designated collector. func (rpc *rpcServer) Add(m *Metric, ok *bool) error { const op = errors.Op("Add metric") + rpc.log.Info("Adding metric", "name", m.Name, "value", m.Value, "labels", m.Labels) c, exist := rpc.svc.collectors.Load(m.Name) if !exist { - return errors.E(op, errors.Errorf("undefined collector `%s`", m.Name)) + rpc.log.Error("undefined collector", "collector", m.Name) + return errors.E(op, errors.Errorf("undefined collector %s, try first Declare the desired collector", m.Name)) } switch c := c.(type) { @@ -56,6 +60,7 @@ func (rpc *rpcServer) Add(m *Metric, ok *bool) error { // RPC, set ok to true as return value. Need by rpc.Call reply argument *ok = true + rpc.log.Info("new metric successfully added") return nil } @@ -197,7 +202,7 @@ func (rpc *rpcServer) Declare(nc *NamedCollector, ok *bool) error { } default: - return errors.E(op, errors.Errorf("unknown collector type `%s`", nc.Type)) + return errors.E(op, errors.Errorf("unknown collector type %s", nc.Type)) } // add collector to sync.Map diff --git a/plugins/metrics/rpc_test.go b/plugins/metrics/rpc_test.go index b29ccaec..aab4a2ec 100644 --- a/plugins/metrics/rpc_test.go +++ b/plugins/metrics/rpc_test.go @@ -12,234 +12,12 @@ package metrics // "time" // ) // -// var port = 5004 // -// func setup(t *testing.T, metric string, portNum string) (*rpc2.Client, service.Container) { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(rpc.ID, &rpc.Plugin{}) -// c.Register(ID, &Plugin{}) -// -// assert.NoError(t, c.Init(&testCfg{ -// rpcCfg: `{"enable":true, "listen":"tcp://:` + strconv.Itoa(port) + `"}`, -// metricsCfg: `{ -// "address": "localhost:` + portNum + `", -// "collect":{ -// ` + metric + ` -// } -// }`})) -// -// // rotate ports for travis -// port++ -// -// s, _ := c.Get(ID) -// assert.NotNil(t, s) -// -// s2, _ := c.Get(rpc.ID) -// rs := s2.(*rpc.Plugin) -// -// assert.True(t, s.(*Plugin).Enabled()) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("error during the Serve: error %v", err) -// } -// }() -// time.Sleep(time.Millisecond * 200) -// -// client, err := rs.Client() -// assert.NoError(t, err) -// if err != nil { -// panic(err) -// } -// -// return client, c -// } -// -// func Test_Set_RPC(t *testing.T) { -// client, c := setup( -// t, -// `"user_gauge":{ -// "type": "gauge" -// }`, -// "2112", -// ) -// defer c.Stop() -// -// var ok bool -// assert.NoError(t, client.Call("metrics.Set", Metric{ -// Name: "user_gauge", -// Value: 100.0, -// }, &ok)) -// assert.True(t, ok) -// -// out, _, err := get("http://localhost:2112/metrics") -// assert.NoError(t, err) -// assert.Contains(t, out, `user_gauge 100`) -// } -// -// func Test_Set_RPC_Vector(t *testing.T) { -// client, c := setup( -// t, -// `"user_gauge":{ -// "type": "gauge", -// "labels": ["type", "section"] -// }`, -// "2113", -// ) -// defer c.Stop() -// -// var ok bool -// assert.NoError(t, client.Call("metrics.Set", Metric{ -// Name: "user_gauge", -// Value: 100.0, -// Labels: []string{"core", "first"}, -// }, &ok)) -// assert.True(t, ok) -// -// out, _, err := get("http://localhost:2113/metrics") -// assert.NoError(t, err) -// assert.Contains(t, out, `user_gauge{section="first",type="core"} 100`) -// } -// -// func Test_Set_RPC_CollectorError(t *testing.T) { -// client, c := setup( -// t, -// `"user_gauge":{ -// "type": "gauge", -// "labels": ["type", "section"] -// }`, -// "2114", -// ) -// defer c.Stop() -// -// var ok bool -// assert.Error(t, client.Call("metrics.Set", Metric{ -// Name: "user_gauge_2", -// Value: 100.0, -// Labels: []string{"missing"}, -// }, &ok)) -//} -// -// func Test_Set_RPC_MetricError(t *testing.T) { -// client, c := setup( -// t, -// `"user_gauge":{ -// "type": "gauge", -// "labels": ["type", "section"] -// }`, -// "2115", -// ) -// defer c.Stop() -// -// var ok bool -// assert.Error(t, client.Call("metrics.Set", Metric{ -// Name: "user_gauge", -// Value: 100.0, -// Labels: []string{"missing"}, -// }, &ok)) -// } -// -// func Test_Set_RPC_MetricError_2(t *testing.T) { -// client, c := setup( -// t, -// `"user_gauge":{ -// "type": "gauge", -// "labels": ["type", "section"] -// }`, -// "2116", -// ) -// defer c.Stop() -// -// var ok bool -// assert.Error(t, client.Call("metrics.Set", Metric{ -// Name: "user_gauge", -// Value: 100.0, -// }, &ok)) -// } -// -// func Test_Set_RPC_MetricError_3(t *testing.T) { -// client, c := setup( -// t, -// `"user_gauge":{ -// "type": "histogram", -// "labels": ["type", "section"] -// }`, -// "2117", -// ) -// defer c.Stop() -// -// var ok bool -// assert.Error(t, client.Call("metrics.Set", Metric{ -// Name: "user_gauge", -// Value: 100.0, -// }, &ok)) -// } -// -// // sub -// -// func Test_Sub_RPC(t *testing.T) { -// client, c := setup( -// t, -// `"user_gauge":{ -// "type": "gauge" -// }`, -// "2118", -// ) -// defer c.Stop() -// -// var ok bool -// assert.NoError(t, client.Call("metrics.Add", Metric{ -// Name: "user_gauge", -// Value: 100.0, -// }, &ok)) -// assert.True(t, ok) -// -// assert.NoError(t, client.Call("metrics.Sub", Metric{ -// Name: "user_gauge", -// Value: 10.0, -// }, &ok)) -// assert.True(t, ok) -// -// out, _, err := get("http://localhost:2118/metrics") -// assert.NoError(t, err) -// assert.Contains(t, out, `user_gauge 90`) -// } -// -// func Test_Sub_RPC_Vector(t *testing.T) { -// client, c := setup( -// t, -// `"user_gauge":{ -// "type": "gauge", -// "labels": ["type", "section"] -// }`, -// "2119", -// ) -// defer c.Stop() -// -// var ok bool -// assert.NoError(t, client.Call("metrics.Add", Metric{ -// Name: "user_gauge", -// Value: 100.0, -// Labels: []string{"core", "first"}, -// }, &ok)) -// assert.True(t, ok) -// -// assert.NoError(t, client.Call("metrics.Sub", Metric{ -// Name: "user_gauge", -// Value: 10.0, -// Labels: []string{"core", "first"}, -// }, &ok)) -// assert.True(t, ok) + // -// out, _, err := get("http://localhost:2119/metrics") -// assert.NoError(t, err) -// assert.Contains(t, out, `user_gauge{section="first",type="core"} 90`) -// } + // + // func Test_Register_RPC_Histogram(t *testing.T) { // client, c := setup( // t, diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index 2df011e6..860528f8 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -2,7 +2,9 @@ package tests import ( "io/ioutil" + "net" "net/http" + "net/rpc" "os" "os/signal" "syscall" @@ -10,10 +12,11 @@ import ( "time" "github.com/spiral/endure" + "github.com/spiral/goridge/v2" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/metrics" - "github.com/spiral/roadrunner/v2/plugins/rpc" + rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc" "github.com/stretchr/testify/assert" ) @@ -57,7 +60,7 @@ func TestMetricsInit(t *testing.T) { t.Fatal(err) } - err = cont.Register(&rpc.Plugin{}) + err = cont.Register(&rpcPlugin.Plugin{}) if err != nil { t.Fatal(err) } @@ -124,29 +127,14 @@ func TestMetricsGaugeCollector(t *testing.T) { cfg.Prefix = "rr" cfg.Path = ".rr-test.yaml" - err = cont.Register(cfg) - if err != nil { - t.Fatal(err) - } - - err = cont.Register(&metrics.Plugin{}) - if err != nil { - t.Fatal(err) - } - - err = cont.Register(&rpc.Plugin{}) - if err != nil { - t.Fatal(err) - } - - err = cont.Register(&logger.ZapLogger{}) - if err != nil { - t.Fatal(err) - } - err = cont.Register(&Plugin1{}) - if err != nil { - t.Fatal(err) - } + err = cont.RegisterAll( + cfg, + &metrics.Plugin{}, + &rpcPlugin.Plugin{}, + &logger.ZapLogger{}, + &Plugin1{}, + ) + assert.NoError(t, err) err = cont.Init() if err != nil { @@ -166,6 +154,10 @@ func TestMetricsGaugeCollector(t *testing.T) { assert.NoError(t, err) assert.Contains(t, out, "my_gauge 100") + genericOut, err := get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "go_gc_duration_seconds") + for { select { case e := <-ch: @@ -190,3 +182,428 @@ func TestMetricsGaugeCollector(t *testing.T) { } } } + +func TestMetricsDifferentRPCCalls(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) + if err != nil { + t.Fatal(err) + } + + cfg := &config.Viper{} + cfg.Prefix = "rr" + cfg.Path = ".rr-test.yaml" + + err = cont.RegisterAll( + cfg, + &metrics.Plugin{}, + &rpcPlugin.Plugin{}, + &logger.ZapLogger{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + go func() { + tt := time.NewTimer(time.Minute * 3) + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-tt.C: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + t.Run("DeclareMetric", declareMetricsTest) + genericOut, err := get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "test_metrics_named_collector") + + t.Run("AddMetric", addMetricsTest) + genericOut, err = get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "test_metrics_named_collector 10000") + + t.Run("SetMetric", setMetric) + genericOut, err = get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "user_gauge_collector 100") + + t.Run("VectorMetric", vectorMetric) + genericOut, err = get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "gauge_2_collector{section=\"first\",type=\"core\"} 100") + + t.Run("MissingSection", missingSection) + t.Run("SetWithoutLabels", setWithoutLabels) + t.Run("SetOnHistogram", setOnHistogram) + t.Run("MetricSub", subMetric) + genericOut, err = get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "sub_gauge_subMetric 1") + + t.Run("SubVector", subVector) + genericOut, err = get("http://localhost:2112/metrics") + assert.NoError(t, err) + assert.Contains(t, genericOut, "sub_gauge_subVector{section=\"first\",type=\"core\"} 1") + + close(sig) +} + +func subVector(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "sub_gauge_subVector", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Labels: []string{"type", "section"}, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + m := metrics.Metric{ + Name: "sub_gauge_subVector", + Value: 100000, + Labels: []string{"core", "first"}, + } + + err = client.Call("metrics.Add", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + m = metrics.Metric{ + Name: "sub_gauge_subVector", + Value: 99999, + Labels: []string{"core", "first"}, + } + + err = client.Call("metrics.Sub", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} + +func subMetric(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "sub_gauge_subMetric", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Help: "NO HELP!", + Labels: nil, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + m := metrics.Metric{ + Name: "sub_gauge_subMetric", + Value: 100000, + } + + err = client.Call("metrics.Add", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + m = metrics.Metric{ + Name: "sub_gauge_subMetric", + Value: 99999, + } + + err = client.Call("metrics.Sub", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} + +func setOnHistogram(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "histogram_setOnHistogram", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Histogram, + Help: "NO HELP!", + Labels: []string{"type", "section"}, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + + ret = false + + m := metrics.Metric{ + Name: "gauge_setOnHistogram", + Value: 100.0, + } + + err = client.Call("metrics.Set", m, &ret) // expected 2 label values but got 1 in []string{"missing"} + assert.Error(t, err) + assert.False(t, ret) +} + +func setWithoutLabels(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "gauge_setWithoutLabels", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Help: "NO HELP!", + Labels: []string{"type", "section"}, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + + ret = false + + m := metrics.Metric{ + Name: "gauge_setWithoutLabels", + Value: 100.0, + } + + err = client.Call("metrics.Set", m, &ret) // expected 2 label values but got 1 in []string{"missing"} + assert.Error(t, err) + assert.False(t, ret) +} + +func missingSection(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "gauge_missing_section_collector", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Help: "NO HELP!", + Labels: []string{"type", "section"}, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + + ret = false + + m := metrics.Metric{ + Name: "gauge_missing_section_collector", + Value: 100.0, + Labels: []string{"missing"}, + } + + err = client.Call("metrics.Set", m, &ret) // expected 2 label values but got 1 in []string{"missing"} + assert.Error(t, err) + assert.False(t, ret) +} + +func vectorMetric(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "gauge_2_collector", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Help: "NO HELP!", + Labels: []string{"type", "section"}, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + + ret = false + + m := metrics.Metric{ + Name: "gauge_2_collector", + Value: 100.0, + Labels: []string{"core", "first"}, + } + + err = client.Call("metrics.Set", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} + +func setMetric(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "user_gauge_collector", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Gauge, + Help: "NO HELP!", + Labels: nil, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) + ret = false + + m := metrics.Metric{ + Name: "user_gauge_collector", + Value: 100.0, + } + + err = client.Call("metrics.Set", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} + +func addMetricsTest(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + m := metrics.Metric{ + Name: "test_metrics_named_collector", + Value: 10000, + Labels: nil, + } + + err = client.Call("metrics.Add", m, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} + +func declareMetricsTest(t *testing.T) { + time.Sleep(time.Second * 1) + + conn, err := net.Dial("tcp", "127.0.0.1:6001") + assert.NoError(t, err) + defer conn.Close() + + client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + var ret bool + + nc := metrics.NamedCollector{ + Name: "test_metrics_named_collector", + Collector: metrics.Collector{ + Namespace: "default", + Subsystem: "default", + Type: metrics.Counter, + Help: "NO HELP!", + Labels: nil, + Buckets: nil, + }, + } + + err = client.Call("metrics.Declare", nc, &ret) + assert.NoError(t, err) + assert.True(t, ret) +} diff --git a/plugins/metrics/tests/plugin2.go b/plugins/metrics/tests/plugin2.go deleted file mode 100644 index 5d36b729..00000000 --- a/plugins/metrics/tests/plugin2.go +++ /dev/null @@ -1,45 +0,0 @@ -package tests - -// plugin2 makes a call to the plugin1 via RPC -// this is just a simulation of external call FOR TEST -// you don't need to do such things :) -type Plugin2 struct { -} - -func (p2 *Plugin2) Init() error { - return nil -} - -func (p2 *Plugin2) Serve() chan error { - errCh := make(chan error, 1) - - go func() { - // time.Sleep(time.Second * 3) - // - // conn, err := net.Dial("tcp", "127.0.0.1:6001") - // if err != nil { - // errCh <- errors.E(errors.Serve, err) - // return - // } - // client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) - // var ret string - // err = client.Call("metrics_test.plugin1.Hello", "Valery", &ret) - // if err != nil { - // errCh <- err - // return - // } - // if ret != "Hello, username: Valery" { - // errCh <- errors.E("wrong response") - // return - // } - // // to stop exec - // errCh <- errors.E(errors.Disabled) - return - }() - - return errCh -} - -func (p2 *Plugin2) Stop() error { - return nil -} |