Skip to content

Commit d395776

Browse files
authored
Allow CLI flags to be provided via config.yaml (#3094)
Allow config flags to be provided via config.yaml
1 parent 3249847 commit d395776

File tree

6 files changed

+448
-8
lines changed

6 files changed

+448
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ To debug potential errors, enable debug logging using `--log-level=debug`.
5353

5454
## Configuration
5555

56+
Parca Agent supports specifying configuration through command-line flags or a YAML configuration file via `--config-path`. The configuration file can contain both relabel configurations and any CLI flag using hyphenated names (e.g., `log-level`, `http-address`). Command-line arguments take precedence over configuration file values, making it easy to override specific settings while maintaining a base configuration file.
57+
5658
<details><summary>Flags:</summary>
5759
<p>
5860

flags/flags.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/alecthomas/kong"
25+
kongyaml "github.com/alecthomas/kong-yaml"
2526
log "github.com/sirupsen/logrus"
2627
"go.opentelemetry.io/ebpf-profiler/tracer"
2728
_ "google.golang.org/grpc/encoding/proto"
@@ -61,13 +62,43 @@ const (
6162
func Parse() (Flags, error) {
6263
flags := Flags{}
6364
hostname, hostnameErr := os.Hostname() // hotnameErr handled below.
64-
kong.Parse(&flags, kong.Vars{
65-
"hostname": hostname,
66-
"default_cpu_sampling_frequency": strconv.Itoa(defaultCPUSamplingFrequency),
67-
"default_map_scale_factor": strconv.Itoa(defaultMapScaleFactor),
68-
"max_map_scale_factor": strconv.Itoa(maxMapScaleFactor),
69-
"default_memlock_rlimit": "0", // No limit by default. (flag is deprecated)
70-
})
65+
66+
// Build Kong options
67+
kongOptions := []kong.Option{
68+
kong.Vars{
69+
"hostname": hostname,
70+
"default_cpu_sampling_frequency": strconv.Itoa(defaultCPUSamplingFrequency),
71+
"default_map_scale_factor": strconv.Itoa(defaultMapScaleFactor),
72+
"max_map_scale_factor": strconv.Itoa(maxMapScaleFactor),
73+
"default_memlock_rlimit": "0", // No limit by default. (flag is deprecated)
74+
},
75+
}
76+
77+
kong.Parse(&flags, kongOptions...)
78+
79+
// If a config path is provided, load the YAML configuration
80+
if flags.ConfigPath != "" {
81+
// Create a new parser with YAML configuration support
82+
parser, err := kong.New(&flags,
83+
kong.Vars{
84+
"hostname": hostname,
85+
"default_cpu_sampling_frequency": strconv.Itoa(defaultCPUSamplingFrequency),
86+
"default_map_scale_factor": strconv.Itoa(defaultMapScaleFactor),
87+
"max_map_scale_factor": strconv.Itoa(maxMapScaleFactor),
88+
"default_memlock_rlimit": "0",
89+
},
90+
kong.Configuration(kongyaml.Loader, flags.ConfigPath),
91+
)
92+
if err != nil {
93+
return Flags{}, fmt.Errorf("failed to create parser with config: %w", err)
94+
}
95+
96+
// Parse again with the configuration
97+
_, err = parser.Parse(os.Args[1:])
98+
if err != nil {
99+
return Flags{}, fmt.Errorf("failed to parse flags with config: %w", err)
100+
}
101+
}
71102

72103
if flags.Node == "" && hostnameErr != nil {
73104
return Flags{}, fmt.Errorf("failed to get hostname. Please set it with the --node flag: %w", hostnameErr)

0 commit comments

Comments
 (0)