diff --git a/pkg/kubelet/metrics/collectors/cri_metrics.go b/pkg/kubelet/metrics/collectors/cri_metrics.go index 8736eb9e319..9ae50db409a 100644 --- a/pkg/kubelet/metrics/collectors/cri_metrics.go +++ b/pkg/kubelet/metrics/collectors/cri_metrics.go @@ -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. diff --git a/pkg/kubelet/metrics/collectors/log_metrics.go b/pkg/kubelet/metrics/collectors/log_metrics.go index 4b2237fbbad..1443557a1f6 100644 --- a/pkg/kubelet/metrics/collectors/log_metrics.go +++ b/pkg/kubelet/metrics/collectors/log_metrics.go @@ -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 } diff --git a/pkg/kubelet/metrics/collectors/resource_metrics.go b/pkg/kubelet/metrics/collectors/resource_metrics.go index dff6a812a04..c7b566a36c7 100644 --- a/pkg/kubelet/metrics/collectors/resource_metrics.go +++ b/pkg/kubelet/metrics/collectors/resource_metrics.go @@ -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 } diff --git a/pkg/kubelet/metrics/collectors/resource_metrics_test.go b/pkg/kubelet/metrics/collectors/resource_metrics_test.go index 40953dd0abf..9aa533f6727 100644 --- a/pkg/kubelet/metrics/collectors/resource_metrics_test.go +++ b/pkg/kubelet/metrics/collectors/resource_metrics_test.go @@ -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) diff --git a/pkg/kubelet/metrics/collectors/volume_stats.go b/pkg/kubelet/metrics/collectors/volume_stats.go index b139cf5eaec..6b8aaac51ec 100644 --- a/pkg/kubelet/metrics/collectors/volume_stats.go +++ b/pkg/kubelet/metrics/collectors/volume_stats.go @@ -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 diff --git a/pkg/kubelet/metrics/collectors/volume_stats_test.go b/pkg/kubelet/metrics/collectors/volume_stats_test.go index 73bc1541610..f0caf19672a 100644 --- a/pkg/kubelet/metrics/collectors/volume_stats_test.go +++ b/pkg/kubelet/metrics/collectors/volume_stats_test.go @@ -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 = `