diff options
author | Wolfy-J <[email protected]> | 2019-06-27 13:56:58 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-06-27 13:56:58 +0300 |
commit | f9a775a009f1c91666d697c2d69cf602aaffb51c (patch) | |
tree | e2b2ed85a4fa51bd327056d05e694551b8780423 | |
parent | b97df692cc8f460bcb1639621d2eb27c3c29fdab (diff) |
more tests
-rw-r--r-- | service/metrics/config.go | 2 | ||||
-rw-r--r-- | service/metrics/config_test.go | 45 | ||||
-rw-r--r-- | service/metrics/service.go | 2 |
3 files changed, 47 insertions, 2 deletions
diff --git a/service/metrics/config.go b/service/metrics/config.go index 343e31d1..b17fc0c9 100644 --- a/service/metrics/config.go +++ b/service/metrics/config.go @@ -41,7 +41,7 @@ func (c *Config) Hydrate(cfg service.Config) error { } // register application specific metrics. -func (c *Config) initCollectors() (map[string]prometheus.Collector, error) { +func (c *Config) getCollectors() (map[string]prometheus.Collector, error) { if c.Collect == nil { return nil, nil } diff --git a/service/metrics/config_test.go b/service/metrics/config_test.go index bd02d1cf..ad60102b 100644 --- a/service/metrics/config_test.go +++ b/service/metrics/config_test.go @@ -2,6 +2,7 @@ package metrics import ( "encoding/json" + "github.com/prometheus/client_golang/prometheus" "github.com/spiral/roadrunner/service" "github.com/stretchr/testify/assert" "testing" @@ -25,3 +26,47 @@ func Test_Config_Hydrate_Error2(t *testing.T) { assert.Error(t, c.Hydrate(cfg)) } + +func Test_Config_Metrics(t *testing.T) { + cfg := &mockCfg{`{ +"collect":{ + "metric1":{"type": "gauge"}, + "metric2":{ "type": "counter"}, + "metric3":{"type": "summary"}, + "metric4":{"type": "histogram"} +} +}`} + c := &Config{} + + assert.NoError(t, c.Hydrate(cfg)) + + m, err := c.getCollectors() + assert.NoError(t, err) + + assert.IsType(t, prometheus.NewGauge(prometheus.GaugeOpts{}), m["metric1"]) + assert.IsType(t, prometheus.NewCounter(prometheus.CounterOpts{}), m["metric2"]) + assert.IsType(t, prometheus.NewSummary(prometheus.SummaryOpts{}), m["metric3"]) + assert.IsType(t, prometheus.NewHistogram(prometheus.HistogramOpts{}), m["metric4"]) +} + +func Test_Config_MetricsVector(t *testing.T) { + cfg := &mockCfg{`{ +"collect":{ + "metric1":{"type": "gauge","labels":["label"]}, + "metric2":{ "type": "counter","labels":["label"]}, + "metric3":{"type": "summary","labels":["label"]}, + "metric4":{"type": "histogram","labels":["label"]} +} +}`} + c := &Config{} + + assert.NoError(t, c.Hydrate(cfg)) + + m, err := c.getCollectors() + assert.NoError(t, err) + + assert.IsType(t, prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{}), m["metric1"]) + assert.IsType(t, prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{}), m["metric2"]) + assert.IsType(t, prometheus.NewSummaryVec(prometheus.SummaryOpts{}, []string{}), m["metric3"]) + assert.IsType(t, prometheus.NewHistogramVec(prometheus.HistogramOpts{}, []string{}), m["metric4"]) +} diff --git a/service/metrics/service.go b/service/metrics/service.go index b3d0e2da..4916b3e0 100644 --- a/service/metrics/service.go +++ b/service/metrics/service.go @@ -56,7 +56,7 @@ func (s *Service) MustRegister(c prometheus.Collector) { // Serve prometheus metrics service. func (s *Service) Serve() error { // register application specific metrics - collectors, err := s.cfg.initCollectors() + collectors, err := s.cfg.getCollectors() if err != nil { return err } |