@@ -33,6 +33,10 @@ import (
3333 log "github.com/sirupsen/logrus"
3434)
3535
36+ const (
37+ logFieldReloadID = "reload_id"
38+ )
39+
3640type IReloadAgent interface {
3741 Reload () string
3842 Restart () error
@@ -115,6 +119,7 @@ func (ra *ReloadAgent) setLkgPath(configFile, path string) {
115119}
116120
117121func (ra * ReloadAgent ) handleReload (id string ) {
122+ logFields := map [string ]interface {}{logFieldReloadID : id }
118123 ra .cache .mu .Lock ()
119124 ra .cache .current = id
120125
@@ -126,10 +131,10 @@ func (ra *ReloadAgent) handleReload(id string) {
126131 response , err := ra .reloadHAProxy (id )
127132 if err != nil {
128133 ra .cache .failReload (response )
129- log .Warningf ( "Reload %s failed: %s" , id , err )
134+ log .WithFields ( logFields ). Warnf ( "Reload failed: %s" , err )
130135 } else {
131136 ra .cache .succeedReload (response )
132- log .Debugf ( "Handling reload %s completed, waiting for new requests" , id )
137+ log .WithFields ( logFields ). Debug ( "Handling reload completed, waiting for new requests" )
133138 }
134139}
135140
@@ -149,15 +154,16 @@ func (ra *ReloadAgent) handleReloads() {
149154}
150155
151156func (ra * ReloadAgent ) reloadHAProxy (id string ) (string , error ) {
157+ logFields := map [string ]interface {}{logFieldReloadID : id }
152158 // try the reload
153- log .Debugf ( "Reload %s started" , id )
159+ log .WithFields ( logFields ). Debug ( "Reload started" )
154160 t := time .Now ()
155161 output , err := execCmd (ra .reloadCmd )
156- log .Debugf ("Reload %s finished in %s" , id , time .Since (t ))
162+ log .WithFields ( logFields ). Debugf ("Reload finished in %s" , time .Since (t ))
157163 if err != nil {
158164 reloadFailedError := err
159165 // if failed, return to last known good file and restart and return the original file
160- log .Infof ( "Reload %s failed, restarting with last known good config..." , id )
166+ log .WithFields ( logFields ). Info ( "Reload failed, restarting with last known good config..." )
161167 if err := copyFile (ra .configFile , ra .configFile + ".bck" ); err != nil {
162168 return fmt .Sprintf ("Reload failed: %s, failed to backup original config file for restart." , output ), err
163169 }
@@ -170,13 +176,13 @@ func (ra *ReloadAgent) reloadHAProxy(id string) (string, error) {
170176 return fmt .Sprintf ("Reload failed: %s, failed to revert to last known good config file" , output ), err
171177 }
172178 if err := ra .restartHAProxy (); err != nil {
173- log .Warn ( "Restart failed, please check the reason and restart manually: " , err )
179+ log .WithFields ( logFields ). Warnf ( "Restart failed, please check the reason and restart manually: %s " , err )
174180 return fmt .Sprintf ("Reload failed: %s, failed to restart HAProxy, please check and start manually" , output ), err
175181 }
176- log .Debug ("HAProxy restarted with last known good config. " )
182+ log .WithFields ( logFields ). Debug ("HAProxy restarted with last known good config" )
177183 return output , reloadFailedError
178184 }
179- log .Debugf ( "Reload %s successful" , id )
185+ log .WithFields ( logFields ). Debug ( "Reload successful" )
180186 // if success, replace last known good file
181187 // nolint:errcheck
182188 copyFile (ra .configFile , ra .lkgConfigFile )
@@ -217,7 +223,7 @@ func (ra *ReloadAgent) Reload() string {
217223 next := ra .cache .getNext ()
218224 if next == "" {
219225 next = ra .cache .newReload ()
220- log .Debugf ( "Scheduling a new reload with id: %s" , next )
226+ log .WithFields ( map [ string ] interface {}{ logFieldReloadID : next }). Debug ( "Scheduling a new reload..." )
221227 }
222228
223229 return next
@@ -244,11 +250,12 @@ func (rc *reloadCache) Init(retention int) {
244250}
245251
246252func (rc * reloadCache ) newReload () string {
247- next := rc .generateID ()
248253 rc .mu .Lock ()
249- rc .next = next
250- rc .mu .Unlock ()
251- return next
254+ defer rc .mu .Unlock ()
255+ id := fmt .Sprintf ("%s-%v" , time .Now ().Format ("2006-01-02" ), rc .index )
256+ rc .index ++
257+ rc .next = id
258+ return rc .next
252259}
253260
254261func (rc * reloadCache ) getNext () string {
@@ -380,14 +387,6 @@ func (ra *ReloadAgent) Restart() error {
380387 return ra .restartHAProxy ()
381388}
382389
383- func (rc * reloadCache ) generateID () string {
384- rc .mu .Lock ()
385- defer rc .mu .Unlock ()
386- id := fmt .Sprintf ("%s-%v" , time .Now ().Format ("2006-01-02" ), rc .index )
387- rc .index ++
388- return id
389- }
390-
391390func getTimeIndexFromID (id string ) (time.Time , int64 , error ) {
392391 data := strings .Split (id , "-" )
393392 index , err := strconv .ParseInt (data [len (data )- 1 ], 10 , 64 )
0 commit comments