summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--codecov.yml4
-rw-r--r--plugins/metrics/plugin_test.go123
-rw-r--r--plugins/metrics/rpc.go12
-rw-r--r--plugins/metrics/rpc_test.go152
-rw-r--r--plugins/metrics/tests/metrics_test.go163
5 files changed, 177 insertions, 277 deletions
diff --git a/codecov.yml b/codecov.yml
new file mode 100644
index 00000000..3f7eef9a
--- /dev/null
+++ b/codecov.yml
@@ -0,0 +1,4 @@
+coverage:
+ status:
+ project: on
+ patch: off \ No newline at end of file
diff --git a/plugins/metrics/plugin_test.go b/plugins/metrics/plugin_test.go
deleted file mode 100644
index fa622811..00000000
--- a/plugins/metrics/plugin_test.go
+++ /dev/null
@@ -1,123 +0,0 @@
-package metrics
-
-// func Test_ServiceCustomMetricMust(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:2114"
-// }`}))
-//
-// s, _ := c.Get(ID)
-// assert.NotNil(t, s)
-//
-// collector := prometheus.NewGauge(prometheus.GaugeOpts{
-// Name: "my_gauge_2",
-// Help: "My gauge value",
-// })
-//
-// s.(*Plugin).MustRegister(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:2114/metrics")
-// assert.NoError(t, err)
-//
-// assert.Contains(t, out, "my_gauge_2 100")
-// }
-//
-// func Test_ConfiguredMetric(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:2113",
-// "collect":{
-// "user_gauge":{
-// "type": "gauge"
-// }
-// }
-// }`}))
-//
-// s, _ := c.Get(ID)
-// assert.NotNil(t, s)
-//
-// 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 * 100)
-// defer c.Stop()
-//
-// s.(*Plugin).Collector("user_gauge").(prometheus.Gauge).Set(100)
-//
-// assert.Nil(t, s.(*Plugin).Collector("invalid"))
-//
-// out, _, err := get("http://localhost:2113/metrics")
-// assert.NoError(t, err)
-//
-// assert.Contains(t, out, "user_gauge 100")
-// }
-//
-// func Test_ConfiguredDuplicateMetric(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:2112",
-// "collect":{
-// "go_gc_duration_seconds":{
-// "type": "gauge"
-// }
-// }
-// }`}))
-//
-// s, _ := c.Get(ID)
-// assert.NotNil(t, s)
-//
-// assert.True(t, s.(*Plugin).Enabled())
-//
-// assert.Error(t, c.Serve())
-// }
-//
-// func Test_ConfiguredInvalidMetric(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:2112",
-// "collect":{
-// "user_gauge":{
-// "type": "invalid"
-// }
-// }
-//
-// }`}))
-//
-// assert.Error(t, c.Serve())
-// }
diff --git a/plugins/metrics/rpc.go b/plugins/metrics/rpc.go
index d0f071ef..f6455bdf 100644
--- a/plugins/metrics/rpc.go
+++ b/plugins/metrics/rpc.go
@@ -112,7 +112,11 @@ func (rpc *rpcServer) Observe(m *Metric, ok *bool) error {
return errors.E(op, errors.Errorf("required labels for collector `%s`", m.Name))
}
- c.WithLabelValues(m.Labels...).Observe(m.Value)
+ observer, err := c.GetMetricWithLabelValues(m.Labels...)
+ if err != nil {
+ return errors.E(op, err)
+ }
+ observer.Observe(m.Value)
case prometheus.Histogram:
c.Observe(m.Value)
@@ -122,7 +126,11 @@ func (rpc *rpcServer) Observe(m *Metric, ok *bool) error {
return errors.E(op, errors.Errorf("required labels for collector `%s`", m.Name))
}
- c.WithLabelValues(m.Labels...).Observe(m.Value)
+ observer, err := c.GetMetricWithLabelValues(m.Labels...)
+ if err != nil {
+ return errors.E(op, err)
+ }
+ observer.Observe(m.Value)
default:
return errors.E(op, errors.Errorf("collector `%s` does not support method `Observe`", m.Name))
}
diff --git a/plugins/metrics/rpc_test.go b/plugins/metrics/rpc_test.go
index aab4a2ec..c3e3854f 100644
--- a/plugins/metrics/rpc_test.go
+++ b/plugins/metrics/rpc_test.go
@@ -1,157 +1,5 @@
package metrics
-// import (
-// "github.com/sirupsen/logrus"
-// "github.com/sirupsen/logrus/hooks/test"
-// "github.com/spiral/roadrunner/service"
-// "github.com/spiral/roadrunner/service/rpc"
-// "github.com/stretchr/testify/assert"
-// rpc2 "net/rpc"
-// "strconv"
-// "testing"
-// "time"
-// )
-//
-//
-
-//
-
-//
-
-// func Test_Register_RPC_Histogram(t *testing.T) {
-// client, c := setup(
-// t,
-// `"user_gauge":{
-// "type": "gauge",
-// "labels": ["type", "section"]
-// }`,
-// "2319",
-// )
-// defer c.Stop()
-//
-// var ok bool
-// assert.NoError(t, client.Call("metrics.Declare", &NamedCollector{
-// Name: "custom_histogram",
-// Collector: Collector{
-// Namespace: "test_histogram",
-// Subsystem: "test_histogram",
-// Type: Histogram,
-// Help: "test_histogram",
-// Labels: nil,
-// Buckets: []float64{0.1, 0.2, 0.5},
-// },
-// }, &ok))
-// assert.True(t, ok)
-//
-// var ok2 bool
-// // histogram does not support Add, should be an error
-// assert.Error(t, client.Call("metrics.Add", Metric{
-// Name: "custom_histogram",
-// }, &ok2))
-// // ok should became false
-// assert.False(t, ok2)
-//
-// out, _, err := get("http://localhost:2319/metrics")
-// assert.NoError(t, err)
-// assert.Contains(t, out, `TYPE test_histogram_test_histogram_custom_histogram histogram`)
-//
-// // check buckets
-// assert.Contains(t, out, `test_histogram_test_histogram_custom_histogram_bucket{le="0.1"} 0`)
-// assert.Contains(t, out, `test_histogram_test_histogram_custom_histogram_bucket{le="0.2"} 0`)
-// assert.Contains(t, out, `test_histogram_test_histogram_custom_histogram_bucket{le="0.5"} 0`)
-// }
-//
-// func Test_Register_RPC_Gauge(t *testing.T) {
-// // FOR register method, setup used just to init the rpc
-// client, c := setup(
-// t,
-// `"user_gauge":{
-// "type": "gauge",
-// "labels": ["type", "section"]
-// }`,
-// "2324",
-// )
-// defer c.Stop()
-//
-// var ok bool
-// assert.NoError(t, client.Call("metrics.Declare", &NamedCollector{
-// Name: "custom_gauge",
-// Collector: Collector{
-// Namespace: "test_gauge",
-// Subsystem: "test_gauge",
-// Type: Gauge,
-// Help: "test_gauge",
-// Labels: []string{"type", "section"},
-// Buckets: nil,
-// },
-// }, &ok))
-// assert.True(t, ok)
-//
-// var ok2 bool
-// // Add to custom_gauge
-// assert.NoError(t, client.Call("metrics.Add", Metric{
-// Name: "custom_gauge",
-// Value: 100.0,
-// Labels: []string{"core", "first"},
-// }, &ok2))
-// // ok should became true
-// assert.True(t, ok2)
-//
-// // Subtract from custom runtime metric
-// var ok3 bool
-// assert.NoError(t, client.Call("metrics.Sub", Metric{
-// Name: "custom_gauge",
-// Value: 10.0,
-// Labels: []string{"core", "first"},
-// }, &ok3))
-// assert.True(t, ok3)
-//
-// out, _, err := get("http://localhost:2324/metrics")
-// assert.NoError(t, err)
-// assert.Contains(t, out, `test_gauge_test_gauge_custom_gauge{section="first",type="core"} 90`)
-// }
-//
-// func Test_Register_RPC_Counter(t *testing.T) {
-// // FOR register method, setup used just to init the rpc
-// client, c := setup(
-// t,
-// `"user_gauge":{
-// "type": "gauge",
-// "labels": ["type", "section"]
-// }`,
-// "2328",
-// )
-// defer c.Stop()
-//
-// var ok bool
-// assert.NoError(t, client.Call("metrics.Declare", &NamedCollector{
-// Name: "custom_counter",
-// Collector: Collector{
-// Namespace: "test_counter",
-// Subsystem: "test_counter",
-// Type: Counter,
-// Help: "test_counter",
-// Labels: []string{"type", "section"},
-// Buckets: nil,
-// },
-// }, &ok))
-// assert.True(t, ok)
-//
-// var ok2 bool
-// // Add to custom_counter
-// assert.NoError(t, client.Call("metrics.Add", Metric{
-// Name: "custom_counter",
-// Value: 100.0,
-// Labels: []string{"type2", "section2"},
-// }, &ok2))
-// // ok should became true
-// assert.True(t, ok2)
-//
-// out, _, err := get("http://localhost:2328/metrics")
-// assert.NoError(t, err)
-// assert.Contains(t, out, `test_counter_test_counter_custom_counter{section="section2",type="type2"} 100`)
-// }
-//
// func Test_Register_RPC_Summary(t *testing.T) {
// // FOR register method, setup used just to init the rpc
// client, c := setup(
diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go
index 0eaa94a9..cee33514 100644
--- a/plugins/metrics/tests/metrics_test.go
+++ b/plugins/metrics/tests/metrics_test.go
@@ -276,9 +276,172 @@ func TestMetricsDifferentRPCCalls(t *testing.T) {
assert.NoError(t, err)
assert.Contains(t, genericOut, "sub_gauge_subVector{section=\"first\",type=\"core\"} 1")
+ t.Run("RegisterHistogram", registerHistogram)
+
+ genericOut, err = get(getAddr)
+ assert.NoError(t, err)
+ assert.Contains(t, genericOut, `TYPE histogram_registerHistogram`)
+
+ // check buckets
+ assert.Contains(t, genericOut, `histogram_registerHistogram_bucket{le="0.1"} 0`)
+ assert.Contains(t, genericOut, `histogram_registerHistogram_bucket{le="0.2"} 0`)
+ assert.Contains(t, genericOut, `histogram_registerHistogram_bucket{le="0.5"} 0`)
+ assert.Contains(t, genericOut, `histogram_registerHistogram_bucket{le="+Inf"} 0`)
+ assert.Contains(t, genericOut, `histogram_registerHistogram_sum 0`)
+ assert.Contains(t, genericOut, `histogram_registerHistogram_count 0`)
+
+ t.Run("CounterMetric", counterMetric)
+ genericOut, err = get(getAddr)
+ assert.NoError(t, err)
+ assert.Contains(t, genericOut, "HELP default_default_counter_CounterMetric test_counter")
+ assert.Contains(t, genericOut, `default_default_counter_CounterMetric{section="section2",type="type2"}`)
+
+ t.Run("ObserveMetric", observeMetric)
+ genericOut, err = get(getAddr)
+ assert.NoError(t, err)
+ assert.Contains(t, genericOut, "observe_observeMetric")
+
+ t.Run("ObserveMetricNotEnoughLabels", observeMetricNotEnoughLabels)
+
close(sig)
}
+func observeMetricNotEnoughLabels(t *testing.T) {
+ conn, err := net.Dial(dialNetwork, dialAddr)
+ assert.NoError(t, err)
+ defer func() {
+ _ = conn.Close()
+ }()
+ client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn))
+ var ret bool
+
+ nc := metrics.NamedCollector{
+ Name: "observe_observeMetricNotEnoughLabels",
+ Collector: metrics.Collector{
+ Namespace: "default",
+ Subsystem: "default",
+ Help: "test_observe",
+ Type: metrics.Histogram,
+ Labels: []string{"type", "section"},
+ },
+ }
+
+ err = client.Call("metrics.Declare", nc, &ret)
+ assert.NoError(t, err)
+ assert.True(t, ret)
+ ret = false
+
+ assert.Error(t, client.Call("metrics.Observe", metrics.Metric{
+ Name: "observe_observeMetric",
+ Value: 100.0,
+ Labels: []string{"test"},
+ }, &ret))
+ assert.False(t, ret)
+}
+
+func observeMetric(t *testing.T) {
+ conn, err := net.Dial(dialNetwork, dialAddr)
+ assert.NoError(t, err)
+ defer func() {
+ _ = conn.Close()
+ }()
+ client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn))
+ var ret bool
+
+ nc := metrics.NamedCollector{
+ Name: "observe_observeMetric",
+ Collector: metrics.Collector{
+ Namespace: "default",
+ Subsystem: "default",
+ Help: "test_observe",
+ Type: metrics.Histogram,
+ Labels: []string{"type", "section"},
+ },
+ }
+
+ err = client.Call("metrics.Declare", nc, &ret)
+ assert.NoError(t, err)
+ assert.True(t, ret)
+ ret = false
+
+ assert.NoError(t, client.Call("metrics.Observe", metrics.Metric{
+ Name: "observe_observeMetric",
+ Value: 100.0,
+ Labels: []string{"test", "test2"},
+ }, &ret))
+ assert.True(t, ret)
+}
+
+func counterMetric(t *testing.T) {
+ conn, err := net.Dial(dialNetwork, dialAddr)
+ assert.NoError(t, err)
+ defer func() {
+ _ = conn.Close()
+ }()
+ client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn))
+ var ret bool
+
+ nc := metrics.NamedCollector{
+ Name: "counter_CounterMetric",
+ Collector: metrics.Collector{
+ Namespace: "default",
+ Subsystem: "default",
+ Help: "test_counter",
+ Type: metrics.Counter,
+ Labels: []string{"type", "section"},
+ },
+ }
+
+ err = client.Call("metrics.Declare", nc, &ret)
+ assert.NoError(t, err)
+ assert.True(t, ret)
+
+ ret = false
+
+ assert.NoError(t, client.Call("metrics.Add", metrics.Metric{
+ Name: "counter_CounterMetric",
+ Value: 100.0,
+ Labels: []string{"type2", "section2"},
+ }, &ret))
+ assert.True(t, ret)
+}
+
+func registerHistogram(t *testing.T) {
+ conn, err := net.Dial(dialNetwork, dialAddr)
+ assert.NoError(t, err)
+ defer func() {
+ _ = conn.Close()
+ }()
+ client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn))
+ var ret bool
+
+ nc := metrics.NamedCollector{
+ Name: "histogram_registerHistogram",
+ Collector: metrics.Collector{
+ Help: "test_histogram",
+ Type: metrics.Histogram,
+ Buckets: []float64{0.1, 0.2, 0.5},
+ },
+ }
+
+ err = client.Call("metrics.Declare", nc, &ret)
+ assert.NoError(t, err)
+ assert.True(t, ret)
+
+ ret = false
+
+ m := metrics.Metric{
+ Name: "histogram_registerHistogram",
+ Value: 10000,
+ Labels: nil,
+ }
+
+ err = client.Call("metrics.Add", m, &ret)
+ assert.Error(t, err)
+ assert.False(t, ret)
+
+}
+
func subVector(t *testing.T) {
conn, err := net.Dial(dialNetwork, dialAddr)
assert.NoError(t, err)