Run custom collectors in container housekeeping.
This will allow us to register and run custom collectors for each container.
This commit is contained in:
parent
5b39a77318
commit
bce54ce3f5
@ -24,6 +24,7 @@ import (
|
||||
|
||||
"github.com/docker/docker/pkg/units"
|
||||
"github.com/golang/glog"
|
||||
"github.com/google/cadvisor/collector"
|
||||
"github.com/google/cadvisor/container"
|
||||
info "github.com/google/cadvisor/info/v1"
|
||||
"github.com/google/cadvisor/info/v2"
|
||||
@ -63,6 +64,9 @@ type containerData struct {
|
||||
|
||||
// Tells the container to stop.
|
||||
stop chan bool
|
||||
|
||||
// Runs custom metric collectors.
|
||||
collectorManager collector.CollectorManager
|
||||
}
|
||||
|
||||
func (c *containerData) Start() error {
|
||||
@ -109,7 +113,7 @@ func (c *containerData) DerivedStats() (v2.DerivedStats, error) {
|
||||
return c.summaryReader.DerivedStats()
|
||||
}
|
||||
|
||||
func newContainerData(containerName string, memoryStorage *memory.InMemoryStorage, handler container.ContainerHandler, loadReader cpuload.CpuLoadReader, logUsage bool) (*containerData, error) {
|
||||
func newContainerData(containerName string, memoryStorage *memory.InMemoryStorage, handler container.ContainerHandler, loadReader cpuload.CpuLoadReader, logUsage bool, collectorManager collector.CollectorManager) (*containerData, error) {
|
||||
if memoryStorage == nil {
|
||||
return nil, fmt.Errorf("nil memory storage")
|
||||
}
|
||||
@ -129,6 +133,7 @@ func newContainerData(containerName string, memoryStorage *memory.InMemoryStorag
|
||||
logUsage: logUsage,
|
||||
loadAvg: -1.0, // negative value indicates uninitialized.
|
||||
stop: make(chan bool, 1),
|
||||
collectorManager: collectorManager,
|
||||
}
|
||||
cont.info.ContainerReference = ref
|
||||
|
||||
@ -172,6 +177,7 @@ func (self *containerData) nextHousekeeping(lastHousekeeping time.Time) time.Tim
|
||||
return lastHousekeeping.Add(self.housekeepingInterval)
|
||||
}
|
||||
|
||||
// TODO(vmarmol): Implement stats collecting as a custom collector.
|
||||
func (c *containerData) housekeeping() {
|
||||
// Long housekeeping is either 100ms or half of the housekeeping interval.
|
||||
longHousekeeping := 100 * time.Millisecond
|
||||
@ -226,12 +232,24 @@ func (c *containerData) housekeeping() {
|
||||
}
|
||||
}
|
||||
|
||||
// Schedule the next housekeeping. Sleep until that time.
|
||||
nextHousekeeping := c.nextHousekeeping(lastHousekeeping)
|
||||
if time.Now().Before(nextHousekeeping) {
|
||||
time.Sleep(nextHousekeeping.Sub(time.Now()))
|
||||
// Run custom collectors.
|
||||
nextCollectionTime, err := c.collectorManager.Collect()
|
||||
if err != nil && c.allowErrorLogging() {
|
||||
glog.Warningf("[%s] Collection failed: %v", c.info.Name, err)
|
||||
}
|
||||
lastHousekeeping = nextHousekeeping
|
||||
|
||||
// Next housekeeping is the first of the stats or the custom collector's housekeeping.
|
||||
nextHousekeeping := c.nextHousekeeping(lastHousekeeping)
|
||||
next := nextHousekeeping
|
||||
if !nextCollectionTime.IsZero() && nextCollectionTime.Before(nextHousekeeping) {
|
||||
next = nextCollectionTime
|
||||
}
|
||||
|
||||
// Schedule the next housekeeping. Sleep until that time.
|
||||
if time.Now().Before(next) {
|
||||
time.Sleep(next.Sub(time.Now()))
|
||||
}
|
||||
lastHousekeeping = next
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/cadvisor/collector"
|
||||
"github.com/google/cadvisor/container"
|
||||
info "github.com/google/cadvisor/info/v1"
|
||||
itest "github.com/google/cadvisor/info/v1/test"
|
||||
@ -40,7 +41,7 @@ func setupContainerData(t *testing.T, spec info.ContainerSpec) (*containerData,
|
||||
nil,
|
||||
)
|
||||
memoryStorage := memory.New(60, nil)
|
||||
ret, err := newContainerData(containerName, memoryStorage, mockHandler, nil, false)
|
||||
ret, err := newContainerData(containerName, memoryStorage, mockHandler, nil, false, &collector.FakeCollectorManager{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import (
|
||||
|
||||
"github.com/docker/libcontainer/cgroups"
|
||||
"github.com/golang/glog"
|
||||
"github.com/google/cadvisor/collector"
|
||||
"github.com/google/cadvisor/container"
|
||||
"github.com/google/cadvisor/container/docker"
|
||||
"github.com/google/cadvisor/container/raw"
|
||||
@ -650,8 +651,13 @@ func (m *manager) createContainer(containerName string) error {
|
||||
glog.V(4).Infof("ignoring container %q", containerName)
|
||||
return nil
|
||||
}
|
||||
// TODO(vmarmol): Register collectors.
|
||||
collectorManager, err := collector.NewCollectorManager()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logUsage := *logCadvisorUsage && containerName == m.cadvisorContainer
|
||||
cont, err := newContainerData(containerName, m.memoryStorage, handler, m.loadReader, logUsage)
|
||||
cont, err := newContainerData(containerName, m.memoryStorage, handler, m.loadReader, logUsage, collectorManager)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/cadvisor/collector"
|
||||
"github.com/google/cadvisor/container"
|
||||
"github.com/google/cadvisor/container/docker"
|
||||
info "github.com/google/cadvisor/info/v1"
|
||||
@ -52,7 +53,7 @@ func createManagerAndAddContainers(
|
||||
spec,
|
||||
nil,
|
||||
).Once()
|
||||
cont, err := newContainerData(name, memoryStorage, mockHandler, nil, false)
|
||||
cont, err := newContainerData(name, memoryStorage, mockHandler, nil, false, &collector.FakeCollectorManager{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user