From d83b8c316c5ac91a7ff9e2b8ebde21d0bf4ce629 Mon Sep 17 00:00:00 2001 From: Swetha Repakula Date: Tue, 25 Jun 2024 17:51:31 -0700 Subject: [PATCH] Encapsulate time.Since in a defer function * Fix how time.Since should be used with defer as described in https://github.com/golang/go/issues/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. --- pkg/instancegroups/controller.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/instancegroups/controller.go b/pkg/instancegroups/controller.go index 2ddb4c909b..13c79826a9 100644 --- a/pkg/instancegroups/controller.go +++ b/pkg/instancegroups/controller.go @@ -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 {