Skip to content

cri: route sandbox stats through Controller.Metrics - #13312

Merged
mxpv merged 3 commits into
containerd:mainfrom
mxpv:stats
Jun 29, 2026
Merged

mxpv merged 3 commits into
containerd:mainfrom
mxpv:stats

Conversation

@mxpv

@mxpv mxpv commented Apr 28, 2026 •

Copy link
Copy Markdown
Member
  • Currently, Metrics endpoint in podsandbox/ is noop, everything is happening in CRI layer. This PR move cgroups metrics queries behind Controller, leaving CRI decoding logic.
  • Updates stats collector to call controller's metrics API instead of duplicating cgroups logic.

Copilot AI review requested due to automatic review settings April 28, 2026 19:05
@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review Apr 28, 2026
@dosubot dosubot Bot added area/cri Container Runtime Interface (CRI) kind/refactor labels Apr 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR moves pod sandbox cgroup metrics collection out of the CRI server layer and into the pod sandbox Controller.Metrics implementation, leaving the CRI layer responsible for decoding and shaping the CRI PodSandboxStats response.

Changes:

  • Update CRI sandbox stats path to call SandboxController(...).Metrics(...) and decode returned types.Metric data via typeurl.
  • Implement Linux podsandbox.Controller.Metrics to read parent cgroup CPU/memory stats and return them as a *types.Metric.
  • Simplify cpuContainerStats / memoryContainerStats helpers to take only cgroupMetrics + timestamp, updating call sites and tests.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/cri/server/sandbox_stats_linux.go Switch sandbox stats to fetch metrics via controller and decode them into cgroupMetrics.
internal/cri/server/podsandbox/sandbox_stats_linux.go New Linux implementation of Controller.Metrics that reads cgroup v1/v2 stats and marshals them into types.Metric.
internal/cri/server/podsandbox/sandbox_stats_other.go Non-Linux stub for Controller.Metrics guarded by //go:build !linux.
internal/cri/server/container_stats_list.go Update stats helpers’ signatures and adjust container metrics path accordingly.
internal/cri/server/container_stats_list_test.go Update tests to match the updated memoryContainerStats signature.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mxpv mxpv moved this from Needs Triage to Needs Reviewers in Pull Request Review May 11, 2026
Copilot AI review requested due to automatic review settings June 17, 2026 18:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Comment thread internal/cri/server/stats_collector.go Resolved
Comment thread internal/cri/server/stats_collector.go Resolved
Copilot AI review requested due to automatic review settings June 17, 2026 19:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Comment thread internal/cri/server/stats_collector.go Resolved
@dmcgowan dmcgowan added this to the 2.4 milestone Jun 18, 2026
@github-project-automation github-project-automation Bot moved this from Needs Reviewers to Review In Progress in Pull Request Review Jun 29, 2026
@mxpv
mxpv added this pull request to the merge queue Jun 29, 2026
// Sample at the metric's own timestamp, captured by the controller at
// cgroup read time. This keeps the delta-time in the UsageNanoCores
// rate accurate and free of per-sandbox collection latency.
c.addSample(sb.ID, metric.GetTimestamp().AsTime(), usageCoreNanoSeconds)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

metric.GetTimestamp().AsTime() is correct for podsandbox (it sets time.Now()), but a controller that leaves Timestamp nil yields the Unix epoch here rather than being skipped, feeding a garbage delta into the UsageNanoCores rate. The same latent issue exists on the container path, but the sandbox path now inherits it via arbitrary controllers. Worth guarding with a zero-timestamp check (skip, or fall back to time.Now()).

Review generated from Claude Opus 4.8.

if err != nil {
return nil, fmt.Errorf("failed to get controller for sandbox %s: %w", sandbox.ID, err)
}
metric, err := ctrl.Metrics(ctx, sandbox.ID)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Routing through ctrl.Metrics here changes the failure surface of ListPodSandboxStats. Previously this path read the parent cgroup directly and could only fail with cgroup load/read errors; now any controller that doesn't implement metrics returns errdefs.ErrNotImplemented (gRPC Unimplemented, not Unavailable).

In sandbox_stats_list.go only Unavailable/NotFound/ttrpc.ErrClosed are tolerated — everything else goes into errs[i] and errors.Join fails the whole RPC. So on a node mixing the default podsandbox sandboxer with a shim/remote sandboxer whose controller lacks SandboxMetrics, a single such pod breaks stats for every pod on the node.

The background collector (collectSandboxStats) already tolerates this — logs at debug and continues. The synchronous path should be similarly forgiving, e.g. also skip on errdefs.IsNotImplemented(err).

Review generated from Claude Opus 4.8.

@mxpv mxpv Jun 29, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is probably valid, but also quite exotic edge case today.
Will follow up as I plan more work around sandbox APIs.

@github-project-automation github-project-automation Bot moved this from Review In Progress to Needs Update in Pull Request Review Jun 29, 2026
Merged via the queue into containerd:main with commit b13f929 Jun 29, 2026
85 of 88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/cri Container Runtime Interface (CRI) kind/refactor size/L

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants