fix(storage): Close meter provider after client close#14171
fix(storage): Close meter provider after client close#14171krishnamd-jkp wants to merge 1 commit intogoogleapis:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces two positive changes: it corrects the shutdown order of the gRPC connection and the metrics provider to prevent lost metrics, and it avoids shutting down a user-provided meter provider, which is proper resource lifecycle management. My review focuses on a latent bug in the metrics shutdown logic where an expired context is used, which would cause shutdown to fail. I've also pointed out that shutdown errors are not being handled. I've provided suggestions to fix these issues by using a new context for the shutdown operation and propagating any errors to the caller.
| // true if the provider was created by the SDK and should be shut down here | ||
| isDefaultProvider bool | ||
| // clean func to call when closing gRPC client | ||
| close func() |
There was a problem hiding this comment.
| close: func() { | ||
| provider.Shutdown(ctx) | ||
| if isDefault { | ||
| provider.Shutdown(ctx) | ||
| } | ||
| }, |
There was a problem hiding this comment.
There are two issues with the current implementation of close:
- The function captures
ctxfromnewGRPCMetricContext. This context is associated with client initialization and is likely to be expired whenclient.Close()is called. This would causeprovider.Shutdown(ctx)to fail. A new context should be created for the shutdown process. - The error returned by
provider.Shutdownis ignored. This error should be returned to the caller.
Note that after this change, grpcStorageClient.Close() will also need to be updated to handle the error returned from metricsContext.close().
close: func() error {
if isDefault {
// Use a background context with a timeout for shutdown to ensure
// it can complete even if the original context is expired.
shutdownCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
return provider.Shutdown(shutdownCtx)
}
return nil
},
No description provided.