Skip to content

Commit 1db63ad

Browse files
committed
update code to use env var in NewSDK instead of init func
Signed-off-by: alex boten <[email protected]>
1 parent eafa057 commit 1db63ad

File tree

2 files changed

+15
-43
lines changed

2 files changed

+15
-43
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2121
- Add a `WithSpanNameFormatter` option to `go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo`. (#7986)
2222
- Add unmarshaling and validation for `AttributeType`, `AttributeNameValue`, `SimpleSpanProcessor`, `SimpleLogRecordProcessor`, `ZipkinSpanExporter`, `NameStringValuePair`, `InstrumentType`, `ExperimentalPeerInstrumentationServiceMappingElem`, `ExporterDefaultHistogramAggregation`, `PullMetricReader` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8127)
2323
- Updated `go.opentelemetry.io/contrib/otelconf` to include the [v1.0.0-rc2](https://github.com/open-telemetry/opentelemetry-configuration/releases/tag/v1.0.0-rc.2) release candidate of schema which includes backwards incompatible changes. (#8026)
24-
- Add support for `OTEL_EXPERIMENTAL_CONFIG_FILE` via an init function in `go.opentelemetry.io/contrib/otelconf` (#8106)
24+
- Add support for `OTEL_EXPERIMENTAL_CONFIG_FILE` via the `NewSDK` function in `go.opentelemetry.io/contrib/otelconf` (#8106)
2525

2626
### Changed
2727

otelconf/config.go

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package otelconf // import "go.opentelemetry.io/contrib/otelconf"
77
import (
88
"context"
99
"errors"
10-
"log"
1110
"os"
1211

1312
apilog "go.opentelemetry.io/otel/log"
@@ -60,60 +59,33 @@ var noopSDK = SDK{
6059
shutdown: func(context.Context) error { return nil },
6160
}
6261

63-
var sdk *SDK
64-
65-
// init checks the local environment and uses the file set in the variable
66-
// `OTEL_EXPERIMENTAL_CONFIG_FILE` to configure the SDK automatically.
67-
func init() {
68-
// look for the env variable
69-
filename, ok := os.LookupEnv("OTEL_EXPERIMENTAL_CONFIG_FILE")
70-
if !ok {
71-
return
72-
}
62+
func parseConfigFileFromEnvironment(filename string) (ConfigurationOption, error) {
7363
b, err := os.ReadFile(filename)
7464
if err != nil {
75-
log.Fatal(err)
65+
return nil, err
7666
}
7767

7868
// Parse a configuration file into an OpenTelemetryConfiguration model.
7969
c, err := ParseYAML(b)
8070
if err != nil {
81-
log.Fatal(err)
71+
return nil, err
8272
}
8373

8474
// Create SDK components with the parsed configuration.
85-
s, err := NewSDK(WithOpenTelemetryConfiguration(*c))
86-
if err != nil {
87-
log.Fatal(err)
88-
}
89-
90-
sdk = &s
75+
return WithOpenTelemetryConfiguration(*c), nil
9176
}
9277

93-
// Global returns the globally configured SDK only instantiated
94-
// through the use of the `OTEL_EXPERIMENTAL_CONFIG_FILE“ environment
95-
// variable.
96-
func Global() SDK {
97-
if sdk != nil {
98-
return *sdk
99-
}
100-
return noopSDK
101-
}
102-
103-
// Shutdown calls the `Shutdown` function of the globally configured SDK only instantiated
104-
// through the use of the `OTEL_EXPERIMENTAL_CONFIG_FILE“ environment
105-
// variable.
106-
func Shutdown(ctx context.Context) {
107-
if sdk == nil {
108-
return
109-
}
110-
if err := sdk.Shutdown(ctx); err != nil {
111-
log.Fatal(err)
112-
}
113-
}
114-
115-
// NewSDK creates SDK providers based on the configuration model.
78+
// NewSDK creates SDK providers based on the configuration model. It checks the local environment and
79+
// uses the file set in the variable `OTEL_EXPERIMENTAL_CONFIG_FILE` to configure the SDK automatically.
11680
func NewSDK(opts ...ConfigurationOption) (SDK, error) {
81+
filename, ok := os.LookupEnv("OTEL_EXPERIMENTAL_CONFIG_FILE")
82+
if ok {
83+
opt, err := parseConfigFileFromEnvironment(filename)
84+
if err != nil {
85+
return noopSDK, err
86+
}
87+
opts = append(opts, opt)
88+
}
11789
o := configOptions{
11890
ctx: context.Background(),
11991
}

0 commit comments

Comments
 (0)