From b6c1c4edb93aadec51952e7d049e338d4effb819 Mon Sep 17 00:00:00 2001 From: dmathieu Date: Thu, 15 Jun 2023 12:06:04 +0200 Subject: [PATCH 1/3] move periodic reader context timeout to collectAndExport --- sdk/metric/periodic_reader.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sdk/metric/periodic_reader.go b/sdk/metric/periodic_reader.go index e32b05a9f6d..c98955e0c28 100644 --- a/sdk/metric/periodic_reader.go +++ b/sdk/metric/periodic_reader.go @@ -219,6 +219,9 @@ func (r *periodicReader) aggregation(kind InstrumentKind) aggregation.Aggregatio // collectAndExport gather all metric data related to the periodicReader r from // the SDK and exports it with r's exporter. func (r *periodicReader) collectAndExport(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, r.timeout) + defer cancel() + // TODO (#3047): Use a sync.Pool or persistent pointer instead of allocating rm every Collect. rm := r.rmPool.Get().(*metricdata.ResourceMetrics) err := r.Collect(ctx, rm) @@ -276,9 +279,7 @@ func (r *periodicReader) collect(ctx context.Context, p interface{}, rm *metricd // export exports metric data m using r's exporter. func (r *periodicReader) export(ctx context.Context, m *metricdata.ResourceMetrics) error { - c, cancel := context.WithTimeout(ctx, r.timeout) - defer cancel() - return r.exporter.Export(c, m) + return r.exporter.Export(ctx, m) } // ForceFlush flushes pending telemetry. From f378f7b697a941dd3fe68590a2c89896c4e0c015 Mon Sep 17 00:00:00 2001 From: dmathieu Date: Thu, 15 Jun 2023 12:09:22 +0200 Subject: [PATCH 2/3] document periodic reader honoring timeouts --- sdk/metric/periodic_reader.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sdk/metric/periodic_reader.go b/sdk/metric/periodic_reader.go index c98955e0c28..14822044775 100644 --- a/sdk/metric/periodic_reader.go +++ b/sdk/metric/periodic_reader.go @@ -104,9 +104,9 @@ func WithInterval(d time.Duration) PeriodicReaderOption { // NewPeriodicReader returns a Reader that collects and exports metric data to // the exporter at a defined interval. By default, the returned Reader will -// collect and export data every 60 seconds, and will cancel export attempts -// that exceed 30 seconds. The export time is not counted towards the interval -// between attempts. +// collect and export data every 60 seconds, and will cancel any attempts that +// exceed 30 seconds, collect and export combined. The collect and export time +// are not counted towards the interval between attempts. // // The Collect method of the returned Reader continues to gather and return // metric data to the user. It will not automatically send that data to the @@ -237,7 +237,8 @@ func (r *periodicReader) collectAndExport(ctx context.Context) error { // data is not exported to the configured exporter, it is left to the caller to // handle that if desired. // -// An error is returned if this is called after Shutdown. An error is return if rm is nil. +// An error is returned if this is called after Shutdown, if rm is nil or if +// the duration of the collect and export exceeded the timeout. func (r *periodicReader) Collect(ctx context.Context, rm *metricdata.ResourceMetrics) error { if rm == nil { return errors.New("periodic reader: *metricdata.ResourceMetrics is nil") From 9963a19a81fead3459966849b4f86edbcec18bcb Mon Sep 17 00:00:00 2001 From: dmathieu Date: Thu, 15 Jun 2023 12:10:22 +0200 Subject: [PATCH 3/3] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e68d9fc0534..e464d2fd9de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Starting from `v1.21.0` of semantic conventions, `go.opentelemetry.io/otel/semconv/{version}/httpconv` and `go.opentelemetry.io/otel/semconv/{version}/netconv` packages will no longer be published. (#4145) - Log duplicate instrument conflict at a warning level instead of info in `go.opentelemetry.io/otel/sdk/metric`. (#4202) - Return an error on the creation of new instruments if their name doesn't pass regexp validation. (#4210) +- Count the Collect time in the PeriodicReader timeout. (#4221) ## [1.16.0/0.39.0] 2023-05-18