From c8459e1e5933f8bf5bc25635ce13724d492e5ebe Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Tue, 25 Jun 2019 17:38:29 +0300 Subject: prometheus --- service/metrics/config.go | 23 ++++++++++++++++++++ service/metrics/service.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 service/metrics/config.go create mode 100644 service/metrics/service.go (limited to 'service') diff --git a/service/metrics/config.go b/service/metrics/config.go new file mode 100644 index 00000000..799ba2d2 --- /dev/null +++ b/service/metrics/config.go @@ -0,0 +1,23 @@ +package metrics + +import "github.com/spiral/roadrunner/service" + +type Config struct { + // Address to listen + Address string + + // Metrics define application specific metrics. + Metrics map[string]Metric +} + +// Metric describes single application specific metric. +type Metric struct { + Type string + Description string + Labels []string +} + +// Hydrate configuration. +func (c *Config) Hydrate(cfg service.Config) error { + return cfg.Unmarshal(c) +} diff --git a/service/metrics/service.go b/service/metrics/service.go new file mode 100644 index 00000000..50b533f8 --- /dev/null +++ b/service/metrics/service.go @@ -0,0 +1,53 @@ +package metrics + +import ( + "context" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + "net/http" +) + +// ID declares public service name. +const ID = "metrics" + +// Service to manage application metrics using Prometheus. +type Service struct { + cfg *Config + http *http.Server +} + +// Init service. +func (s *Service) Init(cfg *Config) (bool, error) { + s.cfg = cfg + return true, nil +} + +// Enabled indicates that server is able to collect metrics. +func (s *Service) Enabled() bool { + return s.cfg != nil +} + +// Register new prometheus collector. +func (s *Service) Register(c prometheus.Collector) error { + return prometheus.Register(c) +} + +// MustRegister registers new collector or fails with panic. +func (s *Service) MustRegister(c prometheus.Collector) { + if err := prometheus.Register(c); err != nil { + panic(err) + } +} + +// Serve prometheus metrics service. +func (s *Service) Serve() error { + s.http = &http.Server{Addr: s.cfg.Address, Handler: promhttp.Handler()} + + return s.http.ListenAndServe() +} + +// Stop prometheus metrics service. +func (s *Service) Stop() { + // gracefully stop server + s.http.Shutdown(context.Background()) +} -- cgit v1.2.3