Skip to content
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

[OSCP]KusciaTask 系统资源指标采集、暴露及统一导出 #454

Open
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

renji555
Copy link

Fixedhttps://github.com//issues/400

@Candicepan Candicepan linked an issue Nov 13, 2024 that may be closed by this pull request
cmd/kuscia/modules/ktexporter.go Show resolved Hide resolved
cmd/kuscia/start/start.go Outdated Show resolved Hide resolved
cmd/kuscia/start/start.go Outdated Show resolved Hide resolved
cmd/kuscia/modules/ktexporter.go Show resolved Hide resolved
pkg/ktexporter/fetchmetrics/fetchmetrics.go Show resolved Hide resolved
pkg/ktexporter/fetchmetrics/fetchmetrics.go Outdated Show resolved Hide resolved
pkg/ktexporter/fetchmetrics/fetchmetrics.go Outdated Show resolved Hide resolved
// GetContainerStats fetches the container stats using crictl stats command
func GetContainerStats() (map[string]ContainerStats, error) {
// Execute the crictl stats command
cmd := exec.Command("crictl", "stats")
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里也建议直接调用 Containerd 的 pkg 去实现,不要使用 crictl stats 实现,这个可以搜一下大模型,有很多成熟的实现

Copy link
Collaborator

Choose a reason for hiding this comment

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

类似这样:

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/containerd/containerd"
	"github.com/containerd/containerd/cio"
	"github.com/containerd/containerd/namespaces"
	"github.com/containerd/typeurl"
	v1 "github.com/containerd/cgroups/stats/v1" // Import cgroups stats
)

func main() {
	// Create a new client connected to the default containerd socket
	client, err := containerd.New("/run/containerd/containerd.sock")
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// Define a context with the appropriate namespace
	ctx := namespaces.WithNamespace(context.Background(), "default")

	// Specify the container ID you want to monitor
	containerID := "your-container-id"

	// Fetch the container by its ID
	container, err := client.LoadContainer(ctx, containerID)
	if err != nil {
		log.Fatalf("failed to load container: %v", err)
	}

	// Get the task associated with the container
	task, err := container.Task(ctx, cio.Load)
	if err != nil {
		log.Fatalf("failed to load task for container: %v", err)
	}

	// Get the metrics for the task
	metrics, err := task.Metrics(ctx)
	if err != nil {
		log.Fatalf("failed to get metrics: %v", err)
	}

	// Parse the metrics into a cgroups metrics structure
	data, err := typeurl.UnmarshalAny(metrics.Data)
	if err != nil {
		log.Fatalf("failed to unmarshal metrics data: %v", err)
	}

	// Assert the data to the expected type
	stats := data.(*v1.Metrics)

	// Print the CPU and memory usage
	fmt.Printf("CPU Usage: %v\n", stats.CPU.Usage.Total)
	fmt.Printf("Memory Usage: %v\n", stats.Memory.Usage.Usage)
}

Copy link
Author

Choose a reason for hiding this comment

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

那task的相关任务指标是否可以通过crictl stats获得呢

Copy link
Author

Choose a reason for hiding this comment

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

这种方法只能获得CPU和内存使用,剩下的Iodes和disk该如何解决呢

@yushiqie yushiqie self-requested a review November 15, 2024 09:05
}

func GetKusciaTaskPID() (map[string]string, error) {
const containerdDir = "/home/kuscia/containerd/run/io.containerd.runtime.v2.task/k8s.io/"
Copy link
Collaborator

Choose a reason for hiding this comment

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

containerd 有metrics plugin,不建议裸写。
https://github.com/containerd/containerd/blob/main/docs/ops.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KusciaTask 系统资源指标采集、暴露及统一导出
3 participants