diff options
Diffstat (limited to 'service/metrics')
-rw-r--r-- | service/metrics/rpc.go | 30 | ||||
-rw-r--r-- | service/metrics/rpc_test.go | 7 | ||||
-rw-r--r-- | service/metrics/service.go | 10 | ||||
-rw-r--r-- | service/metrics/service_test.go | 37 |
4 files changed, 62 insertions, 22 deletions
diff --git a/service/metrics/rpc.go b/service/metrics/rpc.go index ee8ef984..9e7de640 100644 --- a/service/metrics/rpc.go +++ b/service/metrics/rpc.go @@ -32,26 +32,26 @@ func (rpc *rpcServer) Add(m *Metric, ok *bool) (err error) { return fmt.Errorf("undefined collector `%s`", m.Name) } - switch c.(type) { + switch c := c.(type) { case prometheus.Gauge: - c.(prometheus.Gauge).Add(m.Value) + c.Add(m.Value) case *prometheus.GaugeVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.GaugeVec).WithLabelValues(m.Labels...).Add(m.Value) + c.WithLabelValues(m.Labels...).Add(m.Value) case prometheus.Counter: - c.(prometheus.Counter).Add(m.Value) + c.Add(m.Value) case *prometheus.CounterVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.CounterVec).WithLabelValues(m.Labels...).Add(m.Value) + c.WithLabelValues(m.Labels...).Add(m.Value) default: return fmt.Errorf("collector `%s` does not support method `Add`", m.Name) @@ -74,16 +74,16 @@ func (rpc *rpcServer) Sub(m *Metric, ok *bool) (err error) { return fmt.Errorf("undefined collector `%s`", m.Name) } - switch c.(type) { + switch c := c.(type) { case prometheus.Gauge: - c.(prometheus.Gauge).Sub(m.Value) + c.Sub(m.Value) case *prometheus.GaugeVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.GaugeVec).WithLabelValues(m.Labels...).Sub(m.Value) + c.WithLabelValues(m.Labels...).Sub(m.Value) default: return fmt.Errorf("collector `%s` does not support method `Sub`", m.Name) } @@ -105,23 +105,23 @@ func (rpc *rpcServer) Observe(m *Metric, ok *bool) (err error) { return fmt.Errorf("undefined collector `%s`", m.Name) } - switch c.(type) { + switch c := c.(type) { case *prometheus.SummaryVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.SummaryVec).WithLabelValues(m.Labels...).Observe(m.Value) + c.WithLabelValues(m.Labels...).Observe(m.Value) case prometheus.Histogram: - c.(prometheus.Histogram).Observe(m.Value) + c.Observe(m.Value) case *prometheus.HistogramVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.HistogramVec).WithLabelValues(m.Labels...).Observe(m.Value) + c.WithLabelValues(m.Labels...).Observe(m.Value) default: return fmt.Errorf("collector `%s` does not support method `Observe`", m.Name) } @@ -143,16 +143,16 @@ func (rpc *rpcServer) Set(m *Metric, ok *bool) (err error) { return fmt.Errorf("undefined collector `%s`", m.Name) } - switch c.(type) { + switch c := c.(type) { case prometheus.Gauge: - c.(prometheus.Gauge).Set(m.Value) + c.Set(m.Value) case *prometheus.GaugeVec: if len(m.Labels) == 0 { return fmt.Errorf("required labels for collector `%s`", m.Name) } - c.(*prometheus.GaugeVec).WithLabelValues(m.Labels...).Set(m.Value) + c.WithLabelValues(m.Labels...).Set(m.Value) default: return fmt.Errorf("collector `%s` does not support method `Set`", m.Name) diff --git a/service/metrics/rpc_test.go b/service/metrics/rpc_test.go index 6d061f1d..feae927a 100644 --- a/service/metrics/rpc_test.go +++ b/service/metrics/rpc_test.go @@ -42,7 +42,12 @@ func setup(t *testing.T, metric string, portNum string) (*rpc2.Client, service.C assert.True(t, s.(*Service).Enabled()) - go func() { c.Serve() }() + go func() { + err := c.Serve() + if err != nil { + t.Errorf("error during the Serve: error %v", err) + } + }() time.Sleep(time.Millisecond * 100) client, err := rs.Client() diff --git a/service/metrics/service.go b/service/metrics/service.go index 4916b3e0..9e2a1a71 100644 --- a/service/metrics/service.go +++ b/service/metrics/service.go @@ -2,6 +2,7 @@ package metrics import ( "context" + "fmt" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/spiral/roadrunner/service/rpc" @@ -86,7 +87,14 @@ func (s *Service) Stop() { if s.http != nil { // gracefully stop server - go s.http.Shutdown(context.Background()) + go func() { + err := s.http.Shutdown(context.Background()) + if err != nil { + // TODO how to show error message? + // Function should be Stop() error + fmt.Println(fmt.Errorf("error shutting down the server: error %v", err)) + } + }() } } diff --git a/service/metrics/service_test.go b/service/metrics/service_test.go index 0cf6fd95..62e6f6d7 100644 --- a/service/metrics/service_test.go +++ b/service/metrics/service_test.go @@ -43,9 +43,16 @@ func get(url string) (string, *http.Response, error) { if err != nil { return "", nil, err } - defer r.Body.Close() b, err := ioutil.ReadAll(r.Body) + if err != nil { + return "", nil, err + } + + err = r.Body.Close() + if err != nil { + return "", nil, err + } return string(b), r, err } @@ -63,7 +70,12 @@ func TestService_Serve(t *testing.T) { s, _ := c.Get(ID) assert.NotNil(t, s) - go func() { c.Serve() }() + 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() @@ -94,7 +106,12 @@ func Test_ServiceCustomMetric(t *testing.T) { assert.NoError(t, s.(*Service).Register(collector)) - go func() { c.Serve() }() + 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() @@ -127,7 +144,12 @@ func Test_ServiceCustomMetricMust(t *testing.T) { s.(*Service).MustRegister(collector) - go func() { c.Serve() }() + 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() @@ -160,7 +182,12 @@ func Test_ConfiguredMetric(t *testing.T) { assert.True(t, s.(*Service).Enabled()) - go func() { c.Serve() }() + 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() |