-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(storage): Close meter provider after client close #14171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,6 +118,8 @@ type metricsContext struct { | |
| clientOpts []option.ClientOption | ||
| // instance of metric reader used by gRPC client-side metrics | ||
| provider *metric.MeterProvider | ||
| // 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() | ||
| } | ||
|
|
@@ -173,9 +175,11 @@ func newGRPCMetricContext(ctx context.Context, cfg metricsConfig) (*metricsConte | |
| meterOpts = append(meterOpts, metric.WithReader( | ||
| metric.NewPeriodicReader(&exporterLogSuppressor{Exporter: exporter}, metric.WithInterval(interval)))) | ||
| } | ||
| isDefault := false | ||
| provider := cfg.meterProvider | ||
| if provider == nil { | ||
| provider = metric.NewMeterProvider(meterOpts...) | ||
| isDefault = true | ||
| } | ||
| mo := opentelemetry.MetricsOptions{ | ||
| MeterProvider: provider, | ||
|
|
@@ -204,10 +208,13 @@ func newGRPCMetricContext(ctx context.Context, cfg metricsConfig) (*metricsConte | |
| grpc.WithDefaultCallOptions(grpc.StaticMethodCallOption{})), | ||
| } | ||
| return &metricsContext{ | ||
| clientOpts: opts, | ||
| provider: provider, | ||
| clientOpts: opts, | ||
| provider: provider, | ||
| isDefaultProvider: isDefault, | ||
| close: func() { | ||
| provider.Shutdown(ctx) | ||
| if isDefault { | ||
| provider.Shutdown(ctx) | ||
| } | ||
| }, | ||
|
Comment on lines
214
to
218
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two issues with the current implementation of
Note that after this change, 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
},
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix this? |
||
| }, nil | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
closefunction should return an error to propagate the result ofprovider.Shutdown. TheShutdownmethod onMeterProvidercan return an error (e.g., if the shutdown context times out) which should be handled by the client'sClosemethod.