Add runtime label to metrics

Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
Maksym Pavlenko
2021-07-23 10:04:46 -07:00
parent 34fb8d8967
commit efa8ab7158
6 changed files with 65 additions and 31 deletions

View File

@@ -28,7 +28,7 @@ import (
v1 "github.com/containerd/containerd/metrics/types/v1"
"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"
)
@@ -53,7 +53,7 @@ func NewCollector(ns *metrics.Namespace) *Collector {
// add machine cpus and memory info
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...)
@@ -69,12 +69,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
@@ -109,10 +116,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 {
@@ -129,13 +137,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
}
@@ -145,7 +157,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
}
@@ -165,6 +181,6 @@ func (c *Collector) RemoveAll() {
return
}
c.mu.Lock()
c.tasks = make(map[string]Statable)
c.tasks = make(map[string]entry)
c.mu.Unlock()
}