Skip to content

Commit

Permalink
config: Add support to configure periodic reader interval and timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdandrutu committed May 24, 2024
1 parent 1c0eae0 commit 6629d38
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
11 changes: 9 additions & 2 deletions config/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
Expand Down Expand Up @@ -59,7 +58,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 {
Expand Down
22 changes: 22 additions & 0 deletions config/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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) {
Expand All @@ -476,3 +494,7 @@ func TestReader(t *testing.T) {
})
}
}

func newIntPtr(i int) *int {
return &i
}

0 comments on commit 6629d38

Please sign in to comment.