summaryrefslogtreecommitdiff
path: root/service/metrics/service.go
diff options
context:
space:
mode:
Diffstat (limited to 'service/metrics/service.go')
-rw-r--r--service/metrics/service.go15
1 files changed, 10 insertions, 5 deletions
diff --git a/service/metrics/service.go b/service/metrics/service.go
index c7cfa376..2c94568d 100644
--- a/service/metrics/service.go
+++ b/service/metrics/service.go
@@ -17,7 +17,7 @@ type Service struct {
cfg *Config
mu sync.Mutex
http *http.Server
- collectors map[string]prometheus.Collector
+ collectors sync.Map
}
// Init service.
@@ -51,12 +51,17 @@ func (s *Service) MustRegister(c prometheus.Collector) {
}
// Serve prometheus metrics service.
-func (s *Service) Serve() (err error) {
+func (s *Service) Serve() error {
// register application specific metrics
- if s.collectors, err = s.cfg.initCollectors(); err != nil {
+ collectors, err := s.cfg.initCollectors()
+ if err != nil {
return err
}
+ for name, collector := range collectors {
+ s.collectors.Store(name, collector)
+ }
+
s.mu.Lock()
s.http = &http.Server{Addr: s.cfg.Address, Handler: promhttp.Handler()}
s.mu.Unlock()
@@ -77,10 +82,10 @@ func (s *Service) Stop() {
// 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]
+ collector, ok := s.collectors.Load(name)
if !ok {
return nil
}
- return collector
+ return collector.(prometheus.Collector)
}