From 727509cc77b0e21fd59b491bf2298938c71e038c Mon Sep 17 00:00:00 2001 From: Gerry Agbobada Date: Thu, 30 Nov 2023 11:36:17 +0100 Subject: [PATCH] Read Otel metric export configuration from env vars --- go.work.sum | 1 + otel/autometrics/init.go | 6 ++++++ otel/autometrics/otel.go | 26 ++++++++++++++++++++++++-- pkg/autometrics/global_state.go | 10 ++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/go.work.sum b/go.work.sum index cf32a8a..cb6f727 100644 --- a/go.work.sum +++ b/go.work.sum @@ -355,6 +355,7 @@ github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3x github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= diff --git a/otel/autometrics/init.go b/otel/autometrics/init.go index 48f592b..057638d 100644 --- a/otel/autometrics/init.go +++ b/otel/autometrics/init.go @@ -175,6 +175,9 @@ func WithPushJobName(pushJobName string) InitOption { // WithPushPeriod sets the duration between consecutive metrics pushes. // +// The standard `OTEL_METRIC_EXPORT_INTERVAL` environment variable overrides +// this initialization argument. +// // The default value is 10 seconds. func WithPushPeriod(pushPeriod time.Duration) InitOption { return initOptionFunc(func(initArgs *initArguments) error { @@ -185,6 +188,9 @@ func WithPushPeriod(pushPeriod time.Duration) InitOption { // WithPushTimeout sets the timeout duration of a single metric push // +// The standard `OTEL_METRIC_EXPORT_TIMEOUT` environment variable overrides +// this initialization argument. +// // The default value is 5 seconds. func WithPushTimeout(pushTimeout time.Duration) InitOption { return initOptionFunc(func(initArgs *initArguments) error { diff --git a/otel/autometrics/otel.go b/otel/autometrics/otel.go index 690490f..601e488 100644 --- a/otel/autometrics/otel.go +++ b/otel/autometrics/otel.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "strconv" "sync" "time" @@ -314,10 +315,31 @@ func initProvider(pushExporter metric.Exporter, initArgs initArguments) (*metric timeout := defaultPushTimeout interval := defaultPushPeriod - if initArgs.pushPeriod > 0 { + readInitArgs := false + if pushPeriod, ok := os.LookupEnv(autometrics.OTelPushPeriodEnv); ok { + pushPeriodMs, err := strconv.ParseInt(pushPeriod, 10, 32) + if err != nil { + autometrics.GetLogger().Warn("opentelemetry: the push period environment variable has non-integer value, ignoring: %s", err) + readInitArgs = true + } else { + interval = time.Duration(pushPeriodMs) * time.Millisecond + } + } + if readInitArgs && initArgs.pushPeriod > 0 { interval = initArgs.pushPeriod } - if initArgs.pushTimeout > 0 { + + readInitArgs = false + if pushTimeout, ok := os.LookupEnv(autometrics.OTelPushTimeoutEnv); ok { + pushTimeoutMs, err := strconv.ParseInt(pushTimeout, 10, 32) + if err != nil { + autometrics.GetLogger().Warn("opentelemetry: the push timeout environment variable has non-integer value, ignoring: %s", err) + readInitArgs = true + } else { + timeout = time.Duration(pushTimeoutMs) * time.Millisecond + } + } + if readInitArgs && initArgs.pushTimeout > 0 { timeout = initArgs.pushTimeout } diff --git a/pkg/autometrics/global_state.go b/pkg/autometrics/global_state.go index 0501880..dd572f9 100644 --- a/pkg/autometrics/global_state.go +++ b/pkg/autometrics/global_state.go @@ -26,6 +26,16 @@ const ( // the repository provider to use as a label. This environment variable has precedence over // over hardcoding the variable directly in [BuildInfo] struct in the Init call. AutometricsRepoProviderEnv = "AUTOMETRICS_REPOSITORY_PROVIDER" + // OTelPushPeriodEnv is the name of the environment variable to declare to change the interval + // between 2 metrics pushes in milliseconds. + // + // Reference: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#periodic-exporting-metricreader + OTelPushPeriodEnv = "OTEL_METRIC_EXPORT_INTERVAL" + // OTelPushTimeoutEnv is the name of the environment variable to declare to change the timeout + // threshold of a single metrics push in milliseconds. + // + // Reference: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#periodic-exporting-metricreader + OTelPushTimeoutEnv = "OTEL_METRIC_EXPORT_TIMEOUT" ) var (