summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-06-27 13:35:16 +0300
committerWolfy-J <[email protected]>2019-06-27 13:35:16 +0300
commit1ba7781dd4bb9767759fa596d3d7209497aaf37f (patch)
treefc047874eb51d6014120eb695e932ef47436e485
parent11870a2df3ccd8f79f131354ba106e557c248b48 (diff)
more tests, isolated prometheus instance
-rw-r--r--service/metrics/config.go4
-rw-r--r--service/metrics/rpc_test.go102
-rw-r--r--service/metrics/service.go23
-rw-r--r--service/metrics/service_test.go1
4 files changed, 116 insertions, 14 deletions
diff --git a/service/metrics/config.go b/service/metrics/config.go
index b9b21ea9..343e31d1 100644
--- a/service/metrics/config.go
+++ b/service/metrics/config.go
@@ -108,10 +108,6 @@ func (c *Config) initCollectors() (map[string]prometheus.Collector, error) {
return nil, fmt.Errorf("invalid metric type `%s` for `%s`", m.Type, name)
}
- if err := prometheus.Register(collector); err != nil {
- return nil, err
- }
-
collectors[name] = collector
}
diff --git a/service/metrics/rpc_test.go b/service/metrics/rpc_test.go
index 78bf3e20..0eed52fe 100644
--- a/service/metrics/rpc_test.go
+++ b/service/metrics/rpc_test.go
@@ -10,7 +10,7 @@ import (
"time"
)
-func Test_Set_RPC_Metric(t *testing.T) {
+func Test_Set_RPC(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
@@ -23,7 +23,54 @@ func Test_Set_RPC_Metric(t *testing.T) {
metricsCfg: `{
"address": "localhost:2112",
"collect":{
- "user_gauge_2":{
+ "user_gauge":{
+ "type": "gauge"
+ }
+ }
+
+ }`}))
+
+ s, _ := c.Get(ID)
+ assert.NotNil(t, s)
+
+ s2, _ := c.Get(rpc.ID)
+ rs := s2.(*rpc.Service)
+
+ assert.True(t, s.(*Service).Enabled())
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ client, err := rs.Client()
+ assert.NoError(t, err)
+
+ var ok bool
+ assert.NoError(t, client.Call("metrics.Set", Metric{
+ Name: "user_gauge",
+ Value: 100.0,
+ }, &ok))
+ assert.True(t, ok)
+
+ out, _, err := get("http://localhost:2112/metrics")
+ assert.NoError(t, err)
+ assert.Contains(t, out, `user_gauge 100`)
+}
+
+func Test_Set_RPC_Vector(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(rpc.ID, &rpc.Service{})
+ c.Register(ID, &Service{})
+
+ assert.NoError(t, c.Init(&testCfg{
+ rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`,
+ metricsCfg: `{
+ "address": "localhost:2112",
+ "collect":{
+ "user_gauge":{
"type": "gauge",
"labels": ["type", "section"]
}
@@ -48,7 +95,7 @@ func Test_Set_RPC_Metric(t *testing.T) {
var ok bool
assert.NoError(t, client.Call("metrics.Set", Metric{
- Name: "user_gauge_2",
+ Name: "user_gauge",
Value: 100.0,
Labels: []string{"core", "first"},
}, &ok))
@@ -56,7 +103,50 @@ func Test_Set_RPC_Metric(t *testing.T) {
out, _, err := get("http://localhost:2112/metrics")
assert.NoError(t, err)
- assert.Contains(t, out, `user_gauge_2{section="first",type="core"} 100`)
+ assert.Contains(t, out, `user_gauge{section="first",type="core"} 100`)
+}
+
+func Test_Set_RPC_CollectorError(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(rpc.ID, &rpc.Service{})
+ c.Register(ID, &Service{})
+
+ assert.NoError(t, c.Init(&testCfg{
+ rpcCfg: `{"enable":true, "listen":"tcp://:5004"}`,
+ metricsCfg: `{
+ "address": "localhost:2112",
+ "collect":{
+ "user_gauge":{
+ "type": "gauge",
+ "labels": ["type", "section"]
+ }
+ }
+ }`}))
+
+ s, _ := c.Get(ID)
+ assert.NotNil(t, s)
+
+ s2, _ := c.Get(rpc.ID)
+ rs := s2.(*rpc.Service)
+
+ assert.True(t, s.(*Service).Enabled())
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ client, err := rs.Client()
+ assert.NoError(t, err)
+
+ var ok bool
+ assert.Error(t, client.Call("metrics.Set", Metric{
+ Name: "user_gauge_2",
+ Value: 100.0,
+ Labels: []string{"missing"},
+ }, &ok))
}
func Test_Set_RPC_MetricError(t *testing.T) {
@@ -72,7 +162,7 @@ func Test_Set_RPC_MetricError(t *testing.T) {
metricsCfg: `{
"address": "localhost:2112",
"collect":{
- "user_gauge_3":{
+ "user_gauge":{
"type": "gauge",
"labels": ["type", "section"]
}
@@ -97,7 +187,7 @@ func Test_Set_RPC_MetricError(t *testing.T) {
var ok bool
assert.Error(t, client.Call("metrics.Set", Metric{
- Name: "user_gauge_3",
+ Name: "user_gauge",
Value: 100.0,
Labels: []string{"missing"},
}, &ok))
diff --git a/service/metrics/service.go b/service/metrics/service.go
index 2c94568d..0ae86c83 100644
--- a/service/metrics/service.go
+++ b/service/metrics/service.go
@@ -18,11 +18,21 @@ type Service struct {
mu sync.Mutex
http *http.Server
collectors sync.Map
+ registry *prometheus.Registry
}
// Init service.
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
+ }
if r != nil {
if err := r.Register(ID, &rpcServer{s}); err != nil {
@@ -40,12 +50,12 @@ func (s *Service) Enabled() bool {
// Register new prometheus collector.
func (s *Service) Register(c prometheus.Collector) error {
- return prometheus.Register(c)
+ return s.registry.Register(c)
}
// MustRegister registers new collector or fails with panic.
func (s *Service) MustRegister(c prometheus.Collector) {
- if err := prometheus.Register(c); err != nil {
+ if err := s.registry.Register(c); err != nil {
panic(err)
}
}
@@ -59,11 +69,18 @@ func (s *Service) Serve() error {
}
for name, collector := range collectors {
+ if err := s.registry.Register(collector); err != nil {
+ return err
+ }
+
s.collectors.Store(name, collector)
}
s.mu.Lock()
- s.http = &http.Server{Addr: s.cfg.Address, Handler: promhttp.Handler()}
+ s.http = &http.Server{Addr: s.cfg.Address, Handler: promhttp.HandlerFor(
+ s.registry,
+ promhttp.HandlerOpts{},
+ )}
s.mu.Unlock()
return s.http.ListenAndServe()
diff --git a/service/metrics/service_test.go b/service/metrics/service_test.go
index 892c9e30..541bad0d 100644
--- a/service/metrics/service_test.go
+++ b/service/metrics/service_test.go
@@ -153,7 +153,6 @@ func Test_ConfiguredMetric(t *testing.T) {
"type": "gauge"
}
}
-
}`}))
s, _ := c.Get(ID)