Skip to content

Commit

Permalink
config: return a noop SDK if disabled is set to true (open-telemetry#…
Browse files Browse the repository at this point in the history
…6185)

This change ensures the config package returns a noop SDK if disabled
has been set to true by the config.

Additionally, returning the noop SDK on errors to reduce the chances for
end users to use an uninitialized sdk struct.

---------

Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
codeboten authored Oct 4, 2024
1 parent 5d9f9cd commit 1428c2e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed

- The function signature of `NewLogProcessor` in `go.opentelemetry.io/contrib/processors/minsev` has changed to accept the added `Severitier` interface instead of a `log.Severity`. (#6116)
- `NewSDK` in `go.opentelemetry.io/contrib/config` now returns a no-op SDK if `disabled` is set to `true`. (#6185)

### Fixed

Expand Down
21 changes: 17 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
"gopkg.in/yaml.v3"

"go.opentelemetry.io/otel/log"
nooplog "go.opentelemetry.io/otel/log/noop"
"go.opentelemetry.io/otel/metric"
noopmetric "go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"
nooptrace "go.opentelemetry.io/otel/trace/noop"
)

const (
Expand Down Expand Up @@ -62,31 +65,41 @@ func (s *SDK) Shutdown(ctx context.Context) error {
return s.shutdown(ctx)
}

var noopSDK = SDK{
loggerProvider: nooplog.LoggerProvider{},
meterProvider: noopmetric.MeterProvider{},
tracerProvider: nooptrace.TracerProvider{},
shutdown: func(ctx context.Context) error { return nil },
}

// NewSDK creates SDK providers based on the configuration model.
func NewSDK(opts ...ConfigurationOption) (SDK, error) {
o := configOptions{}
for _, opt := range opts {
o = opt.apply(o)
}
if o.opentelemetryConfig.Disabled != nil && *o.opentelemetryConfig.Disabled {
return noopSDK, nil
}

r, err := newResource(o.opentelemetryConfig.Resource)
if err != nil {
return SDK{}, err
return noopSDK, err
}

mp, mpShutdown, err := meterProvider(o, r)
if err != nil {
return SDK{}, err
return noopSDK, err
}

tp, tpShutdown, err := tracerProvider(o, r)
if err != nil {
return SDK{}, err
return noopSDK, err
}

lp, lpShutdown, err := loggerProvider(o, r)
if err != nil {
return SDK{}, err
return noopSDK, err
}

return SDK{
Expand Down
15 changes: 15 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ func TestNewSDK(t *testing.T) {
wantMeterProvider: &sdkmetric.MeterProvider{},
wantLoggerProvider: &sdklog.LoggerProvider{},
},
{
name: "with-sdk-disabled",
cfg: []ConfigurationOption{
WithContext(context.Background()),
WithOpenTelemetryConfiguration(OpenTelemetryConfiguration{
Disabled: ptr(true),
TracerProvider: &TracerProvider{},
MeterProvider: &MeterProvider{},
LoggerProvider: &LoggerProvider{},
}),
},
wantTracerProvider: tracenoop.NewTracerProvider(),
wantMeterProvider: metricnoop.NewMeterProvider(),
wantLoggerProvider: lognoop.NewLoggerProvider(),
},
}
for _, tt := range tests {
sdk, err := NewSDK(tt.cfg...)
Expand Down

0 comments on commit 1428c2e

Please sign in to comment.