@@ -73,6 +73,7 @@ func main() {
7373 flag .StringVar (& opts .LogFormat , "log-format" , "console" , "Define log format. Allowed values: console, json" )
7474 flag .BoolVar (& opts .VerifyConfig , "verify-config" , false , "Enable this flag to check config file loads, then exit" )
7575 flag .BoolVar (& opts .Version , "version" , false , "set to print version information" )
76+ flag .BoolVar (& opts .EnableAutoReload , "enable-auto-reload" , true , "Enable automatic config reload when the config file changes" )
7677 flag .Parse ()
7778
7879 if opts .Version {
@@ -126,6 +127,38 @@ func main() {
126127 os .Exit (1 )
127128 }
128129
130+ var configWatcher * config.ConfigWatcher
131+ if opts .EnableAutoReload && opts .ConfigFile != "" {
132+ configWatcher , err = config .NewConfigWatcher (logger , opts .ConfigFile , func () {
133+ // Create a new config instance for reloading
134+ newCfg := config.Config {
135+ Listen : config.ListenConfig {
136+ Port : 4040 ,
137+ MetricsEndpoint : "/metrics" ,
138+ },
139+ }
140+
141+ if err := config .LoadConfigFromFile (logger , & newCfg , opts .ConfigFile ); err != nil {
142+ logger .Errorf ("error reloading config: %v" , err )
143+ return
144+ }
145+
146+ if stabilityError := newCfg .StabilityWarnings (); stabilityError != nil && ! opts .EnableExperimentalFeatures {
147+ logger .Errorf ("reloaded config contains experimental features but they are not enabled" )
148+ return
149+ }
150+
151+ // Update the current config
152+ cfg = newCfg
153+ logger .Info ("configuration reloaded successfully" )
154+ })
155+ if err != nil {
156+ logger .Errorf ("error setting up config watcher: %v" , err )
157+ } else {
158+ defer configWatcher .Close ()
159+ }
160+ }
161+
129162 if cfg .Consul .Enable {
130163 setupConsul (logger , & cfg , stopChan , & stopHandlers )
131164 }
0 commit comments