diff options
Diffstat (limited to 'service/metrics')
-rw-r--r-- | service/metrics/rpc_test.go | 5 | ||||
-rw-r--r-- | service/metrics/service.go | 17 |
2 files changed, 17 insertions, 5 deletions
diff --git a/service/metrics/rpc_test.go b/service/metrics/rpc_test.go index feae927a..2468c083 100644 --- a/service/metrics/rpc_test.go +++ b/service/metrics/rpc_test.go @@ -48,10 +48,13 @@ func setup(t *testing.T, metric string, portNum string) (*rpc2.Client, service.C t.Errorf("error during the Serve: error %v", err) } }() - time.Sleep(time.Millisecond * 100) + time.Sleep(time.Millisecond * 200) client, err := rs.Client() assert.NoError(t, err) + if err != nil { + panic(err) + } return client, c } diff --git a/service/metrics/service.go b/service/metrics/service.go index 9e2a1a71..6fa4da50 100644 --- a/service/metrics/service.go +++ b/service/metrics/service.go @@ -1,10 +1,13 @@ package metrics +// todo: declare metric at runtime + import ( "context" "fmt" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/sirupsen/logrus" "github.com/spiral/roadrunner/service/rpc" "net/http" "sync" @@ -16,6 +19,7 @@ const ID = "metrics" // Service to manage application metrics using Prometheus. type Service struct { cfg *Config + log *logrus.Logger mu sync.Mutex http *http.Server collectors sync.Map @@ -23,8 +27,9 @@ type Service struct { } // Init service. -func (s *Service) Init(cfg *Config, r *rpc.Service) (bool, error) { +func (s *Service) Init(cfg *Config, r *rpc.Service, log *logrus.Logger) (bool, error) { s.cfg = cfg + s.log = log s.registry = prometheus.NewRegistry() s.registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) @@ -77,7 +82,12 @@ func (s *Service) Serve() error { )} s.mu.Unlock() - return s.http.ListenAndServe() + err = s.http.ListenAndServe() + if err == nil || err == http.ErrServerClosed { + return nil + } + + return err } // Stop prometheus metrics service. @@ -90,9 +100,8 @@ func (s *Service) Stop() { 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)) + s.log.Error(fmt.Errorf("error shutting down the metrics server: error %v", err)) } }() } |