Fix time.Since() in defer. Wrap in anonymous function

Function arguments in defer evaluated during definition of defer, not
during execution
This commit is contained in:
Slavik Panasovets
2022-10-25 12:38:35 +00:00
parent 9b9a963a8d
commit 864e41f16d
3 changed files with 10 additions and 3 deletions

View File

@@ -435,7 +435,9 @@ func (e *execPlugin) ExecPlugin(ctx context.Context, image string) (*credentialp
func (e *execPlugin) runPlugin(ctx context.Context, cmd *exec.Cmd, image string) error {
startTime := time.Now()
defer kubeletCredentialProviderPluginDuration.WithLabelValues(e.name).Observe(time.Since(startTime).Seconds())
defer func() {
kubeletCredentialProviderPluginDuration.WithLabelValues(e.name).Observe(time.Since(startTime).Seconds())
}()
err := cmd.Run()
if ctx.Err() != nil {