From e5e77a9cc072f3884c810cce4c154e92be687cb4 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 24 May 2024 02:30:27 -0700 Subject: [PATCH] config: Add support to configure periodic reader interval and timeout --- CHANGELOG.md | 4 ++++ config/metric.go | 10 +++++++++- config/metric_test.go | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e6c88f1967..0d2d722288d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Added + +- The `go.opentelemetry.io/contrib/config` add support to configure periodic reader interval and timeout. (#5661) + ### Deprecated - The `go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo` package is deprecated. diff --git a/config/metric.go b/config/metric.go index 0b7543d3dbc..4763406b529 100644 --- a/config/metric.go +++ b/config/metric.go @@ -59,7 +59,15 @@ func metricReader(ctx context.Context, r MetricReader) (sdkmetric.Reader, error) } if r.Periodic != nil { - return periodicExporter(ctx, r.Periodic.Exporter) + var opts []sdkmetric.PeriodicReaderOption + if r.Periodic.Interval != nil { + opts = append(opts, sdkmetric.WithInterval(time.Duration(*r.Periodic.Interval)*time.Millisecond)) + } + + if r.Periodic.Timeout != nil { + opts = append(opts, sdkmetric.WithTimeout(time.Duration(*r.Periodic.Timeout)*time.Millisecond)) + } + return periodicExporter(ctx, r.Periodic.Exporter, opts...) } if r.Pull != nil { diff --git a/config/metric_test.go b/config/metric_test.go index f8208b080f0..7fc0fabc6e5 100644 --- a/config/metric_test.go +++ b/config/metric_test.go @@ -9,6 +9,7 @@ import ( "net/url" "reflect" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -450,6 +451,23 @@ func TestReader(t *testing.T) { }, wantReader: sdkmetric.NewPeriodicReader(consoleExporter), }, + { + name: "periodic/console-exporter-with-extra-options", + reader: MetricReader{ + Periodic: &PeriodicMetricReader{ + Interval: newIntPtr(30_000), + Timeout: newIntPtr(5_000), + Exporter: MetricExporter{ + Console: Console{}, + }, + }, + }, + wantReader: sdkmetric.NewPeriodicReader( + consoleExporter, + sdkmetric.WithInterval(30_000*time.Millisecond), + sdkmetric.WithTimeout(5_000*time.Millisecond), + ), + }, } for _, tt := range testCases { t.Run(tt.name, func(t *testing.T) { @@ -476,3 +494,7 @@ func TestReader(t *testing.T) { }) } } + +func newIntPtr(i int) *int { + return &i +}