diff options
Diffstat (limited to 'service/metrics/rpc_test.go')
-rw-r--r-- | service/metrics/rpc_test.go | 102 |
1 files changed, 96 insertions, 6 deletions
diff --git a/service/metrics/rpc_test.go b/service/metrics/rpc_test.go index 78bf3e20..0eed52fe 100644 --- a/service/metrics/rpc_test.go +++ b/service/metrics/rpc_test.go @@ -10,7 +10,7 @@ import ( "time" ) -func Test_Set_RPC_Metric(t *testing.T) { +func Test_Set_RPC(t *testing.T) { logger, _ := test.NewNullLogger() logger.SetLevel(logrus.DebugLevel) @@ -23,7 +23,54 @@ func Test_Set_RPC_Metric(t *testing.T) { metricsCfg: `{ "address": "localhost:2112", "collect":{ - "user_gauge_2":{ + "user_gauge":{ + "type": "gauge" + } + } + + }`})) + + s, _ := c.Get(ID) + assert.NotNil(t, s) + + s2, _ := c.Get(rpc.ID) + rs := s2.(*rpc.Service) + + assert.True(t, s.(*Service).Enabled()) + + go func() { c.Serve() }() + time.Sleep(time.Millisecond * 100) + defer c.Stop() + + client, err := rs.Client() + assert.NoError(t, err) + + 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) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(rpc.ID, &rpc.Service{}) + c.Register(ID, &Service{}) + + assert.NoError(t, c.Init(&testCfg{ + rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, + metricsCfg: `{ + "address": "localhost:2112", + "collect":{ + "user_gauge":{ "type": "gauge", "labels": ["type", "section"] } @@ -48,7 +95,7 @@ func Test_Set_RPC_Metric(t *testing.T) { var ok bool assert.NoError(t, client.Call("metrics.Set", Metric{ - Name: "user_gauge_2", + Name: "user_gauge", Value: 100.0, Labels: []string{"core", "first"}, }, &ok)) @@ -56,7 +103,50 @@ func Test_Set_RPC_Metric(t *testing.T) { out, _, err := get("http://localhost:2112/metrics") assert.NoError(t, err) - assert.Contains(t, out, `user_gauge_2{section="first",type="core"} 100`) + assert.Contains(t, out, `user_gauge{section="first",type="core"} 100`) +} + +func Test_Set_RPC_CollectorError(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(rpc.ID, &rpc.Service{}) + c.Register(ID, &Service{}) + + assert.NoError(t, c.Init(&testCfg{ + rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`, + metricsCfg: `{ + "address": "localhost:2112", + "collect":{ + "user_gauge":{ + "type": "gauge", + "labels": ["type", "section"] + } + } + }`})) + + s, _ := c.Get(ID) + assert.NotNil(t, s) + + s2, _ := c.Get(rpc.ID) + rs := s2.(*rpc.Service) + + assert.True(t, s.(*Service).Enabled()) + + go func() { c.Serve() }() + time.Sleep(time.Millisecond * 100) + defer c.Stop() + + client, err := rs.Client() + assert.NoError(t, err) + + 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) { @@ -72,7 +162,7 @@ func Test_Set_RPC_MetricError(t *testing.T) { metricsCfg: `{ "address": "localhost:2112", "collect":{ - "user_gauge_3":{ + "user_gauge":{ "type": "gauge", "labels": ["type", "section"] } @@ -97,7 +187,7 @@ func Test_Set_RPC_MetricError(t *testing.T) { var ok bool assert.Error(t, client.Call("metrics.Set", Metric{ - Name: "user_gauge_3", + Name: "user_gauge", Value: 100.0, Labels: []string{"missing"}, }, &ok)) |