...
1
16
17 package collectors
18
19 import (
20 "context"
21
22 "k8s.io/component-base/metrics"
23 "k8s.io/klog/v2"
24 statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
25 )
26
27 var (
28 descLogSize = metrics.NewDesc(
29 "kubelet_container_log_filesystem_used_bytes",
30 "Bytes used by the container's logs on the filesystem.",
31 []string{
32 "uid",
33 "namespace",
34 "pod",
35 "container",
36 }, nil,
37 metrics.ALPHA,
38 "",
39 )
40 )
41
42 type logMetricsCollector struct {
43 metrics.BaseStableCollector
44
45 podStats func(ctx context.Context) ([]statsapi.PodStats, error)
46 }
47
48
49 var _ metrics.StableCollector = &logMetricsCollector{}
50
51
52
53 func NewLogMetricsCollector(podStats func(ctx context.Context) ([]statsapi.PodStats, error)) metrics.StableCollector {
54 return &logMetricsCollector{
55 podStats: podStats,
56 }
57 }
58
59
60 func (c *logMetricsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
61 ch <- descLogSize
62 }
63
64
65 func (c *logMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
66 podStats, err := c.podStats(context.Background())
67 if err != nil {
68 klog.ErrorS(err, "Failed to get pod stats")
69 return
70 }
71
72 for _, ps := range podStats {
73 for _, c := range ps.Containers {
74 if c.Logs != nil && c.Logs.UsedBytes != nil {
75 ch <- metrics.NewLazyConstMetric(
76 descLogSize,
77 metrics.GaugeValue,
78 float64(*c.Logs.UsedBytes),
79 ps.PodRef.UID,
80 ps.PodRef.Namespace,
81 ps.PodRef.Name,
82 c.Name,
83 )
84 }
85 }
86 }
87 }
88
View as plain text