Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return errors from resource.New #59

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions otelconfig/otelconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ type Config struct {
errorHandler otel.ErrorHandler
}

func newConfig(opts ...Option) *Config {
func newConfig(opts ...Option) (*Config, error) {
c := &Config{
Headers: map[string]string{},
TracesHeaders: map[string]string{},
Expand All @@ -344,6 +344,8 @@ func newConfig(opts ...Option) *Config {
envError := envconfig.Process(context.Background(), c)
if envError != nil {
c.Logger.Fatalf("environment error: %v", envError)
// if our logger implementation doesn't os.Exit, we want to return here
return nil, fmt.Errorf("environment error: %w", envError)
}
// if exporter endpoint is not set using an env var, use the configured default
if c.ExporterEndpoint == "" {
Expand All @@ -366,8 +368,9 @@ func newConfig(opts ...Option) *Config {
l.logLevel = c.LogLevel
}

c.Resource = newResource(c)
return c
var err error
c.Resource, err = newResource(c)
return c, err
}

// OtelConfig is the object we're here for; it implements the initialization of Open Telemetry.
Expand All @@ -376,7 +379,7 @@ type OtelConfig struct {
shutdownFuncs []func() error
}

func newResource(c *Config) *resource.Resource {
func newResource(c *Config) (*resource.Resource, error) {
r := resource.Environment()

hostnameSet := false
Expand Down Expand Up @@ -427,19 +430,14 @@ func newResource(c *Config) *resource.Resource {

options := append(baseOptions, c.ResourceOptions...)

r, err := resource.New(
// Note: There are new detectors we may wish to take advantage
// of, now available in the default SDK (e.g., WithProcess(),
// WithOSType(), ...).
return resource.New(
context.Background(),
options...,
)

if err != nil {
c.Logger.Debugf("error applying resource options: %s", err)
}

// Note: There are new detectors we may wish to take advantage
// of, now available in the default SDK (e.g., WithProcess(),
// WithOSType(), ...).
return r
}

type setupFunc func(*Config) (func() error, error)
Expand Down Expand Up @@ -616,7 +614,10 @@ func setupMetrics(c *Config) (func() error, error) {
// ConfigureOpenTelemetry is a function that be called with zero or more options.
// Options can be the basic ones above, or provided by individual vendors.
func ConfigureOpenTelemetry(opts ...Option) (func(), error) {
c := newConfig(opts...)
c, err := newConfig(opts...)
if err != nil {
return nil, err
}

if c.LogLevel == "debug" {
c.Logger.Debugf("debug logging enabled")
Expand Down
Loading