diff options
author | Valery Piashchynski <[email protected]> | 2020-11-16 15:36:52 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-11-16 15:36:52 +0300 |
commit | f592c6e410fd1f8075c8b75597eb8e5dc7ba9cf1 (patch) | |
tree | 98413f71b0fcc6f8bf763ecaaa735152522d16e9 | |
parent | d40ff179e43a02726bfa4298e523a16c79a88cea (diff) |
Metrics collector returns slice of values
-rw-r--r-- | interfaces/metrics/interface.go | 2 | ||||
-rw-r--r-- | plugins/metrics/plugin.go | 21 | ||||
-rw-r--r-- | plugins/metrics/tests/metrics_test.go | 4 | ||||
-rw-r--r-- | plugins/metrics/tests/plugin1.go | 69 |
4 files changed, 17 insertions, 79 deletions
diff --git a/interfaces/metrics/interface.go b/interfaces/metrics/interface.go index 8207fb51..505c3d7b 100644 --- a/interfaces/metrics/interface.go +++ b/interfaces/metrics/interface.go @@ -5,5 +5,5 @@ import ( ) type StatProvider interface { - MetricsCollector() prometheus.Collector + MetricsCollector() []prometheus.Collector } diff --git a/plugins/metrics/plugin.go b/plugins/metrics/plugin.go index 8ffd19d5..3fd42ee4 100644 --- a/plugins/metrics/plugin.go +++ b/plugins/metrics/plugin.go @@ -25,8 +25,8 @@ const ( ) type statsProvider struct { - collector prometheus.Collector - name string + collectors []prometheus.Collector + name string } // Plugin to manage application metrics using Prometheus. @@ -73,8 +73,8 @@ func (m *Plugin) Init(cfg config.Configurer, log log.Logger) error { // Register invocation will be later in the Serve method for k, v := range collectors { m.collectors.Store(k, statsProvider{ - collector: v, - name: k, + collectors: []prometheus.Collector{v}, + name: k, }) } return nil @@ -92,10 +92,13 @@ func (m *Plugin) Serve() chan error { // key - name // value - statsProvider struct c := value.(statsProvider) - if err := m.registry.Register(c.collector); err != nil { - errCh <- err - return false + for _, v := range c.collectors { + if err := m.registry.Register(v); err != nil { + errCh <- err + return false + } } + return true }) @@ -207,8 +210,8 @@ func (m *Plugin) Collects() []interface{} { // Collector returns application specific collector by name or nil if collector not found. func (m *Plugin) AddStatProvider(name endure.Named, stat metrics.StatProvider) error { m.collectors.Store(name.Name(), statsProvider{ - collector: stat.MetricsCollector(), - name: name.Name(), + collectors: stat.MetricsCollector(), + name: name.Name(), }) return nil } diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index 7528fc46..d3043fbb 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -158,9 +158,9 @@ func TestMetricsGaugeCollector(t *testing.T) { assert.NoError(t, err) assert.Contains(t, out, "my_gauge 100") - genericOut, err := get("http://localhost:2112/metrics") + out, err = get("http://localhost:2112/metrics") assert.NoError(t, err) - assert.Contains(t, genericOut, "go_gc_duration_seconds") + assert.Contains(t, out, "go_gc_duration_seconds") for { select { diff --git a/plugins/metrics/tests/plugin1.go b/plugins/metrics/tests/plugin1.go index 8f1ece52..fb2a2235 100644 --- a/plugins/metrics/tests/plugin1.go +++ b/plugins/metrics/tests/plugin1.go @@ -28,77 +28,12 @@ func (p1 *Plugin1) Name() string { return "metrics_test.plugin1" } -func (p1 *Plugin1) MetricsCollector() prometheus.Collector { +func (p1 *Plugin1) MetricsCollector() []prometheus.Collector { collector := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "my_gauge", Help: "My gauge value", }) collector.Set(100) - return collector -} - -// ////////////////////////////////////////////////////////////// -type Plugin3 struct { - config config.Configurer -} - -func (p *Plugin3) Init(cfg config.Configurer) error { - p.config = cfg - return nil -} - -func (p *Plugin3) Serve() chan error { - errCh := make(chan error, 1) - return errCh -} - -func (p *Plugin3) Stop() error { - return nil -} - -func (p *Plugin3) Name() string { - return "metrics_test.plugin3" -} - -func (p *Plugin3) MetricsCollector() prometheus.Collector { - var ( - cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "cpu_temperature_celsius", - Help: "Current temperature of the CPU.", - }) - ) - return cpuTemp -} - -type Plugin4 struct { - config config.Configurer -} - -func (p *Plugin4) Init(cfg config.Configurer) error { - p.config = cfg - return nil -} - -func (p *Plugin4) Serve() chan error { - errCh := make(chan error, 1) - return errCh -} - -func (p *Plugin4) Stop() error { - return nil -} - -func (p *Plugin4) Name() string { - return "metrics_test.plugin4" -} - -func (p *Plugin4) MetricsCollector() prometheus.Collector { - var ( - cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "cpu_temperature_celsius", - Help: "Current temperature of the CPU.", - }) - ) - return cpuTemp + return []prometheus.Collector{collector} } |