Skip to content

Commit

Permalink
Move main.go config into controller package (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu authored Oct 24, 2024
1 parent 9be4ee2 commit 2c2cb6b
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 147 deletions.
79 changes: 24 additions & 55 deletions cli_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"

"github.com/peterbourgon/ff/v3"
log "github.com/sirupsen/logrus"

"go.opentelemetry.io/ebpf-profiler/internal/controller"
"go.opentelemetry.io/ebpf-profiler/tracer"
)

Expand All @@ -27,8 +27,6 @@ const (

// This is the X in 2^(n + x) where n is the default hardcoded map size value
defaultArgMapScaleFactor = 0
// 1TB of executable address space
maxArgMapScaleFactor = 8
)

// Help strings for command line arguments
Expand All @@ -42,7 +40,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, maxArgMapScaleFactor)
defaultArgMapScaleFactor, controller.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."
Expand All @@ -65,84 +63,62 @@ var (
sendErrorFramesHelp = "Send error frames (devfiler only, breaks Kibana)"
)

type arguments 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

fs *flag.FlagSet
}

// Package-scope variable, so that conditionally compiled other components can refer
// to the same flagset.

func parseArgs() (*arguments, error) {
var args arguments
func parseArgs() (*controller.Config, error) {
var args controller.Config

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)
fs.StringVar(&args.CollAgentAddr, "collection-agent", "", collAgentAddrHelp)
fs.BoolVar(&args.Copyright, "copyright", false, copyrightHelp)

fs.BoolVar(&args.disableTLS, "disable-tls", false, disableTLSHelp)
fs.BoolVar(&args.DisableTLS, "disable-tls", false, disableTLSHelp)

fs.UintVar(&args.mapScaleFactor, "map-scale-factor",
fs.UintVar(&args.MapScaleFactor, "map-scale-factor",
defaultArgMapScaleFactor, mapScaleFactorHelp)

fs.DurationVar(&args.monitorInterval, "monitor-interval", defaultArgMonitorInterval,
fs.DurationVar(&args.MonitorInterval, "monitor-interval", defaultArgMonitorInterval,
monitorIntervalHelp)

fs.DurationVar(&args.clockSyncInterval, "clock-sync-interval", defaultClockSyncInterval,
fs.DurationVar(&args.ClockSyncInterval, "clock-sync-interval", defaultClockSyncInterval,
clockSyncIntervalHelp)

fs.BoolVar(&args.noKernelVersionCheck, "no-kernel-version-check", false,
fs.BoolVar(&args.NoKernelVersionCheck, "no-kernel-version-check", false,
noKernelVersionCheckHelp)

fs.StringVar(&args.pprofAddr, "pprof", "", pprofHelp)
fs.StringVar(&args.PprofAddr, "pprof", "", pprofHelp)

fs.DurationVar(&args.probabilisticInterval, "probabilistic-interval",
fs.DurationVar(&args.ProbabilisticInterval, "probabilistic-interval",
defaultProbabilisticInterval, probabilisticIntervalHelp)
fs.UintVar(&args.probabilisticThreshold, "probabilistic-threshold",
fs.UintVar(&args.ProbabilisticThreshold, "probabilistic-threshold",
defaultProbabilisticThreshold, probabilisticThresholdHelp)

fs.DurationVar(&args.reporterInterval, "reporter-interval", defaultArgReporterInterval,
fs.DurationVar(&args.ReporterInterval, "reporter-interval", defaultArgReporterInterval,
reporterIntervalHelp)

fs.IntVar(&args.samplesPerSecond, "samples-per-second", defaultArgSamplesPerSecond,
fs.IntVar(&args.SamplesPerSecond, "samples-per-second", defaultArgSamplesPerSecond,
samplesPerSecondHelp)

fs.BoolVar(&args.sendErrorFrames, "send-error-frames", defaultArgSendErrorFrames,
fs.BoolVar(&args.SendErrorFrames, "send-error-frames", defaultArgSendErrorFrames,
sendErrorFramesHelp)

fs.StringVar(&args.tracers, "t", "all", "Shorthand for -tracers.")
fs.StringVar(&args.tracers, "tracers", "all", tracersHelp)
fs.StringVar(&args.Tracers, "t", "all", "Shorthand for -tracers.")
fs.StringVar(&args.Tracers, "tracers", "all", tracersHelp)

fs.BoolVar(&args.verboseMode, "v", false, "Shorthand for -verbose.")
fs.BoolVar(&args.verboseMode, "verbose", false, verboseModeHelp)
fs.BoolVar(&args.version, "version", false, versionHelp)
fs.BoolVar(&args.VerboseMode, "v", false, "Shorthand for -verbose.")
fs.BoolVar(&args.VerboseMode, "verbose", false, verboseModeHelp)
fs.BoolVar(&args.Version, "version", false, versionHelp)

fs.Usage = func() {
fs.PrintDefaults()
}

args.fs = fs
args.Fs = fs

return &args, ff.Parse(fs, os.Args[1:],
ff.WithEnvVarPrefix("OTEL_PROFILING_AGENT"),
Expand All @@ -154,10 +130,3 @@ func parseArgs() (*arguments, error) {
ff.WithAllowMissingConfigFile(true),
)
}

func (args *arguments) dump() {
log.Debug("Config:")
args.fs.VisitAll(func(f *flag.Flag) {
log.Debug(fmt.Sprintf("%s: %v", f.Name, f.Value))
})
}
113 changes: 113 additions & 0 deletions internal/controller/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package controller // import "go.opentelemetry.io/ebpf-profiler/internal/controller"

import (
"errors"
"flag"
"fmt"
"runtime"
"time"

log "github.com/sirupsen/logrus"
"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

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() {
log.Debug("Config:")
cfg.Fs.VisitAll(func(f *flag.Flag) {
log.Debug(fmt.Sprintf("%s: %v", f.Name, f.Value))
})
}

// 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.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
}
Loading

0 comments on commit 2c2cb6b

Please sign in to comment.