Allow the id for cgroup metrics to be changed

This makes the metrics package more extensible by allowing the default name of
`container_id` to be changed by the package caller.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2020-03-26 11:55:44 -04:00
parent 7bdec4e47f
commit d654dbafac
2 changed files with 8 additions and 2 deletions

View File

@ -24,6 +24,9 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
// IDName is the name that is used to identify the id being collected in the metric
var IDName = "container_id"
type value struct { type value struct {
v float64 v float64
l []string l []string
@ -41,7 +44,7 @@ type metric struct {
func (m *metric) desc(ns *metrics.Namespace) *prometheus.Desc { func (m *metric) desc(ns *metrics.Namespace) *prometheus.Desc {
// the namespace label is for containerd namespaces // the namespace label is for containerd namespaces
return ns.NewDesc(m.name, m.help, m.unit, append([]string{"container_id", "namespace"}, m.labels...)...) return ns.NewDesc(m.name, m.help, m.unit, append([]string{IDName, "namespace"}, m.labels...)...)
} }
func (m *metric) collect(id, namespace string, stats *v1.Metrics, ns *metrics.Namespace, ch chan<- prometheus.Metric, block bool) { func (m *metric) collect(id, namespace string, stats *v1.Metrics, ns *metrics.Namespace, ch chan<- prometheus.Metric, block bool) {

View File

@ -24,6 +24,9 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
// IDName is the name that is used to identify the id being collected in the metric
var IDName = "container_id"
type value struct { type value struct {
v float64 v float64
l []string l []string
@ -41,7 +44,7 @@ type metric struct {
func (m *metric) desc(ns *metrics.Namespace) *prometheus.Desc { func (m *metric) desc(ns *metrics.Namespace) *prometheus.Desc {
// the namespace label is for containerd namespaces // the namespace label is for containerd namespaces
return ns.NewDesc(m.name, m.help, m.unit, append([]string{"container_id", "namespace"}, m.labels...)...) return ns.NewDesc(m.name, m.help, m.unit, append([]string{IDName, "namespace"}, m.labels...)...)
} }
func (m *metric) collect(id, namespace string, stats *v2.Metrics, ns *metrics.Namespace, ch chan<- prometheus.Metric, block bool) { func (m *metric) collect(id, namespace string, stats *v2.Metrics, ns *metrics.Namespace, ch chan<- prometheus.Metric, block bool) {