Skip to content

Commit

Permalink
Refactor config resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
nhhagen committed Nov 28, 2024
1 parent eb6fdf3 commit 4c44d5c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
28 changes: 9 additions & 19 deletions bootstrap2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions internal/env.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"fmt"
"os"
"strconv"
)
Expand Down Expand Up @@ -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
}
18 changes: 14 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 4c44d5c

Please sign in to comment.