diff options
author | Wolfy-J <[email protected]> | 2019-06-27 13:18:42 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-06-27 13:18:42 +0300 |
commit | 11870a2df3ccd8f79f131354ba106e557c248b48 (patch) | |
tree | 80ec8f7e2de0fe7508b73287affd02c769210a61 /service/metrics | |
parent | c4a260cacc43bd8c5b87cce890ae582124ef3509 (diff) |
rpc test
Diffstat (limited to 'service/metrics')
-rw-r--r-- | service/metrics/rpc.go | 34 | ||||
-rw-r--r-- | service/metrics/rpc_test.go | 54 |
2 files changed, 79 insertions, 9 deletions
diff --git a/service/metrics/rpc.go b/service/metrics/rpc.go index 30ad6c62..ca99d4ac 100644 --- a/service/metrics/rpc.go +++ b/service/metrics/rpc.go @@ -15,12 +15,18 @@ type Metric struct { // Collector value. Value float64 - // Labels associated with metric. Only for vector metrics. + // Labels associated with metric. Only for vector metrics. Must be provided in a form of label values. Labels []string } // Add new metric to the designated collector. -func (rpc *rpcServer) Add(m *Metric, ok *bool) error { +func (rpc *rpcServer) Add(m *Metric, ok *bool) (err error) { + defer func() { + if r, fail := recover().(error); fail { + err = r + } + }() + c := rpc.svc.Collector(m.Name) if c == nil { return fmt.Errorf("undefined collector `%s`", m.Name) @@ -73,7 +79,13 @@ func (rpc *rpcServer) Add(m *Metric, ok *bool) error { } // Sub subtract the value from the specific metric (gauge only). -func (rpc *rpcServer) Sub(m *Metric, ok *bool) error { +func (rpc *rpcServer) Sub(m *Metric, ok *bool) (err error) { + defer func() { + if r, fail := recover().(error); fail { + err = r + } + }() + c := rpc.svc.Collector(m.Name) if c == nil { return fmt.Errorf("undefined collector `%s`", m.Name) @@ -98,7 +110,13 @@ func (rpc *rpcServer) Sub(m *Metric, ok *bool) error { } // Observe the value (histogram and summary only). -func (rpc *rpcServer) Observe(m *Metric, ok *bool) error { +func (rpc *rpcServer) Observe(m *Metric, ok *bool) (err error) { + defer func() { + if r, fail := recover().(error); fail { + err = r + } + }() + c := rpc.svc.Collector(m.Name) if c == nil { return fmt.Errorf("undefined collector `%s`", m.Name) @@ -130,7 +148,13 @@ func (rpc *rpcServer) Observe(m *Metric, ok *bool) error { } // Set the metric value (only for gaude). -func (rpc *rpcServer) Set(m *Metric, ok *bool) error { +func (rpc *rpcServer) Set(m *Metric, ok *bool) (err error) { + defer func() { + if r, fail := recover().(error); fail { + err = r + } + }() + c := rpc.svc.Collector(m.Name) if c == nil { return fmt.Errorf("undefined collector `%s`", m.Name) diff --git a/service/metrics/rpc_test.go b/service/metrics/rpc_test.go index 9668c203..78bf3e20 100644 --- a/service/metrics/rpc_test.go +++ b/service/metrics/rpc_test.go @@ -24,7 +24,8 @@ func Test_Set_RPC_Metric(t *testing.T) { "address": "localhost:2112", "collect":{ "user_gauge_2":{ - "type": "gauge" + "type": "gauge", + "labels": ["type", "section"] } } @@ -47,12 +48,57 @@ func Test_Set_RPC_Metric(t *testing.T) { var ok bool assert.NoError(t, client.Call("metrics.Set", Metric{ - Name: "user_gauge_2", - Value: 100.0, + Name: "user_gauge_2", + Value: 100.0, + Labels: []string{"core", "first"}, }, &ok)) assert.True(t, ok) out, _, err := get("http://localhost:2112/metrics") assert.NoError(t, err) - assert.Contains(t, out, "user_gauge_2 100") + assert.Contains(t, out, `user_gauge_2{section="first",type="core"} 100`) +} + +func Test_Set_RPC_MetricError(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_3":{ + "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_3", + Value: 100.0, + Labels: []string{"missing"}, + }, &ok)) } |