further collector refactoring, add logging to metrics subpackage

This commit is contained in:
Bradley Cicenas 2017-02-23 02:24:26 +00:00
parent 70f86c4812
commit 7edde13a4d
4 changed files with 26 additions and 10 deletions

View File

@ -28,7 +28,6 @@ func (c *Container) SetState(s string) {
// Read metric stream, updating widgets
func (c *Container) Read(stream chan metrics.Metrics) {
log.Infof("starting reader for container: %s", c.id)
go func() {
for metrics := range stream {
c.metrics = metrics
@ -36,5 +35,7 @@ func (c *Container) Read(stream chan metrics.Metrics) {
c.widgets.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent)
c.widgets.SetNet(metrics.NetRx, metrics.NetTx)
}
log.Infof("reader stopped for container: %s", c.id)
}()
log.Infof("reader started for container: %s", c.id)
}

View File

@ -55,9 +55,14 @@ func (cm *ContainerMap) Refresh() {
}
}
if _, ok := cm.collectors[id]; ok == false {
cm.collectors[id] = metrics.NewDocker(cm.client, id)
}
}
var removeIDs []string
var collectIDs []string
for id, c := range cm.containers {
// mark stale internal containers
if _, ok := states[id]; ok == false {
@ -67,16 +72,19 @@ func (cm *ContainerMap) Refresh() {
c.SetState(states[id])
// start collector if needed
if c.state == "running" {
if _, ok := cm.collectors[id]; ok == false {
log.Infof("starting collector for container: %s", id)
cm.collectors[id] = metrics.NewDocker(cm.client, id)
cm.collectors[id].Start()
c.Read(cm.collectors[id].Stream())
}
collectIDs = append(collectIDs, id)
}
}
// remove dead containers
for _, id := range collectIDs {
if !cm.collectors[id].Running() {
cm.collectors[id].Start()
stream := cm.collectors[id].Stream()
cm.containers[id].Read(stream)
}
}
// delete removed containers
cm.Del(removeIDs...)
}
@ -103,6 +111,7 @@ func (cm *ContainerMap) Get(id string) *Container {
func (cm *ContainerMap) Del(ids ...string) {
for _, id := range ids {
delete(cm.containers, id)
delete(cm.collectors, id)
}
}

View File

@ -21,12 +21,12 @@ func NewDocker(client *api.Client, id string) *Docker {
Metrics: Metrics{},
id: id,
client: client,
stream: make(chan Metrics),
done: make(chan bool),
}
}
func (c *Docker) Start() {
c.done = make(chan bool)
c.stream = make(chan Metrics)
stats := make(chan *api.Stats)
go func() {
@ -48,9 +48,11 @@ func (c *Docker) Start() {
c.ReadNet(s)
c.stream <- c.Metrics
}
log.Infof("collector stopped for container: %s", c.id)
}()
c.running = true
log.Infof("collector started for container: %s", c.id)
}
func (c *Docker) Running() bool {

View File

@ -2,8 +2,12 @@ package metrics
import (
"math"
"github.com/bcicen/ctop/logging"
)
var log = logging.Init()
type Metrics struct {
CPUUtil int
NetTx int64