summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-06-27 12:52:08 +0300
committerWolfy-J <[email protected]>2019-06-27 12:52:08 +0300
commit5c026eb34ef2b5caad5f5e13cfd88b9c05362e92 (patch)
tree65bb569605a7e4dd48f3d7e6e821453b5143f47a
parente2ea9ee27aacd643918fe721735ba57798fb3da2 (diff)
more tests
-rw-r--r--service/metrics/service.go15
-rw-r--r--service/metrics/service_test.go165
2 files changed, 175 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)
}
diff --git a/service/metrics/service_test.go b/service/metrics/service_test.go
index 1abe097a..592bac9c 100644
--- a/service/metrics/service_test.go
+++ b/service/metrics/service_test.go
@@ -1 +1,166 @@
package metrics
+
+import (
+ "encoding/json"
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/sirupsen/logrus"
+ "github.com/sirupsen/logrus/hooks/test"
+ "github.com/spiral/roadrunner/service"
+ "github.com/stretchr/testify/assert"
+ "io/ioutil"
+ "net/http"
+ "testing"
+ "time"
+)
+
+type testCfg struct {
+ metricsCfg string
+ target string
+}
+
+func (cfg *testCfg) Get(name string) service.Config {
+ if name == ID {
+ return &testCfg{target: cfg.metricsCfg}
+ }
+
+ return nil
+}
+
+func (cfg *testCfg) Unmarshal(out interface{}) error {
+ err := json.Unmarshal([]byte(cfg.target), out)
+ return err
+}
+
+// get request and return body
+func get(url string) (string, *http.Response, error) {
+ r, err := http.Get(url)
+ if err != nil {
+ return "", nil, err
+ }
+ defer r.Body.Close()
+
+ b, err := ioutil.ReadAll(r.Body)
+ return string(b), r, err
+}
+
+func TestService_Serve(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"
+ }`}))
+
+ s, _ := c.Get(ID)
+ assert.NotNil(t, s)
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ out, _, err := get("http://localhost:2112/metrics")
+ assert.NoError(t, err)
+
+ assert.Contains(t, out, "go_gc_duration_seconds")
+}
+
+func Test_ServiceCustomMetric(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"
+ }`}))
+
+ s, _ := c.Get(ID)
+ assert.NotNil(t, s)
+
+ collector := prometheus.NewGauge(prometheus.GaugeOpts{
+ Name: "my_gauge",
+ Help: "My gauge value",
+ })
+
+ assert.NoError(t, s.(*Service).Register(collector))
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ collector.Set(100)
+
+ out, _, err := get("http://localhost:2112/metrics")
+ assert.NoError(t, err)
+
+ assert.Contains(t, out, "my_gauge 100")
+}
+
+func Test_ServiceCustomMetricMust(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"
+ }`}))
+
+ s, _ := c.Get(ID)
+ assert.NotNil(t, s)
+
+ collector := prometheus.NewGauge(prometheus.GaugeOpts{
+ Name: "my_gauge_2",
+ Help: "My gauge value",
+ })
+
+ s.(*Service).MustRegister(collector)
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ collector.Set(100)
+
+ out, _, err := get("http://localhost:2112/metrics")
+ assert.NoError(t, err)
+
+ assert.Contains(t, out, "my_gauge_2 100")
+}
+
+func Test_ConfiguredMetric(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":{
+ "user_gauge":{
+ "type": "gauge"
+ }
+ }
+
+ }`}))
+
+ s, _ := c.Get(ID)
+ assert.NotNil(t, s)
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ s.(*Service).Collector("user_gauge").(prometheus.Gauge).Set(100)
+
+ out, _, err := get("http://localhost:2112/metrics")
+ assert.NoError(t, err)
+
+ assert.Contains(t, out, "user_gauge 100")
+}