Collectors export metrics from Collect().

This commit is contained in:
Victor Marmol 2015-05-07 15:35:40 -07:00
parent 1e55ccf30d
commit 4fdd709717
5 changed files with 21 additions and 12 deletions

View File

@ -18,6 +18,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"time" "time"
"github.com/google/cadvisor/info/v2"
) )
type collectorManager struct { type collectorManager struct {
@ -46,17 +48,19 @@ func (cm *collectorManager) RegisterCollector(collector Collector) error {
return nil return nil
} }
func (cm *collectorManager) Collect() (time.Time, error) { func (cm *collectorManager) Collect() (time.Time, []v2.Metric, error) {
var errors []error var errors []error
// Collect from all collectors that are ready. // Collect from all collectors that are ready.
var next time.Time var next time.Time
var metrics []v2.Metric
for _, c := range cm.collectors { for _, c := range cm.collectors {
if c.nextCollectionTime.Before(time.Now()) { if c.nextCollectionTime.Before(time.Now()) {
nextCollection, err := c.collector.Collect() nextCollection, newMetrics, err := c.collector.Collect()
if err != nil { if err != nil {
errors = append(errors, err) errors = append(errors, err)
} }
metrics = append(metrics, newMetrics...)
c.nextCollectionTime = nextCollection c.nextCollectionTime = nextCollection
} }
@ -66,7 +70,7 @@ func (cm *collectorManager) Collect() (time.Time, error) {
} }
} }
return next, compileErrors(errors) return next, metrics, compileErrors(errors)
} }
// Make an error slice into a single error. // Make an error slice into a single error.

View File

@ -18,6 +18,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/google/cadvisor/info/v2"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -27,9 +28,9 @@ type fakeCollector struct {
collectedFrom int collectedFrom int
} }
func (fc *fakeCollector) Collect() (time.Time, error) { func (fc *fakeCollector) Collect() (time.Time, []v2.Metric, error) {
fc.collectedFrom++ fc.collectedFrom++
return fc.nextCollectionTime, fc.err return fc.nextCollectionTime, []v2.Metric{}, fc.err
} }
func (fc *fakeCollector) Name() string { func (fc *fakeCollector) Name() string {
@ -53,7 +54,7 @@ func TestCollect(t *testing.T) {
assert.NoError(cm.RegisterCollector(f2)) assert.NoError(cm.RegisterCollector(f2))
// First collection, everyone gets collected from. // First collection, everyone gets collected from.
nextTime, err := cm.Collect() nextTime, _, err := cm.Collect()
assert.Equal(firstTime, nextTime) assert.Equal(firstTime, nextTime)
assert.NoError(err) assert.NoError(err)
assert.Equal(1, f1.collectedFrom) assert.Equal(1, f1.collectedFrom)
@ -62,7 +63,7 @@ func TestCollect(t *testing.T) {
f1.nextCollectionTime = time.Now().Add(2 * time.Hour) f1.nextCollectionTime = time.Now().Add(2 * time.Hour)
// Second collection, only the one that is ready gets collected from. // Second collection, only the one that is ready gets collected from.
nextTime, err = cm.Collect() nextTime, _, err = cm.Collect()
assert.Equal(secondTime, nextTime) assert.Equal(secondTime, nextTime)
assert.NoError(err) assert.NoError(err)
assert.Equal(2, f1.collectedFrom) assert.Equal(2, f1.collectedFrom)

View File

@ -16,6 +16,8 @@ package collector
import ( import (
"time" "time"
"github.com/google/cadvisor/info/v2"
) )
type FakeCollectorManager struct { type FakeCollectorManager struct {
@ -25,7 +27,7 @@ func (fkm *FakeCollectorManager) RegisterCollector(collector Collector) error {
return nil return nil
} }
func (fkm *FakeCollectorManager) Collect() (time.Time, error) { func (fkm *FakeCollectorManager) Collect() (time.Time, []v2.Metric, error) {
var zero time.Time var zero time.Time
return zero, nil return zero, []v2.Metric{}, nil
} }

View File

@ -15,6 +15,7 @@
package collector package collector
import ( import (
"github.com/google/cadvisor/info/v2"
"time" "time"
) )
@ -26,7 +27,7 @@ type Collector interface {
// Returns the next time this collector should be collected from. // Returns the next time this collector should be collected from.
// Next collection time is always returned, even when an error occurs. // Next collection time is always returned, even when an error occurs.
// A collection time of zero means no more collection. // A collection time of zero means no more collection.
Collect() (time.Time, error) Collect() (time.Time, []v2.Metric, error)
// Name of this collector. // Name of this collector.
Name() string Name() string
@ -41,5 +42,5 @@ type CollectorManager interface {
// at which a collector will be ready to collect from. // at which a collector will be ready to collect from.
// Next collection time is always returned, even when an error occurs. // Next collection time is always returned, even when an error occurs.
// A collection time of zero means no more collection. // A collection time of zero means no more collection.
Collect() (time.Time, error) Collect() (time.Time, []v2.Metric, error)
} }

View File

@ -232,8 +232,9 @@ func (c *containerData) housekeeping() {
} }
} }
// TODO(vmarmol): Export metrics.
// Run custom collectors. // Run custom collectors.
nextCollectionTime, err := c.collectorManager.Collect() nextCollectionTime, _, err := c.collectorManager.Collect()
if err != nil && c.allowErrorLogging() { if err != nil && c.allowErrorLogging() {
glog.Warningf("[%s] Collection failed: %v", c.info.Name, err) glog.Warningf("[%s] Collection failed: %v", c.info.Name, err)
} }