Merge pull request #130157 from zhifei92/migrate-kubelet-metrics-to-contextual-logging

chore(kubelet): migrate metrics to contextual logging
This commit is contained in:
Kubernetes Prow Robot
2025-09-08 12:21:27 -07:00
committed by GitHub
6 changed files with 30 additions and 16 deletions

View File

@@ -41,7 +41,8 @@ var _ metrics.StableCollector = &criMetricsCollector{}
func NewCRIMetricsCollector(ctx context.Context, listPodSandboxMetricsFn func(context.Context) ([]*runtimeapi.PodSandboxMetrics, error), listMetricDescriptorsFn func(context.Context) ([]*runtimeapi.MetricDescriptor, error)) metrics.StableCollector {
descs, err := listMetricDescriptorsFn(ctx)
if err != nil {
klog.ErrorS(err, "Error reading MetricDescriptors")
logger := klog.FromContext(ctx)
logger.Error(err, "Error reading MetricDescriptors")
return &criMetricsCollector{
listPodSandboxMetricsFn: listPodSandboxMetricsFn,
}
@@ -68,22 +69,26 @@ func (c *criMetricsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
// Collect implements the metrics.CollectWithStability interface.
// TODO(haircommander): would it be better if these were processed async?
func (c *criMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
podMetrics, err := c.listPodSandboxMetricsFn(context.Background())
// Use context.TODO() because we currently do not have a proper context to pass in.
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
ctx := context.TODO()
logger := klog.FromContext(ctx)
podMetrics, err := c.listPodSandboxMetricsFn(ctx)
if err != nil {
klog.ErrorS(err, "Failed to get pod metrics")
logger.Error(err, "Failed to get pod metrics")
return
}
for _, podMetric := range podMetrics {
for _, metric := range podMetric.GetMetrics() {
promMetric, err := c.criMetricToProm(metric)
promMetric, err := c.criMetricToProm(logger, metric)
if err == nil {
ch <- promMetric
}
}
for _, ctrMetric := range podMetric.GetContainerMetrics() {
for _, metric := range ctrMetric.GetMetrics() {
promMetric, err := c.criMetricToProm(metric)
promMetric, err := c.criMetricToProm(logger, metric)
if err == nil {
ch <- promMetric
}
@@ -98,11 +103,11 @@ func criDescToProm(m *runtimeapi.MetricDescriptor) *metrics.Desc {
return metrics.NewDesc(m.Name, m.Help, m.LabelKeys, nil, metrics.INTERNAL, "")
}
func (c *criMetricsCollector) criMetricToProm(m *runtimeapi.Metric) (metrics.Metric, error) {
func (c *criMetricsCollector) criMetricToProm(logger klog.Logger, m *runtimeapi.Metric) (metrics.Metric, error) {
desc, ok := c.descriptors[m.Name]
if !ok {
err := fmt.Errorf("error converting CRI Metric to prometheus format")
klog.V(5).ErrorS(err, "Descriptor not present in pre-populated list of descriptors", "name", m.Name)
logger.V(5).Error(err, "Descriptor not present in pre-populated list of descriptors", "name", m.Name)
return nil, err
}
@@ -110,7 +115,7 @@ func (c *criMetricsCollector) criMetricToProm(m *runtimeapi.Metric) (metrics.Met
pm, err := metrics.NewConstMetric(desc, typ, float64(m.GetValue().Value), m.LabelValues...)
if err != nil {
klog.ErrorS(err, "Error getting CRI prometheus metric", "descriptor", desc.String())
logger.Error(err, "Error getting CRI prometheus metric", "descriptor", desc.String())
return nil, err
}
// If Timestamp is 0, then the runtime did not cache the result.

View File

@@ -63,9 +63,13 @@ func (c *logMetricsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
// CollectWithStability implements the metrics.StableCollector interface.
func (c *logMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
podStats, err := c.podStats(context.Background())
// Use context.TODO() because we currently do not have a proper context to pass in.
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
ctx := context.TODO()
logger := klog.FromContext(ctx)
podStats, err := c.podStats(ctx)
if err != nil {
klog.ErrorS(err, "Failed to get pod stats")
logger.Error(err, "Failed to get pod stats")
return
}

View File

@@ -157,7 +157,9 @@ func (rc *resourceMetricsCollector) DescribeWithStability(ch chan<- *metrics.Des
// leak metric collectors for containers or pods that no longer exist. Instead, implement
// custom collector in a way that only collects metrics for active containers.
func (rc *resourceMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
ctx := context.Background()
// Use context.TODO() because we currently do not have a proper context to pass in.
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
ctx := context.TODO()
var errorCount float64
defer func() {
ch <- metrics.NewLazyConstMetric(resourceScrapeResultDesc, metrics.GaugeValue, errorCount)
@@ -165,8 +167,9 @@ func (rc *resourceMetricsCollector) CollectWithStability(ch chan<- metrics.Metri
}()
statsSummary, err := rc.provider.GetCPUAndMemoryStats(ctx)
if err != nil {
logger := klog.FromContext(ctx)
errorCount = 1
klog.ErrorS(err, "Error getting summary for resourceMetric prometheus endpoint")
logger.Error(err, "Error getting summary for resourceMetric prometheus endpoint")
return
}

View File

@@ -410,7 +410,7 @@ func TestCollectResourceMetrics(t *testing.T) {
for _, test := range tests {
tc := test
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
ctx := context.TODO()
provider := summaryprovidertest.NewMockSummaryProvider(t)
provider.EXPECT().GetCPUAndMemoryStats(ctx).Return(tc.summary, tc.summaryErr).Maybe()
collector := NewResourceMetricsCollector(provider)

View File

@@ -98,7 +98,9 @@ func (collector *volumeStatsCollector) DescribeWithStability(ch chan<- *metrics.
// CollectWithStability implements the metrics.StableCollector interface.
func (collector *volumeStatsCollector) CollectWithStability(ch chan<- metrics.Metric) {
ctx := context.Background()
// Use context.TODO() because we currently do not have a proper context to pass in.
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
ctx := context.TODO()
podStats, err := collector.statsProvider.ListPodStats(ctx)
if err != nil {
return

View File

@@ -29,7 +29,7 @@ import (
)
func TestVolumeStatsCollector(t *testing.T) {
ctx := context.Background()
ctx := context.TODO()
// Fixed metadata on type and help text. We prepend this to every expected
// output so we only have to modify a single place when doing adjustments.
const metadata = `
@@ -148,7 +148,7 @@ func TestVolumeStatsCollector(t *testing.T) {
}
func TestVolumeStatsCollectorWithNullVolumeStatus(t *testing.T) {
ctx := context.Background()
ctx := context.TODO()
// Fixed metadata on type and help text. We prepend this to every expected
// output so we only have to modify a single place when doing adjustments.
const metadata = `