From 1428c2e87d551f7c452b06abecd4ab100ec8375e Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Fri, 4 Oct 2024 06:01:23 -0700 Subject: [PATCH 1/2] config: return a noop SDK if disabled is set to true (#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 <223565+codeboten@users.noreply.github.com> --- CHANGELOG.md | 1 + config/config.go | 21 +++++++++++++++++---- config/config_test.go | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6019a59494..e7a7bb3da4e 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/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...) From 64a133654324d112918e98a53fb9f01c2712a479 Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Fri, 4 Oct 2024 13:12:45 -0400 Subject: [PATCH 2/2] Add dashpole as an owner of instrumentation/runtime (#6194) Addresses https://github.com/open-telemetry/opentelemetry-go-contrib/issues/6175#issuecomment-2391843925 --- CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CODEOWNERS b/CODEOWNERS index d0ff0051e1a..20a8c847c5c 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -55,7 +55,7 @@ instrumentation/gopkg.in/macaron.v1/otelmacaron/ @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