Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fbe6955

Browse filesBrowse files
committed
chore(kubelet): migrate metrics to contextual logging.
1 parent fd15e3f commit fbe6955
Copy full SHA for fbe6955

File tree

Expand file treeCollapse file tree

9 files changed

+33
-16
lines changed
Filter options
Expand file treeCollapse file tree

9 files changed

+33
-16
lines changed

‎hack/golangci-hints.yaml

Copy file name to clipboardExpand all lines: hack/golangci-hints.yaml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ linters:
193193
contextual k8s.io/kubernetes/pkg/kubelet/clustertrustbundle/.*
194194
contextual k8s.io/kubernetes/pkg/kubelet/token/.*
195195
contextual k8s.io/kubernetes/pkg/kubelet/cadvisor/.*
196+
contextual k8s.io/kubernetes/pkg/kubelet/metrics/collectors/.*
196197
contextual k8s.io/kubernetes/pkg/kubelet/oom/.*
197198
contextual k8s.io/kubernetes/pkg/kubelet/status/.*
198199
contextual k8s.io/kubernetes/pkg/kubelet/sysctl/.*

‎hack/golangci.yaml

Copy file name to clipboardExpand all lines: hack/golangci.yaml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ linters:
207207
contextual k8s.io/kubernetes/pkg/kubelet/clustertrustbundle/.*
208208
contextual k8s.io/kubernetes/pkg/kubelet/token/.*
209209
contextual k8s.io/kubernetes/pkg/kubelet/cadvisor/.*
210+
contextual k8s.io/kubernetes/pkg/kubelet/metrics/collectors/.*
210211
contextual k8s.io/kubernetes/pkg/kubelet/oom/.*
211212
contextual k8s.io/kubernetes/pkg/kubelet/status/.*
212213
contextual k8s.io/kubernetes/pkg/kubelet/sysctl/.*

‎hack/logcheck.conf

Copy file name to clipboardExpand all lines: hack/logcheck.conf
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ contextual k8s.io/kubernetes/pkg/kubelet/pleg/.*
5454
contextual k8s.io/kubernetes/pkg/kubelet/clustertrustbundle/.*
5555
contextual k8s.io/kubernetes/pkg/kubelet/token/.*
5656
contextual k8s.io/kubernetes/pkg/kubelet/cadvisor/.*
57+
contextual k8s.io/kubernetes/pkg/kubelet/metrics/collectors/.*
5758
contextual k8s.io/kubernetes/pkg/kubelet/oom/.*
5859
contextual k8s.io/kubernetes/pkg/kubelet/status/.*
5960
contextual k8s.io/kubernetes/pkg/kubelet/sysctl/.*

‎pkg/kubelet/metrics/collectors/cri_metrics.go

Copy file name to clipboardExpand all lines: pkg/kubelet/metrics/collectors/cri_metrics.go
+13-8Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ var _ metrics.StableCollector = &criMetricsCollector{}
4141
func NewCRIMetricsCollector(ctx context.Context, listPodSandboxMetricsFn func(context.Context) ([]*runtimeapi.PodSandboxMetrics, error), listMetricDescriptorsFn func(context.Context) ([]*runtimeapi.MetricDescriptor, error)) metrics.StableCollector {
4242
descs, err := listMetricDescriptorsFn(ctx)
4343
if err != nil {
44-
klog.ErrorS(err, "Error reading MetricDescriptors")
44+
logger := klog.FromContext(ctx)
45+
logger.Error(err, "Error reading MetricDescriptors")
4546
return &criMetricsCollector{
4647
listPodSandboxMetricsFn: listPodSandboxMetricsFn,
4748
}
@@ -68,22 +69,26 @@ func (c *criMetricsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
6869
// Collect implements the metrics.CollectWithStability interface.
6970
// TODO(haircommander): would it be better if these were processed async?
7071
func (c *criMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
71-
podMetrics, err := c.listPodSandboxMetricsFn(context.Background())
72+
// Use context.TODO() because we currently do not have a proper context to pass in.
73+
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
74+
ctx := context.TODO()
75+
logger := klog.FromContext(ctx)
76+
podMetrics, err := c.listPodSandboxMetricsFn(ctx)
7277
if err != nil {
73-
klog.ErrorS(err, "Failed to get pod metrics")
78+
logger.Error(err, "Failed to get pod metrics")
7479
return
7580
}
7681

7782
for _, podMetric := range podMetrics {
7883
for _, metric := range podMetric.GetMetrics() {
79-
promMetric, err := c.criMetricToProm(metric)
84+
promMetric, err := c.criMetricToProm(logger, metric)
8085
if err == nil {
8186
ch <- promMetric
8287
}
8388
}
8489
for _, ctrMetric := range podMetric.GetContainerMetrics() {
8590
for _, metric := range ctrMetric.GetMetrics() {
86-
promMetric, err := c.criMetricToProm(metric)
91+
promMetric, err := c.criMetricToProm(logger, metric)
8792
if err == nil {
8893
ch <- promMetric
8994
}
@@ -98,19 +103,19 @@ func criDescToProm(m *runtimeapi.MetricDescriptor) *metrics.Desc {
98103
return metrics.NewDesc(m.Name, m.Help, m.LabelKeys, nil, metrics.INTERNAL, "")
99104
}
100105

101-
func (c *criMetricsCollector) criMetricToProm(m *runtimeapi.Metric) (metrics.Metric, error) {
106+
func (c *criMetricsCollector) criMetricToProm(logger klog.Logger, m *runtimeapi.Metric) (metrics.Metric, error) {
102107
desc, ok := c.descriptors[m.Name]
103108
if !ok {
104109
err := fmt.Errorf("error converting CRI Metric to prometheus format")
105-
klog.V(5).ErrorS(err, "Descriptor not present in pre-populated list of descriptors", "name", m.Name)
110+
logger.V(5).Error(err, "Descriptor not present in pre-populated list of descriptors", "name", m.Name)
106111
return nil, err
107112
}
108113

109114
typ := criTypeToProm[m.MetricType]
110115

111116
pm, err := metrics.NewConstMetric(desc, typ, float64(m.GetValue().Value), m.LabelValues...)
112117
if err != nil {
113-
klog.ErrorS(err, "Error getting CRI prometheus metric", "descriptor", desc.String())
118+
logger.Error(err, "Error getting CRI prometheus metric", "descriptor", desc.String())
114119
return nil, err
115120
}
116121
// If Timestamp is 0, then the runtime did not cache the result.

‎pkg/kubelet/metrics/collectors/log_metrics.go

Copy file name to clipboardExpand all lines: pkg/kubelet/metrics/collectors/log_metrics.go
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@ func (c *logMetricsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
6363

6464
// CollectWithStability implements the metrics.StableCollector interface.
6565
func (c *logMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
66-
podStats, err := c.podStats(context.Background())
66+
// Use context.TODO() because we currently do not have a proper context to pass in.
67+
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
68+
ctx := context.TODO()
69+
logger := klog.FromContext(ctx)
70+
podStats, err := c.podStats(ctx)
6771
if err != nil {
68-
klog.ErrorS(err, "Failed to get pod stats")
72+
logger.Error(err, "Failed to get pod stats")
6973
return
7074
}
7175

‎pkg/kubelet/metrics/collectors/resource_metrics.go

Copy file name to clipboardExpand all lines: pkg/kubelet/metrics/collectors/resource_metrics.go
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,19 @@ func (rc *resourceMetricsCollector) DescribeWithStability(ch chan<- *metrics.Des
149149
// leak metric collectors for containers or pods that no longer exist. Instead, implement
150150
// custom collector in a way that only collects metrics for active containers.
151151
func (rc *resourceMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
152-
ctx := context.Background()
152+
// Use context.TODO() because we currently do not have a proper context to pass in.
153+
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
154+
ctx := context.TODO()
153155
var errorCount float64
154156
defer func() {
155157
ch <- metrics.NewLazyConstMetric(resourceScrapeResultDesc, metrics.GaugeValue, errorCount)
156158
ch <- metrics.NewLazyConstMetric(resourceScrapeErrorResultDesc, metrics.GaugeValue, errorCount)
157159
}()
158160
statsSummary, err := rc.provider.GetCPUAndMemoryStats(ctx)
159161
if err != nil {
162+
logger := klog.FromContext(ctx)
160163
errorCount = 1
161-
klog.ErrorS(err, "Error getting summary for resourceMetric prometheus endpoint")
164+
logger.Error(err, "Error getting summary for resourceMetric prometheus endpoint")
162165
return
163166
}
164167

‎pkg/kubelet/metrics/collectors/resource_metrics_test.go

Copy file name to clipboardExpand all lines: pkg/kubelet/metrics/collectors/resource_metrics_test.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ func TestCollectResourceMetrics(t *testing.T) {
404404
for _, test := range tests {
405405
tc := test
406406
t.Run(tc.name, func(t *testing.T) {
407-
ctx := context.Background()
407+
ctx := context.TODO()
408408
provider := summaryprovidertest.NewMockSummaryProvider(t)
409409
provider.EXPECT().GetCPUAndMemoryStats(ctx).Return(tc.summary, tc.summaryErr).Maybe()
410410
collector := NewResourceMetricsCollector(provider)

‎pkg/kubelet/metrics/collectors/volume_stats.go

Copy file name to clipboardExpand all lines: pkg/kubelet/metrics/collectors/volume_stats.go
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ func (collector *volumeStatsCollector) DescribeWithStability(ch chan<- *metrics.
9898

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

‎pkg/kubelet/metrics/collectors/volume_stats_test.go

Copy file name to clipboardExpand all lines: pkg/kubelet/metrics/collectors/volume_stats_test.go
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func newUint64Pointer(i uint64) *uint64 {
3232
}
3333

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

153153
func TestVolumeStatsCollectorWithNullVolumeStatus(t *testing.T) {
154-
ctx := context.Background()
154+
ctx := context.TODO()
155155
// Fixed metadata on type and help text. We prepend this to every expected
156156
// output so we only have to modify a single place when doing adjustments.
157157
const metadata = `

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.