From 42b9ef11734ba0d0e4be26d8d8e0fda7b056589d Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Tue, 29 Aug 2017 19:50:42 -0400 Subject: [PATCH] UPSTREAM: 51473: Fix cAdvisor prometheus metrics --- .../pkg/kubelet/cadvisor/cadvisor_linux.go | 24 ++++++++++++------- .../kubernetes/pkg/kubelet/server/server.go | 24 ++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/vendor/k8s.io/kubernetes/pkg/kubelet/cadvisor/cadvisor_linux.go b/vendor/k8s.io/kubernetes/pkg/kubelet/cadvisor/cadvisor_linux.go index c82c76103d7a..8f8b3e8f877a 100644 --- a/vendor/k8s.io/kubernetes/pkg/kubelet/cadvisor/cadvisor_linux.go +++ b/vendor/k8s.io/kubernetes/pkg/kubelet/cadvisor/cadvisor_linux.go @@ -74,21 +74,29 @@ func init() { } func containerLabels(c *cadvisorapi.ContainerInfo) map[string]string { - set := map[string]string{metrics.LabelID: c.Name} + // Prometheus requires that all metrics in the same family have the same labels, + // so we arrange to supply blank strings for missing labels + var name, image, podName, namespace, containerName string if len(c.Aliases) > 0 { - set[metrics.LabelName] = c.Aliases[0] - } - if image := c.Spec.Image; len(image) > 0 { - set[metrics.LabelImage] = image + name = c.Aliases[0] } + image = c.Spec.Image if v, ok := c.Spec.Labels[types.KubernetesPodNameLabel]; ok { - set["pod_name"] = v + podName = v } if v, ok := c.Spec.Labels[types.KubernetesPodNamespaceLabel]; ok { - set["namespace"] = v + namespace = v } if v, ok := c.Spec.Labels[types.KubernetesContainerNameLabel]; ok { - set["container_name"] = v + containerName = v + } + set := map[string]string{ + metrics.LabelID: c.Name, + metrics.LabelName: name, + metrics.LabelImage: image, + "pod_name": podName, + "namespace": namespace, + "container_name": containerName, } return set } diff --git a/vendor/k8s.io/kubernetes/pkg/kubelet/server/server.go b/vendor/k8s.io/kubernetes/pkg/kubelet/server/server.go index efde8a6729d1..eadc94f5e4da 100644 --- a/vendor/k8s.io/kubernetes/pkg/kubelet/server/server.go +++ b/vendor/k8s.io/kubernetes/pkg/kubelet/server/server.go @@ -820,21 +820,29 @@ func (a prometheusHostAdapter) GetMachineInfo() (*cadvisorapi.MachineInfo, error // containerPrometheusLabels maps cAdvisor labels to prometheus labels. func containerPrometheusLabels(c *cadvisorapi.ContainerInfo) map[string]string { - set := map[string]string{metrics.LabelID: c.Name} + // Prometheus requires that all metrics in the same family have the same labels, + // so we arrange to supply blank strings for missing labels + var name, image, podName, namespace, containerName string if len(c.Aliases) > 0 { - set[metrics.LabelName] = c.Aliases[0] - } - if image := c.Spec.Image; len(image) > 0 { - set[metrics.LabelImage] = image + name = c.Aliases[0] } + image = c.Spec.Image if v, ok := c.Spec.Labels[kubelettypes.KubernetesPodNameLabel]; ok { - set["pod_name"] = v + podName = v } if v, ok := c.Spec.Labels[kubelettypes.KubernetesPodNamespaceLabel]; ok { - set["namespace"] = v + namespace = v } if v, ok := c.Spec.Labels[kubelettypes.KubernetesContainerNameLabel]; ok { - set["container_name"] = v + containerName = v + } + set := map[string]string{ + metrics.LabelID: c.Name, + metrics.LabelName: name, + metrics.LabelImage: image, + "pod_name": podName, + "namespace": namespace, + "container_name": containerName, } return set }