Add runtime label to metrics
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
@@ -23,7 +23,7 @@ import (
|
||||
|
||||
"github.com/containerd/containerd/events"
|
||||
"github.com/containerd/containerd/runtime"
|
||||
metrics "github.com/docker/go-metrics"
|
||||
"github.com/docker/go-metrics"
|
||||
)
|
||||
|
||||
// NewTaskMonitor returns a new cgroups monitor
|
||||
@@ -42,8 +42,8 @@ type cgroupsMonitor struct {
|
||||
publisher events.Publisher
|
||||
}
|
||||
|
||||
func (m *cgroupsMonitor) Monitor(c runtime.Task) error {
|
||||
if err := m.collector.Add(c); err != nil {
|
||||
func (m *cgroupsMonitor) Monitor(c runtime.Task, labels map[string]string) error {
|
||||
if err := m.collector.Add(c, labels); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -27,7 +27,7 @@ import (
|
||||
v2 "github.com/containerd/containerd/metrics/types/v2"
|
||||
"github.com/containerd/containerd/namespaces"
|
||||
"github.com/containerd/typeurl"
|
||||
metrics "github.com/docker/go-metrics"
|
||||
"github.com/docker/go-metrics"
|
||||
"github.com/gogo/protobuf/types"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
@@ -47,7 +47,7 @@ func NewCollector(ns *metrics.Namespace) *Collector {
|
||||
}
|
||||
c := &Collector{
|
||||
ns: ns,
|
||||
tasks: make(map[string]Statable),
|
||||
tasks: make(map[string]entry),
|
||||
}
|
||||
c.metrics = append(c.metrics, pidMetrics...)
|
||||
c.metrics = append(c.metrics, cpuMetrics...)
|
||||
@@ -62,12 +62,19 @@ func taskID(id, namespace string) string {
|
||||
return fmt.Sprintf("%s-%s", id, namespace)
|
||||
}
|
||||
|
||||
type entry struct {
|
||||
task Statable
|
||||
// ns is an optional child namespace that contains additional to parent labels.
|
||||
// This can be used to append task specific labels to be able to differentiate the different containerd metrics.
|
||||
ns *metrics.Namespace
|
||||
}
|
||||
|
||||
// Collector provides the ability to collect container stats and export
|
||||
// them in the prometheus format
|
||||
type Collector struct {
|
||||
mu sync.RWMutex
|
||||
|
||||
tasks map[string]Statable
|
||||
tasks map[string]entry
|
||||
ns *metrics.Namespace
|
||||
metrics []*metric
|
||||
storedMetrics chan prometheus.Metric
|
||||
@@ -102,10 +109,11 @@ storedLoop:
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (c *Collector) collect(t Statable, ch chan<- prometheus.Metric, block bool, wg *sync.WaitGroup) {
|
||||
func (c *Collector) collect(entry entry, ch chan<- prometheus.Metric, block bool, wg *sync.WaitGroup) {
|
||||
if wg != nil {
|
||||
defer wg.Done()
|
||||
}
|
||||
t := entry.task
|
||||
ctx := namespaces.WithNamespace(context.Background(), t.Namespace())
|
||||
stats, err := t.Stats(ctx)
|
||||
if err != nil {
|
||||
@@ -122,13 +130,17 @@ func (c *Collector) collect(t Statable, ch chan<- prometheus.Metric, block bool,
|
||||
log.L.WithError(err).Errorf("invalid metric type for %s", t.ID())
|
||||
return
|
||||
}
|
||||
ns := entry.ns
|
||||
if ns == nil {
|
||||
ns = c.ns
|
||||
}
|
||||
for _, m := range c.metrics {
|
||||
m.collect(t.ID(), t.Namespace(), s, c.ns, ch, block)
|
||||
m.collect(t.ID(), t.Namespace(), s, ns, ch, block)
|
||||
}
|
||||
}
|
||||
|
||||
// Add adds the provided cgroup and id so that metrics are collected and exported
|
||||
func (c *Collector) Add(t Statable) error {
|
||||
func (c *Collector) Add(t Statable, labels map[string]string) error {
|
||||
if c.ns == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -138,7 +150,11 @@ func (c *Collector) Add(t Statable) error {
|
||||
if _, ok := c.tasks[id]; ok {
|
||||
return nil // requests to collect metrics should be idempotent
|
||||
}
|
||||
c.tasks[id] = t
|
||||
entry := entry{task: t}
|
||||
if labels != nil {
|
||||
entry.ns = c.ns.WithConstLabels(labels)
|
||||
}
|
||||
c.tasks[id] = entry
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -158,6 +174,6 @@ func (c *Collector) RemoveAll() {
|
||||
return
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.tasks = make(map[string]Statable)
|
||||
c.tasks = make(map[string]entry)
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user