Merge pull request #1488 from crosbymichael/prom-containers
Add config for exporting container metrics to prom
This commit is contained in:
commit
17901fafa0
@ -3,6 +3,8 @@
|
||||
package cgroups
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/containerd/cgroups"
|
||||
metrics "github.com/docker/go-metrics"
|
||||
"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
|
||||
}
|
||||
|
@ -15,24 +15,33 @@ import (
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
NoPrometheus bool `toml:"no_prometheus"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
plugin.Register(&plugin.Registration{
|
||||
Type: plugin.TaskMonitorPlugin,
|
||||
ID: "cgroups",
|
||||
Init: New,
|
||||
Type: plugin.TaskMonitorPlugin,
|
||||
ID: "cgroups",
|
||||
Init: New,
|
||||
Config: &Config{},
|
||||
})
|
||||
}
|
||||
|
||||
func New(ic *plugin.InitContext) (interface{}, error) {
|
||||
var (
|
||||
ns = metrics.NewNamespace("container", "", nil)
|
||||
collector = NewCollector(ns)
|
||||
)
|
||||
var ns *metrics.Namespace
|
||||
config := ic.Config.(*Config)
|
||||
if !config.NoPrometheus {
|
||||
ns = metrics.NewNamespace("container", "", nil)
|
||||
}
|
||||
collector := NewCollector(ns)
|
||||
oom, err := NewOOMCollector(ns)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
metrics.Register(ns)
|
||||
if ns != nil {
|
||||
metrics.Register(ns)
|
||||
}
|
||||
return &cgroupsMonitor{
|
||||
collector: collector,
|
||||
oom: oom,
|
||||
|
@ -5,7 +5,6 @@ package cgroups
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"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
|
||||
// that cgroups can be added for collection
|
||||
func NewCollector(ns *metrics.Namespace) *Collector {
|
||||
if ns == nil {
|
||||
return &Collector{}
|
||||
}
|
||||
// add machine cpus and memory info
|
||||
c := &Collector{
|
||||
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
|
||||
func (c *Collector) Add(id, namespace string, cg cgroups.Cgroup) error {
|
||||
if c.ns == nil {
|
||||
return nil
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
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
|
||||
}
|
||||
|
||||
// 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
|
||||
func (c *Collector) Remove(id, namespace string) {
|
||||
if c.ns == nil {
|
||||
return
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
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
|
||||
}
|
||||
|
@ -19,12 +19,18 @@ func NewOOMCollector(ns *metrics.Namespace) (*OOMCollector, error) {
|
||||
if err != nil {
|
||||
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{
|
||||
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),
|
||||
}
|
||||
ns.Add(c)
|
||||
if ns != nil {
|
||||
ns.Add(c)
|
||||
}
|
||||
go c.start()
|
||||
return c, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user