-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
1195 lines (1049 loc) · 33.3 KB
/
main.go
File metadata and controls
1195 lines (1049 loc) · 33.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// cased-agent: Kubernetes metrics collector for Cased observability
//
// Runs as a DaemonSet, collects container/pod metrics from cgroups and /proc,
// enriches with Kubernetes metadata, and sends to Cased API.
//
// Similar to Groundcover's approach but Phase 1 (no eBPF yet):
// - Resource metrics: CPU, memory, network, disk I/O
// - Kubernetes metadata: pod, namespace, node, labels
// - Golden signals computed server-side from these metrics
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// Config holds agent configuration
type Config struct {
APIEndpoint string
APIKey string
ClusterID string
NodeName string
CollectInterval time.Duration
BatchSize int
ProcPath string // Path to /proc (or /host/proc in container)
CgroupPath string // Path to cgroups
EnableEBPF bool // Enable eBPF HTTP tracing
EnableOTel bool // Enable OpenTelemetry receiver
OTelPort int // Port for OTLP HTTP receiver
}
// Metric represents a single metric data point
type Metric struct {
Timestamp int64 `json:"timestamp"`
MetricName string `json:"metric_name"`
Value float64 `json:"value"`
Unit string `json:"unit"`
Tags map[string]string `json:"tags"`
ClusterID string `json:"cluster_id"`
NodeName string `json:"node_name"`
Namespace string `json:"namespace,omitempty"`
PodName string `json:"pod_name,omitempty"`
ContainerName string `json:"container_name,omitempty"`
PodUID string `json:"pod_uid,omitempty"`
}
// MetricsBatch is sent to the API
type MetricsBatch struct {
Metrics []Metric `json:"metrics"`
ClusterID string `json:"cluster_id"`
}
// ContainerStats holds raw stats for a container
type ContainerStats struct {
Namespace string
PodName string
PodUID string
ContainerName string
ContainerID string
Labels map[string]string
// CPU stats (from cpu.stat or cpuacct)
CPUUsageNanos uint64
CPUUserNanos uint64
CPUSystemNanos uint64
CPUThrottledNanos uint64
CPUPeriods uint64
CPUThrottled uint64
// Memory stats (from memory.stat or memory.current)
MemoryUsageBytes uint64
MemoryLimitBytes uint64
MemoryRSSBytes uint64
MemoryCacheBytes uint64
MemorySwapBytes uint64
// Network stats (from /proc/net/dev in container namespace)
NetworkRxBytes uint64
NetworkTxBytes uint64
NetworkRxPackets uint64
NetworkTxPackets uint64
NetworkRxErrors uint64
NetworkTxErrors uint64
// Disk I/O stats (from io.stat or blkio)
DiskReadBytes uint64
DiskWriteBytes uint64
DiskReadOps uint64
DiskWriteOps uint64
}
// Agent collects and sends metrics
type Agent struct {
config *Config
k8sClient *kubernetes.Clientset
httpClient *http.Client
// Previous stats for rate calculations
prevStats map[string]*ContainerStats
prevStatsTime time.Time
// Additional collectors
ebpfCollector *EBPFCollector
k8sEvents *K8sEventCollector
otelReceiver *OTelReceiver
// Health status
ready bool
}
func main() {
// Parse flags
endpoint := flag.String("endpoint", getEnv("CASED_API_ENDPOINT", "https://api.cased.com"), "Cased API endpoint")
apiKey := flag.String("api-key", getEnv("CASED_API_KEY", ""), "Cased API key")
clusterID := flag.String("cluster-id", getEnv("CASED_CLUSTER_ID", ""), "Cluster identifier")
nodeName := flag.String("node-name", getEnv("NODE_NAME", ""), "Node name (usually from downward API)")
interval := flag.Duration("interval", 15*time.Second, "Collection interval")
batchSize := flag.Int("batch-size", 100, "Max metrics per batch")
procPath := flag.String("proc-path", getEnv("PROC_PATH", "/proc"), "Path to /proc filesystem")
cgroupPath := flag.String("cgroup-path", getEnv("CGROUP_PATH", "/sys/fs/cgroup"), "Path to cgroup filesystem")
enableEBPF := flag.Bool("enable-ebpf", getEnv("ENABLE_EBPF", "false") == "true", "Enable eBPF HTTP tracing")
enableOTel := flag.Bool("enable-otel", getEnv("ENABLE_OTEL", "false") == "true", "Enable OpenTelemetry receiver")
otelPort := flag.Int("otel-port", 4318, "Port for OTLP HTTP receiver")
flag.Parse()
if *apiKey == "" {
fmt.Fprintln(os.Stderr, "Error: CASED_API_KEY or --api-key required")
os.Exit(1)
}
if *clusterID == "" {
fmt.Fprintln(os.Stderr, "Error: CASED_CLUSTER_ID or --cluster-id required")
os.Exit(1)
}
config := &Config{
APIEndpoint: *endpoint,
APIKey: *apiKey,
ClusterID: *clusterID,
NodeName: *nodeName,
CollectInterval: *interval,
BatchSize: *batchSize,
ProcPath: *procPath,
CgroupPath: *cgroupPath,
EnableEBPF: *enableEBPF,
EnableOTel: *enableOTel,
OTelPort: *otelPort,
}
agent, err := NewAgent(config)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating agent: %v\n", err)
os.Exit(1)
}
fmt.Printf("cased-agent starting: cluster=%s node=%s interval=%s\n",
config.ClusterID, config.NodeName, config.CollectInterval)
agent.Run(context.Background())
}
// NewAgent creates a new metrics agent
func NewAgent(config *Config) (*Agent, error) {
// Create in-cluster Kubernetes client
k8sConfig, err := rest.InClusterConfig()
if err != nil {
// Fall back to local development mode
fmt.Printf("Warning: not running in cluster, k8s metadata disabled: %v\n", err)
}
var k8sClient *kubernetes.Clientset
if k8sConfig != nil {
k8sClient, err = kubernetes.NewForConfig(k8sConfig)
if err != nil {
return nil, fmt.Errorf("creating k8s client: %w", err)
}
}
agent := &Agent{
config: config,
k8sClient: k8sClient,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
prevStats: make(map[string]*ContainerStats),
}
// Initialize K8s event collector
if k8sClient != nil {
agent.k8sEvents = NewK8sEventCollector(k8sClient, config.NodeName, config.ClusterID)
}
// Initialize eBPF collector (optional)
if config.EnableEBPF {
ebpf, err := NewEBPFCollector()
if err != nil {
fmt.Printf("Warning: eBPF initialization failed (non-fatal): %v\n", err)
} else {
agent.ebpfCollector = ebpf
fmt.Println("eBPF HTTP tracing enabled")
}
}
// Initialize OpenTelemetry receiver (optional)
if config.EnableOTel {
agent.otelReceiver = NewOTelReceiver(config.OTelPort, config.ClusterID, config.NodeName)
fmt.Printf("OpenTelemetry receiver will listen on port %d\n", config.OTelPort)
}
return agent, nil
}
// startHealthServer starts the HTTP health check server
func (a *Agent) startHealthServer() {
mux := http.NewServeMux()
// Liveness probe - always healthy if process is running
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
// Readiness probe - healthy once we've successfully collected metrics
mux.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
if a.ready {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
} else {
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = w.Write([]byte("not ready"))
}
})
server := &http.Server{
Addr: ":8080",
Handler: mux,
}
go func() {
fmt.Println("Health server listening on :8080")
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Fprintf(os.Stderr, "Health server error: %v\n", err)
}
}()
}
// Run starts the collection loop
func (a *Agent) Run(ctx context.Context) {
// Start health server first
a.startHealthServer()
ticker := time.NewTicker(a.config.CollectInterval)
defer ticker.Stop()
// Start additional collectors
if a.k8sEvents != nil {
a.k8sEvents.Start(ctx)
}
if a.ebpfCollector != nil {
a.ebpfCollector.Start()
}
if a.otelReceiver != nil {
if err := a.otelReceiver.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Warning: OTel receiver failed to start: %v\n", err)
}
}
// Collect immediately on start
a.collectAndSend(ctx)
// Mark as ready after first successful collection
a.ready = true
for {
select {
case <-ctx.Done():
fmt.Println("Agent shutting down")
a.shutdown()
return
case <-ticker.C:
a.collectAndSend(ctx)
}
}
}
func (a *Agent) shutdown() {
if a.k8sEvents != nil {
a.k8sEvents.Stop()
}
if a.ebpfCollector != nil {
a.ebpfCollector.Stop()
}
if a.otelReceiver != nil {
a.otelReceiver.Stop()
}
}
func (a *Agent) collectAndSend(ctx context.Context) {
start := time.Now()
metrics, err := a.collectMetrics(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error collecting metrics: %v\n", err)
return
}
if len(metrics) == 0 {
fmt.Println("No metrics collected")
return
}
// Send in batches
for i := 0; i < len(metrics); i += a.config.BatchSize {
end := i + a.config.BatchSize
if end > len(metrics) {
end = len(metrics)
}
batch := MetricsBatch{
Metrics: metrics[i:end],
ClusterID: a.config.ClusterID,
}
if err := a.sendBatch(ctx, &batch); err != nil {
fmt.Fprintf(os.Stderr, "Error sending batch: %v\n", err)
}
}
// Send OTel spans if receiver is enabled
if a.otelReceiver != nil {
spans := a.otelReceiver.CollectSpans()
if len(spans) > 0 {
if err := a.sendSpans(ctx, spans); err != nil {
fmt.Fprintf(os.Stderr, "Error sending spans: %v\n", err)
} else {
fmt.Printf("Sent %d OTel spans\n", len(spans))
}
}
}
fmt.Printf("Collected and sent %d metrics in %s\n", len(metrics), time.Since(start))
}
func (a *Agent) collectMetrics(ctx context.Context) ([]Metric, error) {
var metrics []Metric
now := time.Now()
timestamp := now.UnixMilli()
// Collect node-level metrics
nodeMetrics, err := a.collectNodeMetrics(timestamp)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: node metrics collection failed: %v\n", err)
} else {
metrics = append(metrics, nodeMetrics...)
}
// Collect container metrics
containerStats, err := a.collectContainerStats()
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: container metrics collection failed: %v\n", err)
} else {
// Enrich with K8s metadata (pod name, namespace, labels)
a.enrichWithK8sMetadata(ctx, containerStats)
containerMetrics := a.statsToMetrics(containerStats, timestamp, now)
metrics = append(metrics, containerMetrics...)
}
// Collect K8s events as metrics
if a.k8sEvents != nil {
eventMetrics := a.k8sEvents.CollectMetrics(timestamp)
metrics = append(metrics, eventMetrics...)
}
// Collect eBPF HTTP metrics
if a.ebpfCollector != nil && a.ebpfCollector.IsEnabled() {
httpMetrics := a.ebpfCollector.CollectMetrics(timestamp, a.config.ClusterID, a.config.NodeName)
metrics = append(metrics, httpMetrics...)
}
// Collect OpenTelemetry trace metrics
if a.otelReceiver != nil {
traceMetrics := a.otelReceiver.CollectMetrics(timestamp)
metrics = append(metrics, traceMetrics...)
}
return metrics, nil
}
// collectNodeMetrics collects node-level CPU, memory, network stats
func (a *Agent) collectNodeMetrics(timestamp int64) ([]Metric, error) {
var metrics []Metric
tags := map[string]string{
"node": a.config.NodeName,
}
// CPU from /proc/stat
cpuMetrics, err := a.readNodeCPU()
if err == nil {
for name, value := range cpuMetrics {
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "node.cpu." + name,
Value: value,
Unit: "percent",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
})
}
}
// Memory from /proc/meminfo
memMetrics, err := a.readNodeMemory()
if err == nil {
for name, value := range memMetrics {
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "node.memory." + name,
Value: value,
Unit: "bytes",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
})
}
}
// Network from /proc/net/dev
netMetrics, err := a.readNodeNetwork()
if err == nil {
for name, value := range netMetrics {
unit := "bytes"
if strings.Contains(name, "packets") || strings.Contains(name, "errors") {
unit = "count"
}
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "node.network." + name,
Value: value,
Unit: unit,
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
})
}
}
return metrics, nil
}
func (a *Agent) readNodeCPU() (map[string]float64, error) {
data, err := os.ReadFile(filepath.Join(a.config.ProcPath, "stat"))
if err != nil {
return nil, err
}
// Parse first line: cpu user nice system idle iowait irq softirq steal guest guest_nice
lines := strings.Split(string(data), "\n")
if len(lines) == 0 {
return nil, fmt.Errorf("empty /proc/stat")
}
fields := strings.Fields(lines[0])
if len(fields) < 5 || fields[0] != "cpu" {
return nil, fmt.Errorf("unexpected /proc/stat format")
}
user, _ := strconv.ParseFloat(fields[1], 64)
nice, _ := strconv.ParseFloat(fields[2], 64)
system, _ := strconv.ParseFloat(fields[3], 64)
idle, _ := strconv.ParseFloat(fields[4], 64)
iowait := 0.0
if len(fields) > 5 {
iowait, _ = strconv.ParseFloat(fields[5], 64)
}
total := user + nice + system + idle + iowait
return map[string]float64{
"user_percent": (user / total) * 100,
"system_percent": (system / total) * 100,
"idle_percent": (idle / total) * 100,
"iowait_percent": (iowait / total) * 100,
}, nil
}
func (a *Agent) readNodeMemory() (map[string]float64, error) {
data, err := os.ReadFile(filepath.Join(a.config.ProcPath, "meminfo"))
if err != nil {
return nil, err
}
result := make(map[string]float64)
for _, line := range strings.Split(string(data), "\n") {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
key := strings.TrimSuffix(fields[0], ":")
value, _ := strconv.ParseFloat(fields[1], 64)
// Convert kB to bytes
if len(fields) >= 3 && fields[2] == "kB" {
value *= 1024
}
switch key {
case "MemTotal":
result["total"] = value
case "MemFree":
result["free"] = value
case "MemAvailable":
result["available"] = value
case "Buffers":
result["buffers"] = value
case "Cached":
result["cached"] = value
case "SwapTotal":
result["swap_total"] = value
case "SwapFree":
result["swap_free"] = value
}
}
// Calculate used
if total, ok := result["total"]; ok {
if avail, ok := result["available"]; ok {
result["used"] = total - avail
result["used_percent"] = ((total - avail) / total) * 100
}
}
return result, nil
}
func (a *Agent) readNodeNetwork() (map[string]float64, error) {
data, err := os.ReadFile(filepath.Join(a.config.ProcPath, "net/dev"))
if err != nil {
return nil, err
}
result := map[string]float64{
"rx_bytes": 0,
"tx_bytes": 0,
"rx_packets": 0,
"tx_packets": 0,
"rx_errors": 0,
"tx_errors": 0,
}
lines := strings.Split(string(data), "\n")
for _, line := range lines[2:] { // Skip header lines
fields := strings.Fields(line)
if len(fields) < 17 {
continue
}
iface := strings.TrimSuffix(fields[0], ":")
// Skip loopback and virtual interfaces
if iface == "lo" || strings.HasPrefix(iface, "veth") || strings.HasPrefix(iface, "docker") {
continue
}
rxBytes, _ := strconv.ParseFloat(fields[1], 64)
rxPackets, _ := strconv.ParseFloat(fields[2], 64)
rxErrors, _ := strconv.ParseFloat(fields[3], 64)
txBytes, _ := strconv.ParseFloat(fields[9], 64)
txPackets, _ := strconv.ParseFloat(fields[10], 64)
txErrors, _ := strconv.ParseFloat(fields[11], 64)
result["rx_bytes"] += rxBytes
result["tx_bytes"] += txBytes
result["rx_packets"] += rxPackets
result["tx_packets"] += txPackets
result["rx_errors"] += rxErrors
result["tx_errors"] += txErrors
}
return result, nil
}
// collectContainerStats reads cgroup stats for containers
func (a *Agent) collectContainerStats() ([]*ContainerStats, error) {
cgroupPath := a.config.CgroupPath
// Try cgroup v2 first
if _, err := os.Stat(filepath.Join(cgroupPath, "cgroup.controllers")); err == nil {
return a.collectCgroupV2Stats(cgroupPath)
}
// Fall back to cgroup v1
return a.collectCgroupV1Stats(cgroupPath)
}
func (a *Agent) collectCgroupV2Stats(basePath string) ([]*ContainerStats, error) {
var stats []*ContainerStats
// Paths to search for containers (kubernetes and docker compose)
searchPaths := []string{
filepath.Join(basePath, "kubepods.slice"),
filepath.Join(basePath, "kubepods"),
filepath.Join(basePath, "docker"), // Docker containers
filepath.Join(basePath, "system.slice"), // systemd services
}
for _, searchPath := range searchPaths {
if _, err := os.Stat(searchPath); err != nil {
continue
}
err := filepath.Walk(searchPath, func(path string, info os.FileInfo, err error) error {
if err != nil || !info.IsDir() {
return nil
}
// Look for container directories
// Kubernetes: cri-containerd-*, docker-*
// Docker Compose: docker/<container-id>
basename := filepath.Base(path)
isContainer := strings.HasPrefix(basename, "cri-containerd-") ||
strings.HasPrefix(basename, "docker-") ||
(strings.Contains(path, "/docker/") && len(basename) == 64) // Docker container ID
if !isContainer {
return nil
}
stat, err := a.readCgroupV2Stats(path)
if err != nil {
return nil
}
// Extract pod/container info from path
a.enrichFromPath(stat, path)
stats = append(stats, stat)
return nil
})
if err != nil {
continue
}
}
return stats, nil
}
func (a *Agent) readCgroupV2Stats(cgroupPath string) (*ContainerStats, error) {
stat := &ContainerStats{}
// CPU stats from cpu.stat
if data, err := os.ReadFile(filepath.Join(cgroupPath, "cpu.stat")); err == nil {
for _, line := range strings.Split(string(data), "\n") {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
value, _ := strconv.ParseUint(fields[1], 10, 64)
switch fields[0] {
case "usage_usec":
stat.CPUUsageNanos = value * 1000
case "user_usec":
stat.CPUUserNanos = value * 1000
case "system_usec":
stat.CPUSystemNanos = value * 1000
case "throttled_usec":
stat.CPUThrottledNanos = value * 1000
case "nr_periods":
stat.CPUPeriods = value
case "nr_throttled":
stat.CPUThrottled = value
}
}
}
// Memory from memory.current and memory.stat
if data, err := os.ReadFile(filepath.Join(cgroupPath, "memory.current")); err == nil {
stat.MemoryUsageBytes, _ = strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
}
if data, err := os.ReadFile(filepath.Join(cgroupPath, "memory.max")); err == nil {
s := strings.TrimSpace(string(data))
if s != "max" {
stat.MemoryLimitBytes, _ = strconv.ParseUint(s, 10, 64)
}
}
if data, err := os.ReadFile(filepath.Join(cgroupPath, "memory.stat")); err == nil {
for _, line := range strings.Split(string(data), "\n") {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
value, _ := strconv.ParseUint(fields[1], 10, 64)
switch fields[0] {
case "anon":
stat.MemoryRSSBytes = value
case "file":
stat.MemoryCacheBytes = value
case "swap":
stat.MemorySwapBytes = value
}
}
}
// I/O from io.stat
if data, err := os.ReadFile(filepath.Join(cgroupPath, "io.stat")); err == nil {
for _, line := range strings.Split(string(data), "\n") {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
for _, field := range fields[1:] {
parts := strings.Split(field, "=")
if len(parts) != 2 {
continue
}
value, _ := strconv.ParseUint(parts[1], 10, 64)
switch parts[0] {
case "rbytes":
stat.DiskReadBytes += value
case "wbytes":
stat.DiskWriteBytes += value
case "rios":
stat.DiskReadOps += value
case "wios":
stat.DiskWriteOps += value
}
}
}
}
return stat, nil
}
func (a *Agent) collectCgroupV1Stats(basePath string) ([]*ContainerStats, error) {
var stats []*ContainerStats
cpuPath := filepath.Join(basePath, "cpu", "kubepods")
_ = filepath.Join(basePath, "memory", "kubepods") // Used for memory path substitution
if _, err := os.Stat(cpuPath); err != nil {
return stats, nil
}
err := filepath.Walk(cpuPath, func(path string, info os.FileInfo, err error) error {
if err != nil || !info.IsDir() {
return nil
}
// Look for container directories
if !strings.Contains(path, "docker") && !strings.Contains(path, "cri-containerd") {
return nil
}
stat := &ContainerStats{}
a.enrichFromPath(stat, path)
// CPU stats
if data, err := os.ReadFile(filepath.Join(path, "cpuacct.usage")); err == nil {
stat.CPUUsageNanos, _ = strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
}
// Memory stats - find corresponding memory cgroup
memContainerPath := strings.Replace(path, "/cpu/", "/memory/", 1)
if data, err := os.ReadFile(filepath.Join(memContainerPath, "memory.usage_in_bytes")); err == nil {
stat.MemoryUsageBytes, _ = strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
}
if data, err := os.ReadFile(filepath.Join(memContainerPath, "memory.limit_in_bytes")); err == nil {
stat.MemoryLimitBytes, _ = strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
}
stats = append(stats, stat)
return nil
})
return stats, err
}
func (a *Agent) enrichFromPath(stat *ContainerStats, path string) {
// Extract pod UID and container ID from cgroup path
// Typical paths:
// cgroup v2: /sys/fs/cgroup/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod<uid>.slice/cri-containerd-<containerid>.scope
// cgroup v1: /sys/fs/cgroup/cpu/kubepods/burstable/pod<uid>/<containerid>
parts := strings.Split(path, "/")
for _, part := range parts {
if strings.HasPrefix(part, "pod") || strings.Contains(part, "-pod") {
// Extract pod UID
uid := part
uid = strings.TrimPrefix(uid, "pod")
uid = strings.TrimPrefix(uid, "kubepods-burstable-pod")
uid = strings.TrimPrefix(uid, "kubepods-besteffort-pod")
uid = strings.TrimPrefix(uid, "kubepods-guaranteed-pod")
uid = strings.TrimSuffix(uid, ".slice")
// Convert underscores back to dashes (cgroup escaping)
uid = strings.ReplaceAll(uid, "_", "-")
stat.PodUID = uid
}
if strings.HasPrefix(part, "cri-containerd-") || strings.HasPrefix(part, "docker-") {
id := part
id = strings.TrimPrefix(id, "cri-containerd-")
id = strings.TrimPrefix(id, "docker-")
id = strings.TrimSuffix(id, ".scope")
stat.ContainerID = id
}
}
}
func (a *Agent) statsToMetrics(stats []*ContainerStats, timestamp int64, now time.Time) []Metric {
var metrics []Metric
interval := now.Sub(a.prevStatsTime).Seconds()
if interval <= 0 {
interval = 1
}
// Build set of current container keys for cleanup
currentKeys := make(map[string]bool)
for _, stat := range stats {
key := stat.PodUID + "/" + stat.ContainerID
currentKeys[key] = true
tags := map[string]string{
"node": a.config.NodeName,
"pod_uid": stat.PodUID,
"container": stat.ContainerName,
"namespace": stat.Namespace,
"pod": stat.PodName,
}
// Memory metrics (absolute values)
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "container.memory.usage",
Value: float64(stat.MemoryUsageBytes),
Unit: "bytes",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
Namespace: stat.Namespace,
PodName: stat.PodName,
ContainerName: stat.ContainerName,
PodUID: stat.PodUID,
})
if stat.MemoryLimitBytes > 0 {
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "container.memory.limit",
Value: float64(stat.MemoryLimitBytes),
Unit: "bytes",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
Namespace: stat.Namespace,
PodName: stat.PodName,
ContainerName: stat.ContainerName,
PodUID: stat.PodUID,
})
usagePercent := float64(stat.MemoryUsageBytes) / float64(stat.MemoryLimitBytes) * 100
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "container.memory.usage_percent",
Value: usagePercent,
Unit: "percent",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
Namespace: stat.Namespace,
PodName: stat.PodName,
ContainerName: stat.ContainerName,
PodUID: stat.PodUID,
})
}
// Memory breakdown metrics (RSS vs cache)
if stat.MemoryRSSBytes > 0 {
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "container.memory.rss",
Value: float64(stat.MemoryRSSBytes),
Unit: "bytes",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
Namespace: stat.Namespace,
PodName: stat.PodName,
ContainerName: stat.ContainerName,
PodUID: stat.PodUID,
})
}
if stat.MemoryCacheBytes > 0 {
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "container.memory.cache",
Value: float64(stat.MemoryCacheBytes),
Unit: "bytes",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
Namespace: stat.Namespace,
PodName: stat.PodName,
ContainerName: stat.ContainerName,
PodUID: stat.PodUID,
})
}
if stat.MemorySwapBytes > 0 {
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "container.memory.swap",
Value: float64(stat.MemorySwapBytes),
Unit: "bytes",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
Namespace: stat.Namespace,
PodName: stat.PodName,
ContainerName: stat.ContainerName,
PodUID: stat.PodUID,
})
}
// CPU metrics (rate - need previous value)
if prev, ok := a.prevStats[key]; ok && interval > 0 {
// Only calculate if we have valid previous data (prevent huge deltas on first read)
if prev.CPUUsageNanos > 0 && stat.CPUUsageNanos > prev.CPUUsageNanos {
cpuDelta := float64(stat.CPUUsageNanos - prev.CPUUsageNanos)
cpuPercent := (cpuDelta / (interval * 1e9)) * 100 // nanoseconds to percent
// Sanity check: cap at 10000% (100 cores max)
if cpuPercent >= 0 && cpuPercent <= 10000 {
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "container.cpu.usage_percent",
Value: cpuPercent,
Unit: "percent",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
Namespace: stat.Namespace,
PodName: stat.PodName,
ContainerName: stat.ContainerName,
PodUID: stat.PodUID,
})
}
}
// CPU throttling metrics
if stat.CPUPeriods > prev.CPUPeriods {
throttledPeriods := stat.CPUThrottled - prev.CPUThrottled
totalPeriods := stat.CPUPeriods - prev.CPUPeriods
throttlePercent := float64(throttledPeriods) / float64(totalPeriods) * 100
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "container.cpu.throttle_percent",
Value: throttlePercent,
Unit: "percent",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
Namespace: stat.Namespace,
PodName: stat.PodName,
ContainerName: stat.ContainerName,
PodUID: stat.PodUID,
})
throttledTimeDelta := float64(stat.CPUThrottledNanos - prev.CPUThrottledNanos)
throttledTimeMs := throttledTimeDelta / 1e6 / interval // ms per second
metrics = append(metrics, Metric{
Timestamp: timestamp,
MetricName: "container.cpu.throttled_time",
Value: throttledTimeMs,
Unit: "ms/sec",
Tags: tags,
ClusterID: a.config.ClusterID,
NodeName: a.config.NodeName,
Namespace: stat.Namespace,
PodName: stat.PodName,
ContainerName: stat.ContainerName,
PodUID: stat.PodUID,
})
}
// Network rates
if stat.NetworkRxBytes > prev.NetworkRxBytes {