Skip to content

Commit

Permalink
Encapsulate time.Since in a defer function
Browse files Browse the repository at this point in the history
 * Fix how time.Since should be used with defer as described in
   golang/go#60048
 * Previously time.Since(start) is evaluated immediately when the defer
   is defined which means the latency recorded is always 0 seconds. By
   moving the time.Since into a function, it does not get evaluated
   until the function is called which properly times the processing
   latency.
  • Loading branch information
swetharepakula committed Jun 26, 2024
1 parent ab6fbfe commit d83b8c3
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/instancegroups/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ func (c *Controller) Shutdown() {
func (c *Controller) sync(key string) error {
start := time.Now()
klog.V(4).Infof("Instance groups controller: Start processing %s", key)
defer klog.V(4).Infof("Instance groups controller: Processing key %s took %v", key, time.Since(start))
defer func() {
klog.V(4).Infof("Instance groups controller: Processing key %s took %v", key, time.Since(start))
}()

nodeNames, err := utils.GetReadyNodeNames(listers.NewNodeLister(c.lister))
if err != nil {
Expand Down

0 comments on commit d83b8c3

Please sign in to comment.