mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
further collector refactoring, add logging to metrics subpackage
This commit is contained in:
parent
70f86c4812
commit
7edde13a4d
@ -28,7 +28,6 @@ func (c *Container) SetState(s string) {
|
|||||||
|
|
||||||
// Read metric stream, updating widgets
|
// Read metric stream, updating widgets
|
||||||
func (c *Container) Read(stream chan metrics.Metrics) {
|
func (c *Container) Read(stream chan metrics.Metrics) {
|
||||||
log.Infof("starting reader for container: %s", c.id)
|
|
||||||
go func() {
|
go func() {
|
||||||
for metrics := range stream {
|
for metrics := range stream {
|
||||||
c.metrics = metrics
|
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.SetMem(metrics.MemUsage, metrics.MemLimit, metrics.MemPercent)
|
||||||
c.widgets.SetNet(metrics.NetRx, metrics.NetTx)
|
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)
|
||||||
}
|
}
|
||||||
|
@ -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 removeIDs []string
|
||||||
|
var collectIDs []string
|
||||||
for id, c := range cm.containers {
|
for id, c := range cm.containers {
|
||||||
// mark stale internal containers
|
// mark stale internal containers
|
||||||
if _, ok := states[id]; ok == false {
|
if _, ok := states[id]; ok == false {
|
||||||
@ -67,16 +72,19 @@ func (cm *ContainerMap) Refresh() {
|
|||||||
c.SetState(states[id])
|
c.SetState(states[id])
|
||||||
// start collector if needed
|
// start collector if needed
|
||||||
if c.state == "running" {
|
if c.state == "running" {
|
||||||
if _, ok := cm.collectors[id]; ok == false {
|
collectIDs = append(collectIDs, id)
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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...)
|
cm.Del(removeIDs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,6 +111,7 @@ func (cm *ContainerMap) Get(id string) *Container {
|
|||||||
func (cm *ContainerMap) Del(ids ...string) {
|
func (cm *ContainerMap) Del(ids ...string) {
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
delete(cm.containers, id)
|
delete(cm.containers, id)
|
||||||
|
delete(cm.collectors, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,12 +21,12 @@ func NewDocker(client *api.Client, id string) *Docker {
|
|||||||
Metrics: Metrics{},
|
Metrics: Metrics{},
|
||||||
id: id,
|
id: id,
|
||||||
client: client,
|
client: client,
|
||||||
stream: make(chan Metrics),
|
|
||||||
done: make(chan bool),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Docker) Start() {
|
func (c *Docker) Start() {
|
||||||
|
c.done = make(chan bool)
|
||||||
|
c.stream = make(chan Metrics)
|
||||||
stats := make(chan *api.Stats)
|
stats := make(chan *api.Stats)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -48,9 +48,11 @@ func (c *Docker) Start() {
|
|||||||
c.ReadNet(s)
|
c.ReadNet(s)
|
||||||
c.stream <- c.Metrics
|
c.stream <- c.Metrics
|
||||||
}
|
}
|
||||||
|
log.Infof("collector stopped for container: %s", c.id)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
c.running = true
|
c.running = true
|
||||||
|
log.Infof("collector started for container: %s", c.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Docker) Running() bool {
|
func (c *Docker) Running() bool {
|
||||||
|
@ -2,8 +2,12 @@ package metrics
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
|
"github.com/bcicen/ctop/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = logging.Init()
|
||||||
|
|
||||||
type Metrics struct {
|
type Metrics struct {
|
||||||
CPUUtil int
|
CPUUtil int
|
||||||
NetTx int64
|
NetTx int64
|
||||||
|
Loading…
x
Reference in New Issue
Block a user