Add basic stats

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2015-11-30 15:34:01 -08:00
parent c42c9aeb06
commit 3a30ea0c4f
3 changed files with 25 additions and 0 deletions

View file

@ -36,6 +36,11 @@ var DaemonCommand = cli.Command{
l := log.New(os.Stdout, "[containerd] ", log.LstdFlags) l := log.New(os.Stdout, "[containerd] ", log.LstdFlags)
goRoutineCounter := metrics.NewGauge() goRoutineCounter := metrics.NewGauge()
metrics.DefaultRegistry.Register("goroutines", goRoutineCounter) metrics.DefaultRegistry.Register("goroutines", goRoutineCounter)
for name, m := range containerd.Metrics() {
if err := metrics.DefaultRegistry.Register(name, m); err != nil {
logrus.Fatal(err)
}
}
go func() { go func() {
for range time.Tick(30 * time.Second) { for range time.Tick(30 * time.Second) {
goRoutineCounter.Update(int64(runtime.NumGoroutine())) goRoutineCounter.Update(int64(runtime.NumGoroutine()))

15
stats.go Normal file
View file

@ -0,0 +1,15 @@
package containerd
import "github.com/rcrowley/go-metrics"
var (
ContainerStartTimer = metrics.NewTimer()
ContainersCounter = metrics.NewCounter()
)
func Metrics() map[string]interface{} {
return map[string]interface{}{
"container-start-time": ContainerStartTimer,
"containers": ContainersCounter,
}
}

View file

@ -5,6 +5,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"sync" "sync"
"time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer"
@ -107,6 +108,7 @@ func (s *Supervisor) Start(events chan *Event) error {
continue continue
} }
s.containers[e.ID] = container s.containers[e.ID] = container
ContainersCounter.Inc(1)
s.tasks <- &startTask{ s.tasks <- &startTask{
err: e.Err, err: e.Err,
container: container, container: container,
@ -117,6 +119,7 @@ func (s *Supervisor) Start(events chan *Event) error {
if err := s.deleteContainer(container); err != nil { if err := s.deleteContainer(container); err != nil {
logrus.WithField("error", err).Error("containerd: deleting container") logrus.WithField("error", err).Error("containerd: deleting container")
} }
ContainersCounter.Dec(1)
} }
case GetContainerEventType: case GetContainerEventType:
for _, c := range s.containers { for _, c := range s.containers {
@ -222,6 +225,7 @@ type startTask struct {
func (s *Supervisor) startContainerWorker(tasks chan *startTask) { func (s *Supervisor) startContainerWorker(tasks chan *startTask) {
defer s.workerGroup.Done() defer s.workerGroup.Done()
for t := range tasks { for t := range tasks {
started := time.Now()
if err := t.container.Start(); err != nil { if err := t.container.Start(); err != nil {
e := NewEvent(StartContainerEventType) e := NewEvent(StartContainerEventType)
e.ID = t.container.ID() e.ID = t.container.ID()
@ -229,6 +233,7 @@ func (s *Supervisor) startContainerWorker(tasks chan *startTask) {
t.err <- err t.err <- err
continue continue
} }
ContainerStartTimer.UpdateSince(started)
t.err <- nil t.err <- nil
} }
} }