Skip to content

Commit

Permalink
Collectors export metrics from Collect().
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarmol committed May 11, 2015
1 parent 1e55ccf commit 4fdd709
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
10 changes: 7 additions & 3 deletions collector/collector_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"strings"
"time"

"github.com/google/cadvisor/info/v2"
)

type collectorManager struct {
Expand Down Expand Up @@ -46,17 +48,19 @@ func (cm *collectorManager) RegisterCollector(collector Collector) error {
return nil
}

func (cm *collectorManager) Collect() (time.Time, error) {
func (cm *collectorManager) Collect() (time.Time, []v2.Metric, error) {
var errors []error

// Collect from all collectors that are ready.
var next time.Time
var metrics []v2.Metric
for _, c := range cm.collectors {
if c.nextCollectionTime.Before(time.Now()) {
nextCollection, err := c.collector.Collect()
nextCollection, newMetrics, err := c.collector.Collect()
if err != nil {
errors = append(errors, err)
}
metrics = append(metrics, newMetrics...)
c.nextCollectionTime = nextCollection
}

Expand All @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions collector/collector_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"
"time"

"github.com/google/cadvisor/info/v2"
"github.com/stretchr/testify/assert"
)

Expand All @@ -27,9 +28,9 @@ type fakeCollector struct {
collectedFrom int
}

func (fc *fakeCollector) Collect() (time.Time, error) {
func (fc *fakeCollector) Collect() (time.Time, []v2.Metric, error) {
fc.collectedFrom++
return fc.nextCollectionTime, fc.err
return fc.nextCollectionTime, []v2.Metric{}, fc.err
}

func (fc *fakeCollector) Name() string {
Expand All @@ -53,7 +54,7 @@ func TestCollect(t *testing.T) {
assert.NoError(cm.RegisterCollector(f2))

// First collection, everyone gets collected from.
nextTime, err := cm.Collect()
nextTime, _, err := cm.Collect()
assert.Equal(firstTime, nextTime)
assert.NoError(err)
assert.Equal(1, f1.collectedFrom)
Expand All @@ -62,7 +63,7 @@ func TestCollect(t *testing.T) {
f1.nextCollectionTime = time.Now().Add(2 * time.Hour)

// Second collection, only the one that is ready gets collected from.
nextTime, err = cm.Collect()
nextTime, _, err = cm.Collect()
assert.Equal(secondTime, nextTime)
assert.NoError(err)
assert.Equal(2, f1.collectedFrom)
Expand Down
6 changes: 4 additions & 2 deletions collector/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package collector

import (
"time"

"github.com/google/cadvisor/info/v2"
)

type FakeCollectorManager struct {
Expand All @@ -25,7 +27,7 @@ func (fkm *FakeCollectorManager) RegisterCollector(collector Collector) error {
return nil
}

func (fkm *FakeCollectorManager) Collect() (time.Time, error) {
func (fkm *FakeCollectorManager) Collect() (time.Time, []v2.Metric, error) {
var zero time.Time
return zero, nil
return zero, []v2.Metric{}, nil
}
5 changes: 3 additions & 2 deletions collector/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package collector

import (
"github.com/google/cadvisor/info/v2"
"time"
)

Expand All @@ -26,7 +27,7 @@ type Collector interface {
// Returns the next time this collector should be collected from.
// Next collection time is always returned, even when an error occurs.
// A collection time of zero means no more collection.
Collect() (time.Time, error)
Collect() (time.Time, []v2.Metric, error)

// Name of this collector.
Name() string
Expand All @@ -41,5 +42,5 @@ type CollectorManager interface {
// at which a collector will be ready to collect from.
// Next collection time is always returned, even when an error occurs.
// A collection time of zero means no more collection.
Collect() (time.Time, error)
Collect() (time.Time, []v2.Metric, error)
}
3 changes: 2 additions & 1 deletion manager/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ func (c *containerData) housekeeping() {
}
}

// TODO(vmarmol): Export metrics.
// Run custom collectors.
nextCollectionTime, err := c.collectorManager.Collect()
nextCollectionTime, _, err := c.collectorManager.Collect()
if err != nil && c.allowErrorLogging() {
glog.Warningf("[%s] Collection failed: %v", c.info.Name, err)
}
Expand Down

0 comments on commit 4fdd709

Please sign in to comment.