diff options
author | Wolfy-J <[email protected]> | 2019-06-27 13:42:38 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-06-27 13:42:38 +0300 |
commit | b97df692cc8f460bcb1639621d2eb27c3c29fdab (patch) | |
tree | 1ac7cf88adefb6963c7f9903344e91f427aee638 | |
parent | 1ba7781dd4bb9767759fa596d3d7209497aaf37f (diff) |
more tests
-rw-r--r-- | service/metrics/service.go | 13 | ||||
-rw-r--r-- | service/metrics/service_test.go | 24 |
2 files changed, 27 insertions, 10 deletions
diff --git a/service/metrics/service.go b/service/metrics/service.go index 0ae86c83..b3d0e2da 100644 --- a/service/metrics/service.go +++ b/service/metrics/service.go @@ -26,13 +26,8 @@ func (s *Service) Init(cfg *Config, r *rpc.Service) (bool, error) { s.cfg = cfg s.registry = prometheus.NewRegistry() - if err := s.registry.Register(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})); err != nil { - return false, err - } - - if err := s.registry.Register(prometheus.NewGoCollector()); err != nil { - return false, err - } + s.registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) + s.registry.MustRegister(prometheus.NewGoCollector()) if r != nil { if err := r.Register(ID, &rpcServer{s}); err != nil { @@ -55,9 +50,7 @@ func (s *Service) Register(c prometheus.Collector) error { // MustRegister registers new collector or fails with panic. func (s *Service) MustRegister(c prometheus.Collector) { - if err := s.registry.Register(c); err != nil { - panic(err) - } + s.registry.MustRegister(c) } // Serve prometheus metrics service. diff --git a/service/metrics/service_test.go b/service/metrics/service_test.go index 541bad0d..c7d08fa9 100644 --- a/service/metrics/service_test.go +++ b/service/metrics/service_test.go @@ -174,6 +174,30 @@ func Test_ConfiguredMetric(t *testing.T) { assert.Contains(t, out, "user_gauge 100") } +func Test_ConfiguredDuplicateMetric(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(ID, &Service{}) + + assert.NoError(t, c.Init(&testCfg{metricsCfg: `{ + "address": "localhost:2112", + "collect":{ + "go_gc_duration_seconds":{ + "type": "gauge" + } + } + }`})) + + s, _ := c.Get(ID) + assert.NotNil(t, s) + + assert.True(t, s.(*Service).Enabled()) + + assert.Error(t, c.Serve()) +} + func Test_ConfiguredInvalidMetric(t *testing.T) { logger, _ := test.NewNullLogger() logger.SetLevel(logrus.DebugLevel) |