Skip to content

support disk usage metrics for containerd #3569

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions container/containerd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

containersapi "github.com/containerd/containerd/api/services/containers/v1"
snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
tasksapi "github.com/containerd/containerd/api/services/tasks/v1"
versionapi "github.com/containerd/containerd/api/services/version/v1"
tasktypes "github.com/containerd/containerd/api/types/task"
Expand Down Expand Up @@ -54,6 +55,7 @@ var (

var once sync.Once
var ctrdClient ContainerdClient = nil
var ctrdSnapshotsClient snapshotsapi.SnapshotsClient = nil

const (
maxBackoffDelay = 3 * time.Second
Expand Down Expand Up @@ -105,6 +107,7 @@ func Client(address, namespace string) (ContainerdClient, error) {
taskService: tasksapi.NewTasksClient(conn),
versionService: versionapi.NewVersionClient(conn),
}
ctrdSnapshotsClient = snapshotsapi.NewSnapshotsClient(conn)
})
return ctrdClient, retErr
}
Expand Down
28 changes: 25 additions & 3 deletions container/containerd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/net/context"

snapshotsapi "github.com/containerd/containerd/api/services/snapshots/v1"
"github.com/google/cadvisor/container"
"github.com/google/cadvisor/container/common"
containerlibcontainer "github.com/google/cadvisor/container/libcontainer"
Expand All @@ -46,6 +47,9 @@ type containerdContainerHandler struct {
labels map[string]string
// Image name used for this container.
image string
// Snapshot key used for this container.
Snapshotter string
SnapshotKey string
// Filesystem handler.
includedMetrics container.MetricSet

Expand Down Expand Up @@ -146,6 +150,9 @@ func newContainerdContainerHandler(
}
// Add the name and bare ID as aliases of the container.
handler.image = cntr.Image
// Add the snapshot key of the container.
handler.Snapshotter = cntr.Snapshotter
handler.SnapshotKey = cntr.SnapshotKey

for _, exposedEnv := range metadataEnvAllowList {
if exposedEnv == "" {
Expand All @@ -171,9 +178,7 @@ func (h *containerdContainerHandler) ContainerReference() (info.ContainerReferen
}

func (h *containerdContainerHandler) GetSpec() (info.ContainerSpec, error) {
// TODO: Since we dont collect disk usage stats for containerd, we set hasFilesystem
// to false. Revisit when we support disk usage stats for containerd
hasFilesystem := false
hasFilesystem := true
hasNet := h.includedMetrics.Has(container.NetworkUsageMetrics)
spec, err := common.GetSpec(h.cgroupPaths, h.machineInfoFactory, hasNet, hasFilesystem)
spec.Labels = h.labels
Expand All @@ -192,6 +197,23 @@ func (h *containerdContainerHandler) getFsStats(stats *info.ContainerStats) erro
if h.includedMetrics.Has(container.DiskIOMetrics) {
common.AssignDeviceNamesToDiskStats((*common.MachineInfoNamer)(mi), &stats.DiskIo)
}

if h.includedMetrics.Has(container.DiskUsageMetrics) && ctrdSnapshotsClient != nil {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
usage, err := ctrdSnapshotsClient.Usage(ctx, &snapshotsapi.UsageRequest{
Snapshotter: h.Snapshotter,
Key: h.SnapshotKey,
})
if err == nil {
stats.Filesystem = append(stats.Filesystem, info.FsStats{
BaseUsage: uint64(usage.Size),
Usage: uint64(usage.Size),
Inodes: uint64(usage.Inodes),
})
}
}

return nil
}

Expand Down