From 4c44d5c785fd7beda6a748a845d8355eedd4ce31 Mon Sep 17 00:00:00 2001 From: Niels Henrik Hagen Date: Thu, 28 Nov 2024 22:36:51 +0100 Subject: [PATCH] Refactor config resolution --- bootstrap2.go | 28 +++++++++------------------- internal/env.go | 13 +++++++++++++ options.go | 18 ++++++++++++++---- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/bootstrap2.go b/bootstrap2.go index 339f2613..98d705c7 100644 --- a/bootstrap2.go +++ b/bootstrap2.go @@ -20,7 +20,7 @@ func Start(ctx context.Context, options ...Option) (context.Context, context.Can return ctx, context.CancelFunc(func() {}), nil } - err := verifyEnvVarsSet( + err := internal.VerifyEnvVarsSet( internal.DatadogAPMEndpoint, internal.DatadogDSDEndpoint, internal.DatadogService, @@ -31,12 +31,9 @@ func Start(ctx context.Context, options ...Option) (context.Context, context.Can return ctx, context.CancelFunc(func() {}), err } - cfg := defaultConfig() - - options = append([]Option{withConfigFromEnvVars()}, options...) - - for _, option := range options { - option(cfg) + cfg, err := resolveConfig(options) + if err != nil { + return ctx, context.CancelFunc(func() {}), err } ctx, cancel := context.WithCancel(ctx) @@ -57,7 +54,11 @@ func Start(ctx context.Context, options ...Option) (context.Context, context.Can func start(_ context.Context, cfg *config) error { startTracer(cfg) - return startProfiler(cfg) + err := startProfiler(cfg) + if err != nil { + return err + } + return nil } func startTracer(cfg *config) { @@ -96,17 +97,6 @@ func stop(_ *config) { defer profiler.Stop() } -func verifyEnvVarsSet(keys ...string) error { - for _, key := range keys { - val, ok := os.LookupEnv(key) - println(key, val) - if !ok || val == "" { - return fmt.Errorf("required environmental variable not set: %q", key) - } - } - return nil -} - func getBoolEnv(key string, fallback bool) bool { valStr, ok := os.LookupEnv(key) if !ok { diff --git a/internal/env.go b/internal/env.go index 1964ce6f..5b4d5225 100644 --- a/internal/env.go +++ b/internal/env.go @@ -1,6 +1,7 @@ package internal import ( + "fmt" "os" "strconv" ) @@ -38,3 +39,15 @@ func IsDatadogDisabled() bool { } return val } + +// VerifyEnvVarsSet checks if the provided environmental variables are defined +func VerifyEnvVarsSet(keys ...string) error { + for _, key := range keys { + val, ok := os.LookupEnv(key) + println(key, val) + if !ok || val == "" { + return fmt.Errorf("required environmental variable not set: %q", key) + } + } + return nil +} diff --git a/options.go b/options.go index df98f4fe..79cf8bdc 100644 --- a/options.go +++ b/options.go @@ -15,21 +15,31 @@ type config struct { enableExtraProfiling bool } -func defaultConfig() *config { - return &config{ +func resolveConfig(options []Option) (*config, error) { + cfg := &config{ enableTracing: defaultEnableTracing, enableProfiling: defaultEnableProfiling, enableExtraProfiling: defaultEnableExtraProfiling, } + options = append([]Option{withConfigFromEnvVars()}, options...) + + for _, option := range options { + err := option(cfg) + if err != nil { + return nil, err + } + } + return cfg, nil } // Option is used to configure the behaviour of the Datadog integration. -type Option func(*config) +type Option func(*config) error func withConfigFromEnvVars() Option { - return func(cfg *config) { + return func(cfg *config) error { cfg.enableTracing = getBoolEnv(internal.DatadogEnableTracing, cfg.enableTracing) cfg.enableProfiling = getBoolEnv(internal.DatadogEnableProfiling, cfg.enableProfiling) cfg.enableExtraProfiling = getBoolEnv(internal.DatadogEnableExtraProfiling, cfg.enableExtraProfiling) + return nil } }