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

chore(kubelet): migrate metrics to contextual logging #130157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
Loading
from

Conversation

zhifei92
Copy link
Contributor

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

migrate metrics to contextual logging

Which issue(s) this PR fixes:

Fixes #
part of #130069

Special notes for your reviewer:

Does this PR introduce a user-facing change?

None

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/kubelet sig/node Categorizes an issue or PR as relevant to SIG Node. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Feb 14, 2025
pkg/kubelet/metrics/collectors/cri_metrics.go Outdated Show resolved Hide resolved
@@ -63,9 +63,11 @@ 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())
ctx := context.Background()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ctx := context.Background()
ctx := context.TODO()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review.

pkg/kubelet/metrics/collectors/resource_metrics.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 17, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: zhifei92
Once this PR has been reviewed and has the lgtm label, please assign dchen1107 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@@ -68,22 +70,24 @@ 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())
ctx := context.TODO()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment explaining when this should be removed.

@bart0sh
Copy link
Contributor

bart0sh commented Feb 17, 2025

@zhifei92 It would also make sense to look at the existing calls of the context.Background() and the context.TODO(). Some of them could be replaced by ctx or context.TODO() with a comment:

$ git grep '\(Background()\)\|\(TODO()\)' ./pkg/kubelet/metrics/
pkg/kubelet/metrics/collectors/cri_metrics.go:  ctx := context.TODO()
pkg/kubelet/metrics/collectors/log_metrics.go:  ctx := context.TODO()
pkg/kubelet/metrics/collectors/resource_metrics.go:     ctx := context.Background()
pkg/kubelet/metrics/collectors/resource_metrics_test.go:                        ctx := context.Background()
pkg/kubelet/metrics/collectors/volume_stats.go: ctx := context.Background()
pkg/kubelet/metrics/collectors/volume_stats_test.go:    ctx := context.Background()
pkg/kubelet/metrics/collectors/volume_stats_test.go:    ctx := context.Background()

@zhifei92
Copy link
Contributor Author

It would also make sense to look at the existing calls of the context.Background() and the context.TODO().

Sorry for not reading the requirements document carefully.

@bart0sh
Copy link
Contributor

bart0sh commented Feb 17, 2025

/triage accepted
/priority important-longterm

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Feb 17, 2025
@zhifei92 zhifei92 force-pushed the migrate-kubelet-metrics-to-contextual-logging branch from 77c80d7 to e87dcd1 Compare February 18, 2025 12:08
@bart0sh
Copy link
Contributor

bart0sh commented Feb 18, 2025

/assign

@zhifei92
Copy link
Contributor Author

/retest

@zhifei92 zhifei92 force-pushed the migrate-kubelet-metrics-to-contextual-logging branch from e87dcd1 to 1c7e55f Compare February 25, 2025 09:31
@bart0sh
Copy link
Contributor

bart0sh commented Feb 25, 2025

@zhifei92 I can see context.TODO() calls, even in tests. Are you sure we can't remove them or move to the upper level?

@zhifei92
Copy link
Contributor Author

@zhifei92 I can see context.TODO() calls, even in tests. Are you sure we can't remove them or move to the upper level?

Because the gomock method verifies the parameters passed to the GetCPUAndMemoryStats method (including the specific type of context), I am unsure whether to continue using context.TODO() or to use gomock.Any() to ignore the context parameter.

Like this:

	for _, test := range tests {
		tc := test
		t.Run(tc.name, func(t *testing.T) {
			ctx := context.TODO()
			provider := summaryprovidertest.NewMockSummaryProvider(t)
			provider.EXPECT().GetCPUAndMemoryStats(ctx).Return(tc.summary, tc.summaryErr).Maybe()

@bart0sh
Copy link
Contributor

bart0sh commented Feb 25, 2025

Because the gomock method verifies the parameters passed to the GetCPUAndMemoryStats method (including the specific type of context), I am unsure whether to continue using context.TODO() or to use gomock.Any() to ignore the context parameter.

Can you show how it would look like with gomock.Any() ?

@bart0sh
Copy link
Contributor

bart0sh commented Feb 27, 2025

@zhifei92

Because the gomock method verifies the parameters passed to the GetCPUAndMemoryStats method (including the specific type of context), I am unsure whether to continue using context.TODO() or to use gomock.Any() to ignore the context parameter.

Even if we manage to use the same context it wouldn't help. reflect.DeepEqual, which is called by mocking APIs to verify parameters, is not good at comparing interfaces, context.Context in this particular case. As an example, this trivial test case always fails:

func TestCtxEquality(t *testing.T) {
	ctx := ktesting.Init(t)
	if !reflect.DeepEqual(ctx, ctx) {
		t.Fatal("not equal")
	}
} 

Can we mock context with mock.MatchedBy to make any argument implementing context.Context to match?

If this is not possible, can we switch to passing logger instead of context?

@zhifei92
Copy link
Contributor Author

zhifei92 commented Feb 28, 2025

Can you show how it would look like with gomock.Any() ?

Using mock.Anything to ignore the comparison of ctx in a mock API is simpler than using MatchedBy. How do you see this?

	// Anything is used in Diff and Assert when the argument being tested
	// shouldn't be taken into consideration.
	Anything = "mock.Anything"

@zhifei92
Copy link
Contributor Author

If this is not possible, can we switch to passing logger instead of context?

This approach is not feasible, since it involves making substantial modifications to the existing codebase.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 3, 2025
@zhifei92 zhifei92 force-pushed the migrate-kubelet-metrics-to-contextual-logging branch from 1c7e55f to fbe6955 Compare May 13, 2025 08:13
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 13, 2025
@k8s-ci-robot
Copy link
Contributor

@zhifei92: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-unit-windows-master fbe6955 link false /test pull-kubernetes-unit-windows-master

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubelet cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note-none Denotes a PR that doesn't merit a release note. sig/node Categorizes an issue or PR as relevant to SIG Node. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Development

Successfully merging this pull request may close these issues.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.