From 0ff6a6ec32562b1e16f6c12b2af5f6b1847e0266 Mon Sep 17 00:00:00 2001 From: Olivier G Date: Wed, 8 Oct 2025 15:35:57 +0200 Subject: [PATCH 1/3] Move collector.Config to its own package. Embeds config.Config into controller.Config --- cli_flags.go | 2 +- collector/{ => config}/config.go | 2 +- collector/factory.go | 30 +++++++------------------- internal/controller/config.go | 35 ++++++++----------------------- internal/controller/controller.go | 2 +- 5 files changed, 19 insertions(+), 52 deletions(-) rename collector/{ => config}/config.go (94%) diff --git a/cli_flags.go b/cli_flags.go index db958701e..13d1ed4ff 100644 --- a/cli_flags.go +++ b/cli_flags.go @@ -84,7 +84,7 @@ func parseArgs() (*controller.Config, error) { fs := flag.NewFlagSet("ebpf-profiler", flag.ExitOnError) // Please keep the parameters ordered alphabetically in the source-code. - fs.UintVar(&args.BpfVerifierLogLevel, "bpf-log-level", 0, bpfVerifierLogLevelHelp) + fs.UintVar(&args.BPFVerifierLogLevel, "bpf-log-level", 0, bpfVerifierLogLevelHelp) fs.StringVar(&args.CollAgentAddr, "collection-agent", "", collAgentAddrHelp) fs.BoolVar(&args.Copyright, "copyright", false, copyrightHelp) diff --git a/collector/config.go b/collector/config/config.go similarity index 94% rename from collector/config.go rename to collector/config/config.go index 1839c85c0..869d4aa22 100644 --- a/collector/config.go +++ b/collector/config/config.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package collector // import "go.opentelemetry.io/ebpf-profiler/collector" +package config // import "go.opentelemetry.io/ebpf-profiler/collector/config" import "time" diff --git a/collector/factory.go b/collector/factory.go index 064f8f86d..ee372ea6a 100644 --- a/collector/factory.go +++ b/collector/factory.go @@ -13,6 +13,7 @@ import ( "go.opentelemetry.io/collector/receiver" "go.opentelemetry.io/collector/receiver/xreceiver" + "go.opentelemetry.io/ebpf-profiler/collector/config" "go.opentelemetry.io/ebpf-profiler/collector/internal" "go.opentelemetry.io/ebpf-profiler/internal/controller" ) @@ -37,7 +38,7 @@ func BuildProfilesReceiver(options ...Option) xreceiver.CreateProfilesFunc { baseCfg component.Config, nextConsumer xconsumer.Profiles, ) (xreceiver.Profiles, error) { - cfg, ok := baseCfg.(*Config) + cfg, ok := baseCfg.(*config.Config) if !ok { return nil, errInvalidConfig } @@ -48,27 +49,10 @@ func BuildProfilesReceiver(options ...Option) xreceiver.CreateProfilesFunc { } controlerCfg := &controller.Config{ - ReporterInterval: cfg.ReporterInterval, - MonitorInterval: cfg.MonitorInterval, - SamplesPerSecond: cfg.SamplesPerSecond, - ProbabilisticInterval: cfg.ProbabilisticInterval, - ProbabilisticThreshold: cfg.ProbabilisticThreshold, - Tracers: cfg.Tracers, - ClockSyncInterval: cfg.ClockSyncInterval, - SendErrorFrames: cfg.SendErrorFrames, - VerboseMode: cfg.VerboseMode, - OffCPUThreshold: cfg.OffCPUThreshold, - IncludeEnvVars: cfg.IncludeEnvVars, - UProbeLinks: cfg.UProbeLinks, - LoadProbe: cfg.LoadProbe, - MapScaleFactor: cfg.MapScaleFactor, - BpfVerifierLogLevel: cfg.BPFVerifierLogLevel, - NoKernelVersionCheck: cfg.NoKernelVersionCheck, - MaxGRPCRetries: cfg.MaxGRPCRetries, - MaxRPCMsgSize: cfg.MaxRPCMsgSize, - ExecutableReporter: controllerOption.executableReporter, - ReporterFactory: controllerOption.reporterFactory, - OnShutdown: controllerOption.onShutdown, + Config: *cfg, + ExecutableReporter: controllerOption.executableReporter, + ReporterFactory: controllerOption.reporterFactory, + OnShutdown: controllerOption.onShutdown, } return internal.NewController(controlerCfg, rs, nextConsumer) @@ -76,7 +60,7 @@ func BuildProfilesReceiver(options ...Option) xreceiver.CreateProfilesFunc { } func defaultConfig() component.Config { - return &Config{ + return &config.Config{ ReporterInterval: 5 * time.Second, MonitorInterval: 5 * time.Second, SamplesPerSecond: 20, diff --git a/internal/controller/config.go b/internal/controller/config.go index 43cf31775..7ec8008a0 100644 --- a/internal/controller/config.go +++ b/internal/controller/config.go @@ -10,33 +10,18 @@ import ( log "github.com/sirupsen/logrus" "go.opentelemetry.io/collector/consumer/xconsumer" + "go.opentelemetry.io/ebpf-profiler/collector/config" "go.opentelemetry.io/ebpf-profiler/reporter" "go.opentelemetry.io/ebpf-profiler/tracer" ) type Config struct { - BpfVerifierLogLevel uint - CollAgentAddr string - Copyright bool - DisableTLS bool - MapScaleFactor uint - MonitorInterval time.Duration - ClockSyncInterval time.Duration - NoKernelVersionCheck bool - PprofAddr string - ProbabilisticInterval time.Duration - ProbabilisticThreshold uint - ReporterInterval time.Duration - SamplesPerSecond int - SendErrorFrames bool - Tracers string - VerboseMode bool - Version bool - OffCPUThreshold float64 - UProbeLinks []string - LoadProbe bool - MaxGRPCRetries uint32 - MaxRPCMsgSize int + config.Config + CollAgentAddr string + Copyright bool + DisableTLS bool + PprofAddr string + Version bool ExecutableReporter reporter.ExecutableReporter OnShutdown func() error @@ -47,8 +32,6 @@ type Config struct { Reporter reporter.Reporter Fs *flag.FlagSet - - IncludeEnvVars string } const ( @@ -79,8 +62,8 @@ func (cfg *Config) Validate() error { ) } - if cfg.BpfVerifierLogLevel > 2 { - return fmt.Errorf("invalid eBPF verifier log level: %d", cfg.BpfVerifierLogLevel) + if cfg.BPFVerifierLogLevel > 2 { + return fmt.Errorf("invalid eBPF verifier log level: %d", cfg.BPFVerifierLogLevel) } if cfg.ProbabilisticInterval < 1*time.Minute || cfg.ProbabilisticInterval > 5*time.Minute { diff --git a/internal/controller/controller.go b/internal/controller/controller.go index 91fe57717..538380054 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -82,7 +82,7 @@ func (c *Controller) Start(ctx context.Context) error { MapScaleFactor: int(c.config.MapScaleFactor), KernelVersionCheck: !c.config.NoKernelVersionCheck, VerboseMode: c.config.VerboseMode, - BPFVerifierLogLevel: uint32(c.config.BpfVerifierLogLevel), + BPFVerifierLogLevel: uint32(c.config.BPFVerifierLogLevel), ProbabilisticInterval: c.config.ProbabilisticInterval, ProbabilisticThreshold: c.config.ProbabilisticThreshold, OffCPUThreshold: uint32(c.config.OffCPUThreshold * float64(math.MaxUint32)), From b9ce34475989b7589c921d1b2903ad1d7d6ae2e3 Mon Sep 17 00:00:00 2001 From: Olivier G Date: Wed, 8 Oct 2025 15:57:03 +0200 Subject: [PATCH 2/3] Move controller.Config.Validate content to config.Config.Validate --- cli_flags.go | 3 +- collector/config/config.go | 88 ++++++++++++++++++++++++++++++++++- go.mod | 10 ++++ go.sum | 20 ++++++++ internal/controller/config.go | 76 +----------------------------- 5 files changed, 120 insertions(+), 77 deletions(-) diff --git a/cli_flags.go b/cli_flags.go index 13d1ed4ff..a16386bf5 100644 --- a/cli_flags.go +++ b/cli_flags.go @@ -11,6 +11,7 @@ import ( "github.com/peterbourgon/ff/v3" + "go.opentelemetry.io/ebpf-profiler/collector/config" "go.opentelemetry.io/ebpf-profiler/internal/controller" "go.opentelemetry.io/ebpf-profiler/tracer" ) @@ -42,7 +43,7 @@ var ( mapScaleFactorHelp = fmt.Sprintf("Scaling factor for eBPF map sizes. "+ "Every increase by 1 doubles the map size. Increase if you see eBPF map size errors. "+ "Default is %d corresponding to 4GB of executable address space, max is %d.", - defaultArgMapScaleFactor, controller.MaxArgMapScaleFactor) + defaultArgMapScaleFactor, config.MaxArgMapScaleFactor) disableTLSHelp = "Disable encryption for data in transit." bpfVerifierLogLevelHelp = "Log level of the eBPF verifier output (0,1,2). Default is 0." versionHelp = "Show version." diff --git a/collector/config/config.go b/collector/config/config.go index 869d4aa22..b62bc70de 100644 --- a/collector/config/config.go +++ b/collector/config/config.go @@ -3,7 +3,20 @@ package config // import "go.opentelemetry.io/ebpf-profiler/collector/config" -import "time" +import ( + "errors" + "fmt" + "runtime" + "time" + + "go.opentelemetry.io/collector/confmap/xconfmap" + "go.opentelemetry.io/ebpf-profiler/tracer" +) + +const ( + // 1TB of executable address space + MaxArgMapScaleFactor = 8 +) // Config is the configuration for the collector. type Config struct { @@ -26,3 +39,76 @@ type Config struct { MaxGRPCRetries uint32 `mapstructure:"max_grpc_retries"` MaxRPCMsgSize int `mapstructure:"max_rpc_msg_size"` } + +var _ xconfmap.Validator = (*Config)(nil) + +// Validate validates the config. +// This is automatically called by the config parser as it implements the xconfmap.Validator interface. +func (cfg *Config) Validate() error { + if cfg.SamplesPerSecond < 1 { + return fmt.Errorf("invalid sampling frequency: %d", cfg.SamplesPerSecond) + } + + if cfg.MapScaleFactor > 8 { + return fmt.Errorf( + "eBPF map scaling factor %d exceeds limit (max: %d)", + cfg.MapScaleFactor, MaxArgMapScaleFactor, + ) + } + + if cfg.BPFVerifierLogLevel > 2 { + return fmt.Errorf("invalid eBPF verifier log level: %d", cfg.BPFVerifierLogLevel) + } + + if cfg.ProbabilisticInterval < 1*time.Minute || cfg.ProbabilisticInterval > 5*time.Minute { + return errors.New( + "invalid argument for probabilistic-interval: use " + + "a duration between 1 and 5 minutes", + ) + } + + if cfg.ProbabilisticThreshold < 1 || + cfg.ProbabilisticThreshold > tracer.ProbabilisticThresholdMax { + return fmt.Errorf( + "invalid argument for probabilistic-threshold. Value "+ + "should be between 1 and %d", + tracer.ProbabilisticThresholdMax, + ) + } + + if cfg.OffCPUThreshold < 0.0 || cfg.OffCPUThreshold > 1.0 { + return errors.New( + "invalid argument for off-cpu-threshold. The value " + + "should be in the range [0..1]. 0 disables off-cpu profiling") + } + + if !cfg.NoKernelVersionCheck { + major, minor, patch, err := tracer.GetCurrentKernelVersion() + if err != nil { + return fmt.Errorf("failed to get kernel version: %v", err) + } + + var minMajor, minMinor uint32 + switch runtime.GOARCH { + case "amd64": + if cfg.VerboseMode { + minMajor, minMinor = 5, 2 + } else { + minMajor, minMinor = 4, 19 + } + case "arm64": + // Older ARM64 kernel versions have broken bpf_probe_read. + // https://github.com/torvalds/linux/commit/6ae08ae3dea2cfa03dd3665a3c8475c2d429ef47 + minMajor, minMinor = 5, 5 + default: + return fmt.Errorf("unsupported architecture: %s", runtime.GOARCH) + } + + if major < minMajor || (major == minMajor && minor < minMinor) { + return fmt.Errorf("host Agent requires kernel version "+ + "%d.%d or newer but got %d.%d.%d", minMajor, minMinor, major, minor, patch) + } + } + + return nil +} diff --git a/go.mod b/go.mod index 0e98bd93a..2e9519bfe 100644 --- a/go.mod +++ b/go.mod @@ -62,6 +62,8 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-viper/mapstructure/v2 v2.4.0 // indirect + github.com/gobwas/glob v0.2.3 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/google/go-cmp v0.7.0 // indirect github.com/hashicorp/go-version v1.7.0 // indirect @@ -69,13 +71,20 @@ require ( github.com/jsimonetti/rtnetlink/v2 v2.0.3 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.8 // indirect + github.com/knadh/koanf/maps v0.1.2 // indirect + github.com/knadh/koanf/providers/confmap v1.0.0 // indirect + github.com/knadh/koanf/v2 v2.3.0 // indirect github.com/mdlayher/netlink v1.7.2 // indirect github.com/mdlayher/socket v0.4.1 // indirect + github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect github.com/pmezard/go-difflib v1.0.0 // indirect go.opentelemetry.io/auto/sdk v1.1.0 // indirect go.opentelemetry.io/collector/component/componenttest v0.137.0 // indirect + go.opentelemetry.io/collector/confmap v1.43.0 // indirect + go.opentelemetry.io/collector/confmap/xconfmap v0.137.0 // indirect go.opentelemetry.io/collector/consumer v1.43.0 // indirect go.opentelemetry.io/collector/consumer/consumererror v0.137.0 // indirect go.opentelemetry.io/collector/featuregate v1.43.0 // indirect @@ -88,6 +97,7 @@ require ( go.opentelemetry.io/otel/trace v1.38.0 // indirect go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.27.0 // indirect + go.yaml.in/yaml/v3 v3.0.4 // indirect golang.org/x/net v0.42.0 // indirect golang.org/x/text v0.27.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20250804133106-a7a43d27e69b // indirect diff --git a/go.sum b/go.sum index e5872a090..ce78d5fee 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,10 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-quicktest/qt v1.101.1-0.20240301121107-c6c8733fa1e6 h1:teYtXy9B7y5lHTp8V9KPxpYRAVA7dozigQcMiBust1s= github.com/go-quicktest/qt v1.101.1-0.20240301121107-c6c8733fa1e6/go.mod h1:p4lGIVX+8Wa6ZPNDvqcxq36XpUDLh42FLetFU7odllI= +github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs= +github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= +github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= @@ -79,6 +83,12 @@ github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zt github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knadh/koanf/maps v0.1.2 h1:RBfmAW5CnZT+PJ1CVc1QSJKf4Xu9kxfQgYVQSu8hpbo= +github.com/knadh/koanf/maps v0.1.2/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI= +github.com/knadh/koanf/providers/confmap v1.0.0 h1:mHKLJTE7iXEys6deO5p6olAiZdG5zwp8Aebir+/EaRE= +github.com/knadh/koanf/providers/confmap v1.0.0/go.mod h1:txHYHiI2hAtF0/0sCmcuol4IDcuQbKTybiB1nOcUo1A= +github.com/knadh/koanf/v2 v2.3.0 h1:Qg076dDRFHvqnKG97ZEsi9TAg2/nFTa9hCdcSa1lvlM= +github.com/knadh/koanf/v2 v2.3.0/go.mod h1:gRb40VRAbd4iJMYYD5IxZ6hfuopFcXBpc9bbQpZwo28= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -94,6 +104,10 @@ github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA= github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8= +github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= +github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= +github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= +github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -125,6 +139,10 @@ go.opentelemetry.io/collector/component v1.43.0 h1:9dyOmV0UuIhrNSASMeDH125jhfv7+ go.opentelemetry.io/collector/component v1.43.0/go.mod h1:Pw3qM5HhgnSMpebNRUiiJuEiXxZyHq83vl7wXqxD8hU= go.opentelemetry.io/collector/component/componenttest v0.137.0 h1:QC9MZsYyzQqN9qMlleJb78wf7FeCjbr4jLeCuNlKHLU= go.opentelemetry.io/collector/component/componenttest v0.137.0/go.mod h1:JuiX9pv7qE5G8keihhjM66LeidryEnziPND0sXuK9PQ= +go.opentelemetry.io/collector/confmap v1.43.0 h1:QVAnbS7A+2Ra61xsuG355vhlW6uOMaKWysrwLQzDUz4= +go.opentelemetry.io/collector/confmap v1.43.0/go.mod h1:N5GZpFCmwD1GynDu3IWaZW5Ycfc/7YxSU0q1/E3vLdg= +go.opentelemetry.io/collector/confmap/xconfmap v0.137.0 h1:IKzD6w4YuvBi6GvxZfhz7SJR6GR1UpSQRuxtx20/+9U= +go.opentelemetry.io/collector/confmap/xconfmap v0.137.0/go.mod h1:psXdQr13pVrCqNPdoER2QZZorvONAR5ZUEHURe4POh4= go.opentelemetry.io/collector/consumer v1.43.0 h1:51pfN5h6PLlaBwGPtyHn6BdK0DgtVGRV0UYRPbbscbs= go.opentelemetry.io/collector/consumer v1.43.0/go.mod h1:v3J2g+6IwOPbLsnzL9cQfvgpmmsZt1YS7aXSNDFmJfk= go.opentelemetry.io/collector/consumer/consumererror v0.137.0 h1:4HgYX6vVmaF17RRRtJDpR8EuWmLAv6JdKYG8slDDa+g= @@ -179,6 +197,8 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= golang.org/x/arch v0.21.0 h1:iTC9o7+wP6cPWpDWkivCvQFGAHDQ59SrSxsLPcnkArw= golang.org/x/arch v0.21.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/internal/controller/config.go b/internal/controller/config.go index 7ec8008a0..13abfb275 100644 --- a/internal/controller/config.go +++ b/internal/controller/config.go @@ -1,18 +1,14 @@ package controller // import "go.opentelemetry.io/ebpf-profiler/internal/controller" import ( - "errors" "flag" "fmt" - "runtime" - "time" log "github.com/sirupsen/logrus" "go.opentelemetry.io/collector/consumer/xconsumer" "go.opentelemetry.io/ebpf-profiler/collector/config" "go.opentelemetry.io/ebpf-profiler/reporter" - "go.opentelemetry.io/ebpf-profiler/tracer" ) type Config struct { @@ -34,11 +30,6 @@ type Config struct { Fs *flag.FlagSet } -const ( - // 1TB of executable address space - MaxArgMapScaleFactor = 8 -) - // Dump visits all flag sets, and dumps them all to debug // Used for verbose mode logging. func (cfg *Config) Dump() { @@ -51,70 +42,5 @@ func (cfg *Config) Dump() { // Validate runs validations on the provided configuration, and returns errors // if invalid values were provided. func (cfg *Config) Validate() error { - if cfg.SamplesPerSecond < 1 { - return fmt.Errorf("invalid sampling frequency: %d", cfg.SamplesPerSecond) - } - - if cfg.MapScaleFactor > 8 { - return fmt.Errorf( - "eBPF map scaling factor %d exceeds limit (max: %d)", - cfg.MapScaleFactor, MaxArgMapScaleFactor, - ) - } - - if cfg.BPFVerifierLogLevel > 2 { - return fmt.Errorf("invalid eBPF verifier log level: %d", cfg.BPFVerifierLogLevel) - } - - if cfg.ProbabilisticInterval < 1*time.Minute || cfg.ProbabilisticInterval > 5*time.Minute { - return errors.New( - "invalid argument for probabilistic-interval: use " + - "a duration between 1 and 5 minutes", - ) - } - - if cfg.ProbabilisticThreshold < 1 || - cfg.ProbabilisticThreshold > tracer.ProbabilisticThresholdMax { - return fmt.Errorf( - "invalid argument for probabilistic-threshold. Value "+ - "should be between 1 and %d", - tracer.ProbabilisticThresholdMax, - ) - } - - if cfg.OffCPUThreshold < 0.0 || cfg.OffCPUThreshold > 1.0 { - return errors.New( - "invalid argument for off-cpu-threshold. The value " + - "should be in the range [0..1]. 0 disables off-cpu profiling") - } - - if !cfg.NoKernelVersionCheck { - major, minor, patch, err := tracer.GetCurrentKernelVersion() - if err != nil { - return fmt.Errorf("failed to get kernel version: %v", err) - } - - var minMajor, minMinor uint32 - switch runtime.GOARCH { - case "amd64": - if cfg.VerboseMode { - minMajor, minMinor = 5, 2 - } else { - minMajor, minMinor = 4, 19 - } - case "arm64": - // Older ARM64 kernel versions have broken bpf_probe_read. - // https://github.com/torvalds/linux/commit/6ae08ae3dea2cfa03dd3665a3c8475c2d429ef47 - minMajor, minMinor = 5, 5 - default: - return fmt.Errorf("unsupported architecture: %s", runtime.GOARCH) - } - - if major < minMajor || (major == minMajor && minor < minMinor) { - return fmt.Errorf("host Agent requires kernel version "+ - "%d.%d or newer but got %d.%d.%d", minMajor, minMinor, major, minor, patch) - } - } - - return nil + return cfg.Config.Validate() } From 064ec721a017c316fe582acd3633359b0823d406 Mon Sep 17 00:00:00 2001 From: Olivier G Date: Wed, 8 Oct 2025 16:14:09 +0200 Subject: [PATCH 3/3] Add a test to validate configuration --- collector/config/config_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 collector/config/config_test.go diff --git a/collector/config/config_test.go b/collector/config/config_test.go new file mode 100644 index 000000000..63b27b237 --- /dev/null +++ b/collector/config/config_test.go @@ -0,0 +1,20 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package config // import "go.opentelemetry.io/ebpf-profiler/collector/config" + +import ( + "testing" + + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/confmap/xconfmap" +) + +func TestValidate(t *testing.T) { + cfg := &Config{ + SamplesPerSecond: 0, + } + err := xconfmap.Validate(cfg) + require.Error(t, err) + require.Equal(t, "invalid sampling frequency: 0", err.Error()) +}