diff options
Diffstat (limited to 'service/metrics/service.go')
-rw-r--r-- | service/metrics/service.go | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/service/metrics/service.go b/service/metrics/service.go index 2823b1a4..c7cfa376 100644 --- a/service/metrics/service.go +++ b/service/metrics/service.go @@ -4,6 +4,7 @@ import ( "context" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/spiral/roadrunner/service/rpc" "net/http" "sync" ) @@ -13,14 +14,22 @@ const ID = "metrics" // Service to manage application metrics using Prometheus. type Service struct { - cfg *Config - mu sync.Mutex - http *http.Server + cfg *Config + mu sync.Mutex + http *http.Server + collectors map[string]prometheus.Collector } // Init service. -func (s *Service) Init(cfg *Config) (bool, error) { +func (s *Service) Init(cfg *Config, r *rpc.Service) (bool, error) { s.cfg = cfg + + if r != nil { + if err := r.Register(ID, &rpcServer{s}); err != nil { + return false, err + } + } + return true, nil } @@ -42,9 +51,9 @@ func (s *Service) MustRegister(c prometheus.Collector) { } // Serve prometheus metrics service. -func (s *Service) Serve() error { +func (s *Service) Serve() (err error) { // register application specific metrics - if err := s.cfg.registerMetrics(); err != nil { + if s.collectors, err = s.cfg.initCollectors(); err != nil { return err } @@ -65,3 +74,13 @@ func (s *Service) Stop() { go s.http.Shutdown(context.Background()) } } + +// Collector returns application specific collector by name or nil if collector not found. +func (s *Service) Collector(name string) prometheus.Collector { + collector, ok := s.collectors[name] + if !ok { + return nil + } + + return collector +} |