Skip to content

Commit

Permalink
Read Otel metric export configuration from env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
gagbo committed Nov 30, 2023
1 parent 0464f0e commit 727509c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 6 additions & 0 deletions otel/autometrics/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
26 changes: 24 additions & 2 deletions otel/autometrics/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/autometrics/global_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 727509c

Please sign in to comment.