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.go18
1 files changed, 16 insertions, 2 deletions
diff --git a/service/metrics/service.go b/service/metrics/service.go
index 9d2a3dad..2823b1a4 100644
--- a/service/metrics/service.go
+++ b/service/metrics/service.go
@@ -5,6 +5,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
+ "sync"
)
// ID declares public service name.
@@ -13,6 +14,7 @@ const ID = "metrics"
// Service to manage application metrics using Prometheus.
type Service struct {
cfg *Config
+ mu sync.Mutex
http *http.Server
}
@@ -41,13 +43,25 @@ func (s *Service) MustRegister(c prometheus.Collector) {
// Serve prometheus metrics service.
func (s *Service) Serve() error {
+ // register application specific metrics
+ if err := s.cfg.registerMetrics(); err != nil {
+ return err
+ }
+
+ s.mu.Lock()
s.http = &http.Server{Addr: s.cfg.Address, Handler: promhttp.Handler()}
+ s.mu.Unlock()
return s.http.ListenAndServe()
}
// Stop prometheus metrics service.
func (s *Service) Stop() {
- // gracefully stop server
- go s.http.Shutdown(context.Background())
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ if s.http != nil {
+ // gracefully stop server
+ go s.http.Shutdown(context.Background())
+ }
}