Merge pull request #1488 from crosbymichael/prom-containers

Add config for exporting container metrics to prom
This commit is contained in:
Kenfe-Mickaël Laventure 2017-09-07 13:21:12 -07:00 committed by GitHub
commit 17901fafa0
4 changed files with 47 additions and 34 deletions

View File

@ -3,6 +3,8 @@
package cgroups package cgroups
import ( import (
"strconv"
"github.com/containerd/cgroups" "github.com/containerd/cgroups"
metrics "github.com/docker/go-metrics" metrics "github.com/docker/go-metrics"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -101,3 +103,14 @@ var blkioMetrics = []*metric{
}, },
}, },
} }
func blkioValues(l []*cgroups.BlkIOEntry) []value {
var out []value
for _, e := range l {
out = append(out, value{
v: float64(e.Value),
l: []string{e.Op, e.Device, strconv.FormatUint(e.Major, 10), strconv.FormatUint(e.Minor, 10)},
})
}
return out
}

View File

@ -15,24 +15,33 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
) )
type Config struct {
NoPrometheus bool `toml:"no_prometheus"`
}
func init() { func init() {
plugin.Register(&plugin.Registration{ plugin.Register(&plugin.Registration{
Type: plugin.TaskMonitorPlugin, Type: plugin.TaskMonitorPlugin,
ID: "cgroups", ID: "cgroups",
Init: New, Init: New,
Config: &Config{},
}) })
} }
func New(ic *plugin.InitContext) (interface{}, error) { func New(ic *plugin.InitContext) (interface{}, error) {
var ( var ns *metrics.Namespace
ns = metrics.NewNamespace("container", "", nil) config := ic.Config.(*Config)
collector = NewCollector(ns) if !config.NoPrometheus {
) ns = metrics.NewNamespace("container", "", nil)
}
collector := NewCollector(ns)
oom, err := NewOOMCollector(ns) oom, err := NewOOMCollector(ns)
if err != nil { if err != nil {
return nil, err return nil, err
} }
metrics.Register(ns) if ns != nil {
metrics.Register(ns)
}
return &cgroupsMonitor{ return &cgroupsMonitor{
collector: collector, collector: collector,
oom: oom, oom: oom,

View File

@ -5,7 +5,6 @@ package cgroups
import ( import (
"errors" "errors"
"fmt" "fmt"
"strconv"
"sync" "sync"
"github.com/containerd/cgroups" "github.com/containerd/cgroups"
@ -26,6 +25,9 @@ type Trigger func(string, string, cgroups.Cgroup)
// New registers the Collector with the provided namespace and returns it so // New registers the Collector with the provided namespace and returns it so
// that cgroups can be added for collection // that cgroups can be added for collection
func NewCollector(ns *metrics.Namespace) *Collector { func NewCollector(ns *metrics.Namespace) *Collector {
if ns == nil {
return &Collector{}
}
// add machine cpus and memory info // add machine cpus and memory info
c := &Collector{ c := &Collector{
ns: ns, ns: ns,
@ -91,6 +93,9 @@ func (c *Collector) collect(id, namespace string, cg cgroups.Cgroup, ch chan<- p
// Add adds the provided cgroup and id so that metrics are collected and exported // Add adds the provided cgroup and id so that metrics are collected and exported
func (c *Collector) Add(id, namespace string, cg cgroups.Cgroup) error { func (c *Collector) Add(id, namespace string, cg cgroups.Cgroup) error {
if c.ns == nil {
return nil
}
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
if _, ok := c.cgroups[taskID(id, namespace)]; ok { if _, ok := c.cgroups[taskID(id, namespace)]; ok {
@ -104,32 +109,12 @@ func (c *Collector) Add(id, namespace string, cg cgroups.Cgroup) error {
return nil return nil
} }
// Get returns the cgroup that is being collected under the provided id
// returns ErrCgroupNotExists if the id is not being collected
func (c *Collector) Get(id, namespace string) (cgroups.Cgroup, error) {
c.mu.Lock()
defer c.mu.Unlock()
t, ok := c.cgroups[taskID(id, namespace)]
if !ok {
return nil, ErrCgroupNotExists
}
return t.cgroup, nil
}
// Remove removes the provided cgroup by id from the collector // Remove removes the provided cgroup by id from the collector
func (c *Collector) Remove(id, namespace string) { func (c *Collector) Remove(id, namespace string) {
if c.ns == nil {
return
}
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
delete(c.cgroups, taskID(id, namespace)) delete(c.cgroups, taskID(id, namespace))
} }
func blkioValues(l []*cgroups.BlkIOEntry) []value {
var out []value
for _, e := range l {
out = append(out, value{
v: float64(e.Value),
l: []string{e.Op, e.Device, strconv.FormatUint(e.Major, 10), strconv.FormatUint(e.Minor, 10)},
})
}
return out
}

View File

@ -19,12 +19,18 @@ func NewOOMCollector(ns *metrics.Namespace) (*OOMCollector, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
var desc *prometheus.Desc
if ns != nil {
desc = ns.NewDesc("memory_oom", "The number of times a container has received an oom event", metrics.Total, "container_id", "namespace")
}
c := &OOMCollector{ c := &OOMCollector{
fd: fd, fd: fd,
desc: ns.NewDesc("memory_oom", "The number of times a container has received an oom event", metrics.Total, "container_id", "namespace"), desc: desc,
set: make(map[uintptr]*oom), set: make(map[uintptr]*oom),
} }
ns.Add(c) if ns != nil {
ns.Add(c)
}
go c.start() go c.start()
return c, nil return c, nil
} }