Skip to content

Commit 6b7f334

Browse files
bborehamcofyc
authored andcommitted
Make Prometheus cAdvisor metrics labels consistent
Prometheus requires that all metrics in the same family have the same labels, so we arrange to supply blank strings for missing labels See google/cadvisor#1704
1 parent 2c2fe6e commit 6b7f334

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

pkg/kubelet/cadvisor/cadvisor_linux.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,29 @@ func init() {
7474
}
7575

7676
func containerLabels(c *cadvisorapi.ContainerInfo) map[string]string {
77-
set := map[string]string{metrics.LabelID: c.Name}
77+
// Prometheus requires that all metrics in the same family have the same labels,
78+
// so we arrange to supply blank strings for missing labels
79+
var name, image, podName, namespace, containerName string
7880
if len(c.Aliases) > 0 {
79-
set[metrics.LabelName] = c.Aliases[0]
80-
}
81-
if image := c.Spec.Image; len(image) > 0 {
82-
set[metrics.LabelImage] = image
81+
name = c.Aliases[0]
8382
}
83+
image = c.Spec.Image
8484
if v, ok := c.Spec.Labels[types.KubernetesPodNameLabel]; ok {
85-
set["pod_name"] = v
85+
podName = v
8686
}
8787
if v, ok := c.Spec.Labels[types.KubernetesPodNamespaceLabel]; ok {
88-
set["namespace"] = v
88+
namespace = v
8989
}
9090
if v, ok := c.Spec.Labels[types.KubernetesContainerNameLabel]; ok {
91-
set["container_name"] = v
91+
containerName = v
92+
}
93+
set := map[string]string{
94+
metrics.LabelID: c.Name,
95+
metrics.LabelName: name,
96+
metrics.LabelImage: image,
97+
"pod_name": podName,
98+
"namespace": namespace,
99+
"container_name": containerName,
92100
}
93101
return set
94102
}

pkg/kubelet/server/server.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -816,21 +816,29 @@ func (a prometheusHostAdapter) GetMachineInfo() (*cadvisorapi.MachineInfo, error
816816

817817
// containerPrometheusLabels maps cAdvisor labels to prometheus labels.
818818
func containerPrometheusLabels(c *cadvisorapi.ContainerInfo) map[string]string {
819-
set := map[string]string{metrics.LabelID: c.Name}
819+
// Prometheus requires that all metrics in the same family have the same labels,
820+
// so we arrange to supply blank strings for missing labels
821+
var name, image, podName, namespace, containerName string
820822
if len(c.Aliases) > 0 {
821-
set[metrics.LabelName] = c.Aliases[0]
822-
}
823-
if image := c.Spec.Image; len(image) > 0 {
824-
set[metrics.LabelImage] = image
823+
name = c.Aliases[0]
825824
}
825+
image = c.Spec.Image
826826
if v, ok := c.Spec.Labels[kubelettypes.KubernetesPodNameLabel]; ok {
827-
set["pod_name"] = v
827+
podName = v
828828
}
829829
if v, ok := c.Spec.Labels[kubelettypes.KubernetesPodNamespaceLabel]; ok {
830-
set["namespace"] = v
830+
namespace = v
831831
}
832832
if v, ok := c.Spec.Labels[kubelettypes.KubernetesContainerNameLabel]; ok {
833-
set["container_name"] = v
833+
containerName = v
834+
}
835+
set := map[string]string{
836+
metrics.LabelID: c.Name,
837+
metrics.LabelName: name,
838+
metrics.LabelImage: image,
839+
"pod_name": podName,
840+
"namespace": namespace,
841+
"container_name": containerName,
834842
}
835843
return set
836844
}

0 commit comments

Comments
 (0)