Skip to content

Commit

Permalink
Use PeriodicReader timeout for ForceFlush (#4377)
Browse files Browse the repository at this point in the history
* Use PeriodicReader timeout for ForceFlush

Prioritize the user passed context deadline if it has one.

* Update changelog
  • Loading branch information
MrAlias committed Jul 29, 2023
1 parent 859a870 commit 9418aca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -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

Expand Down
20 changes: 17 additions & 3 deletions sdk/metric/periodic_reader.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 9418aca

Please sign in to comment.