diff --git a/containerd/daemon.go b/containerd/daemon.go index 3519b25..49f17b3 100644 --- a/containerd/daemon.go +++ b/containerd/daemon.go @@ -36,6 +36,11 @@ var DaemonCommand = cli.Command{ l := log.New(os.Stdout, "[containerd] ", log.LstdFlags) goRoutineCounter := metrics.NewGauge() 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() { for range time.Tick(30 * time.Second) { goRoutineCounter.Update(int64(runtime.NumGoroutine())) diff --git a/stats.go b/stats.go new file mode 100644 index 0000000..249fab2 --- /dev/null +++ b/stats.go @@ -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, + } +} diff --git a/supervisor.go b/supervisor.go index c1f2541..fa0763d 100644 --- a/supervisor.go +++ b/supervisor.go @@ -5,6 +5,7 @@ import ( "path/filepath" "runtime" "sync" + "time" "github.com/Sirupsen/logrus" "github.com/opencontainers/runc/libcontainer" @@ -107,6 +108,7 @@ func (s *Supervisor) Start(events chan *Event) error { continue } s.containers[e.ID] = container + ContainersCounter.Inc(1) s.tasks <- &startTask{ err: e.Err, container: container, @@ -117,6 +119,7 @@ func (s *Supervisor) Start(events chan *Event) error { if err := s.deleteContainer(container); err != nil { logrus.WithField("error", err).Error("containerd: deleting container") } + ContainersCounter.Dec(1) } case GetContainerEventType: for _, c := range s.containers { @@ -222,6 +225,7 @@ type startTask struct { func (s *Supervisor) startContainerWorker(tasks chan *startTask) { defer s.workerGroup.Done() for t := range tasks { + started := time.Now() if err := t.container.Start(); err != nil { e := NewEvent(StartContainerEventType) e.ID = t.container.ID() @@ -229,6 +233,7 @@ func (s *Supervisor) startContainerWorker(tasks chan *startTask) { t.err <- err continue } + ContainerStartTimer.UpdateSince(started) t.err <- nil } }