Skip to content

Commit 598d9f5

Browse files
authored
Fix global OTel provider leak in SDK tracing subtests
1 parent ed89d1f commit 598d9f5

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

internal/cmd/tracing_helpers_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/spf13/cobra"
1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
12+
"go.opentelemetry.io/otel"
13+
"go.opentelemetry.io/otel/trace/noop"
1214

1315
"github.com/github/gh-aw-mcpg/internal/config"
1416
"github.com/github/gh-aw-mcpg/internal/tracing"
@@ -91,6 +93,9 @@ func TestInitTracingProviderWithFallback(t *testing.T) {
9193
// HTTP OTLP exporters are lazily connected: otlptracehttp.New succeeds even
9294
// for unreachable endpoints, so InitProvider does not return an error and the
9395
// warn callback is never invoked. The provider is a real SDK (non-noop) instance.
96+
// Reset the global OTel provider to noop after the subtest to avoid leaking
97+
// background batcher goroutines and making other tests order-dependent.
98+
t.Cleanup(func() { otel.SetTracerProvider(noop.NewTracerProvider()) })
9499
var warnCalled bool
95100
cfg := &config.TracingConfig{
96101
Endpoint: "https://127.0.0.1:1/does-not-exist",
@@ -129,6 +134,9 @@ func TestShutdownTracingProviderWithTimeout(t *testing.T) {
129134

130135
t.Run("sdk provider shuts down cleanly", func(t *testing.T) {
131136
// HTTP OTLP exporters are lazy; construction succeeds even for unreachable endpoints.
137+
// Reset the global OTel provider to noop after the subtest so that a shut-down
138+
// provider is not left as the global, which would make later tests order-dependent.
139+
t.Cleanup(func() { otel.SetTracerProvider(noop.NewTracerProvider()) })
132140
provider, err := tracing.InitProvider(context.Background(), &config.TracingConfig{
133141
Endpoint: "http://127.0.0.1:14318",
134142
})

0 commit comments

Comments
 (0)