diff --git a/CHANGELOG.md b/CHANGELOG.md index eceb41e0b84..7c964c2cccf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/CODEOWNERS b/CODEOWNERS index 80fd30e8b9c..7483ae0ea42 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -54,7 +54,7 @@ instrumentation/google.golang.org/grpc/otelgrpc/ @open-te instrumentation/host/ @open-telemetry/go-approvers @dmathieu instrumentation/net/http/httptrace/otelhttptrace/ @open-telemetry/go-approvers @dmathieu instrumentation/net/http/otelhttp/ @open-telemetry/go-approvers @dmathieu -instrumentation/runtime/ @open-telemetry/go-approvers @dmathieu +instrumentation/runtime/ @open-telemetry/go-approvers @dmathieu @dashpole processors/baggagecopy @open-telemetry/go-approvers @codeboten @MikeGoldsmith processors/minsev @open-telemetry/go-approvers @MrAlias diff --git a/config/config.go b/config/config.go index 16193946887..d8357b76ca2 100644 --- a/config/config.go +++ b/config/config.go @@ -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 ( @@ -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{ diff --git a/config/config_test.go b/config/config_test.go index f69a2dcdf9a..c6b33b66e12 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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...)