hanahmily commented on code in PR #919: URL: https://github.com/apache/skywalking-banyandb/pull/919#discussion_r2694431036
########## fodc/agent/internal/ktm/iomonitor/metrics/metrics.go: ########## Review Comment: Could you please write the metrics directly into "FlightRecorder"? You don't need to create ktm's Metric system. ########## fodc/agent/internal/ktm/iomonitor/collector.go: ########## @@ -0,0 +1,209 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Package iomonitor implements I/O monitoring using eBPF. +package iomonitor + +import ( + "context" + "fmt" + "sync" + "time" + + "github.com/rs/zerolog" + + "github.com/apache/skywalking-banyandb/fodc/agent/internal/ktm/iomonitor/metrics" +) + +// CollectorConfig defines the collector configuration. +// Field order chosen to minimize padding (large -> small). +type CollectorConfig struct { + EBPF EBPFConfig `mapstructure:"ebpf"` + Modules []string `mapstructure:"modules"` + Interval time.Duration `mapstructure:"interval"` +} + +// EBPFConfig defines eBPF-specific configuration. +type EBPFConfig struct { + CgroupPath string `mapstructure:"cgroup_path"` // Optional cgroup v2 path to filter PIDs +} + +// Collector manages eBPF program lifecycle and metrics collection. +type Collector struct { + logger zerolog.Logger + modules map[string]Module + metrics *metrics.Store + ticker *time.Ticker + stopChan chan struct{} + config CollectorConfig + wg sync.WaitGroup + mu sync.RWMutex +} + +// Module represents an eBPF monitoring module. +type Module interface { + Name() string + Start() error + Stop() error + Collect() (*metrics.MetricSet, error) +} + +// New creates a new collector instance. +func New(cfg CollectorConfig, log zerolog.Logger) (*Collector, error) { + if cfg.Interval <= 0 { + return nil, fmt.Errorf("collector interval must be positive, got %v", cfg.Interval) + } + + c := &Collector{ + config: cfg, + logger: log, + modules: make(map[string]Module), + metrics: metrics.NewStore(), + stopChan: make(chan struct{}), + } + + // Initialize modules based on configuration + for _, moduleName := range cfg.Modules { + module, err := c.createModule(moduleName, cfg.EBPF) + if err != nil { + return nil, fmt.Errorf("failed to create module %s: %w", moduleName, err) + } + c.modules[moduleName] = module + } + + return c, nil +} + +// createModule creates a module instance by name. +func (c *Collector) createModule(name string, ebpfCfg EBPFConfig) (Module, error) { + switch name { + case "iomonitor": + // Create the comprehensive I/O monitor module + module, err := newModule(c.logger, ebpfCfg) + if err != nil { + return nil, fmt.Errorf("failed to create iomonitor module: %w", err) + } + return module, nil + case "fadvise", "memory", "cache": + // These are all handled by iomonitor now + c.logger.Warn().Str("module", name).Msg("Module is deprecated, use 'iomonitor' instead") + return nil, fmt.Errorf("module %s is deprecated, use 'iomonitor' instead", name) Review Comment: ```suggestion ``` ########## fodc/agent/internal/ktm/iomonitor/collector.go: ########## @@ -0,0 +1,209 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Package iomonitor implements I/O monitoring using eBPF. +package iomonitor + +import ( + "context" + "fmt" + "sync" + "time" + + "github.com/rs/zerolog" + + "github.com/apache/skywalking-banyandb/fodc/agent/internal/ktm/iomonitor/metrics" +) + +// CollectorConfig defines the collector configuration. +// Field order chosen to minimize padding (large -> small). +type CollectorConfig struct { + EBPF EBPFConfig `mapstructure:"ebpf"` + Modules []string `mapstructure:"modules"` + Interval time.Duration `mapstructure:"interval"` +} + +// EBPFConfig defines eBPF-specific configuration. +type EBPFConfig struct { + CgroupPath string `mapstructure:"cgroup_path"` // Optional cgroup v2 path to filter PIDs +} + +// Collector manages eBPF program lifecycle and metrics collection. +type Collector struct { + logger zerolog.Logger + modules map[string]Module + metrics *metrics.Store + ticker *time.Ticker + stopChan chan struct{} + config CollectorConfig + wg sync.WaitGroup + mu sync.RWMutex +} + +// Module represents an eBPF monitoring module. +type Module interface { + Name() string + Start() error + Stop() error + Collect() (*metrics.MetricSet, error) +} + +// New creates a new collector instance. +func New(cfg CollectorConfig, log zerolog.Logger) (*Collector, error) { + if cfg.Interval <= 0 { + return nil, fmt.Errorf("collector interval must be positive, got %v", cfg.Interval) + } + + c := &Collector{ + config: cfg, + logger: log, + modules: make(map[string]Module), + metrics: metrics.NewStore(), + stopChan: make(chan struct{}), + } + + // Initialize modules based on configuration + for _, moduleName := range cfg.Modules { + module, err := c.createModule(moduleName, cfg.EBPF) + if err != nil { + return nil, fmt.Errorf("failed to create module %s: %w", moduleName, err) + } + c.modules[moduleName] = module + } + + return c, nil +} + +// createModule creates a module instance by name. +func (c *Collector) createModule(name string, ebpfCfg EBPFConfig) (Module, error) { + switch name { + case "iomonitor": + // Create the comprehensive I/O monitor module + module, err := newModule(c.logger, ebpfCfg) Review Comment: create a mock "newModule" function for other arch/os in a new file "module_other.go". Rename current "module.go" to "module_linux.go" ########## fodc/agent/internal/cmd/root.go: ########## @@ -102,6 +108,129 @@ func init() { "Interval for sending heartbeats to Proxy. Note: The Proxy may override this value in RegisterAgentResponse.") rootCmd.Flags().DurationVar(&reconnectInterval, "reconnect-interval", defaultReconnectInterval, "Interval for reconnection attempts when connection to Proxy is lost") + rootCmd.Flags().BoolVar(&ktmEnabled, "ktm-enabled", false, "Enable Kernel Trace Module (eBPF)") + rootCmd.Flags().DurationVar(&ktmInterval, "ktm-interval", 10*time.Second, "Interval for KTM metrics collection") + rootCmd.Flags().StringSliceVar(&ktmModules, "ktm-modules", []string{"iomonitor"}, "KTM modules to enable") Review Comment: Would you remove the flag since the option is always "iomonitor"? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
