From 722cbef23b19a7596e05a549db98e5993ba925b4 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 27 Jul 2023 14:20:05 -0700 Subject: [PATCH 1/2] Use PeriodicReader timeout for ForceFlush Prioritize the user passed context deadline if it has one. --- sdk/metric/periodic_reader.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/sdk/metric/periodic_reader.go b/sdk/metric/periodic_reader.go index e04c1edbe18..f62a2ae41e3 100644 --- a/sdk/metric/periodic_reader.go +++ b/sdk/metric/periodic_reader.go @@ -68,7 +68,9 @@ func (o periodicReaderOptionFunc) applyPeriodic(conf periodicReaderConfig) perio // WithTimeout configures the time a PeriodicReader waits for an export to // complete before canceling it. This includes an export which occurs as part -// of Shutdown. +// of Shutdown or ForceFlush if the user passed context does not have a +// deadline. If the user passed context does have a deadline, it will be used +// instead. // // This option overrides any value set for the // OTEL_METRIC_EXPORT_TIMEOUT environment variable. @@ -298,6 +300,13 @@ func (r *PeriodicReader) export(ctx context.Context, m *metricdata.ResourceMetri // // This method is safe to call concurrently. func (r *PeriodicReader) ForceFlush(ctx context.Context) error { + // Prioritize the ctx timeout if it is set. + if _, ok := ctx.Deadline(); !ok { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, r.timeout) + defer cancel() + } + errCh := make(chan error, 1) select { case r.flushCh <- errCh: @@ -324,8 +333,13 @@ func (r *PeriodicReader) ForceFlush(ctx context.Context) error { func (r *PeriodicReader) Shutdown(ctx context.Context) error { err := ErrReaderShutdown r.shutdownOnce.Do(func() { - ctx, cancel := context.WithTimeout(ctx, r.timeout) - defer cancel() + // Prioritize the ctx timeout if it is set. + if _, ok := ctx.Deadline(); !ok { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, r.timeout) + defer cancel() + } + // Stop the run loop. r.cancel() <-r.done From 26c9441dad5c16fbaa6b77f6e960911d0e71887f Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 27 Jul 2023 14:22:14 -0700 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e07b19348af..83f78535ee6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,7 +37,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - If an attribute set is Observed multiple times in an async callback, the values will be summed instead of the last observation winning. (#4289) - Allow the explicit bucket histogram aggregation to be used for the up-down counter, observable counter, observable up-down counter, and observable gauge in the `go.opentelemetry.io/otel/sdk/metric` package. (#4332) - Restrict `Meter`s in `go.opentelemetry.io/otel/sdk/metric` to only register and collect instruments it created. (#4333) -- `PeriodicReader.Shutdown` in `go.opentelemetry.io/otel/sdk/metric` now applies the periodic reader's timeout by default. (#4356) +- `PeriodicReader.Shutdown` and `PeriodicReader.ForceFlush` in `go.opentelemetry.io/otel/sdk/metric` now apply the periodic reader's timeout to the operation if the user provided context does not contain a deadline. (#4356, #4377) ### Fixed