Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't panic when setting a provider to itself #2749

Merged
merged 19 commits into from Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Fixed
dmathieu marked this conversation as resolved.
Show resolved Hide resolved

- Don't panick anymore when setting a (Tracer|Meter)Provider or TextMapPropagator to itself (#2749)
dmathieu marked this conversation as resolved.
Show resolved Hide resolved

## [1.6.1] - 2022-03-28

### Fixed
Expand Down
15 changes: 6 additions & 9 deletions internal/global/state.go
Expand Up @@ -50,14 +50,12 @@ func SetTracerProvider(tp trace.TracerProvider) {
delegateTraceOnce.Do(func() {
current := TracerProvider()
if current == tp {
// Setting the provider to the prior default is nonsense, panic.
// Panic is acceptable because we are likely still early in the
// process lifetime.
panic("invalid TracerProvider, the global instance cannot be reinstalled")
// Setting the provider to the prior default results in a noop. Return
// early.
return
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
} else if def, ok := current.(*tracerProvider); ok {
def.setDelegate(tp)
}

})
globalTracer.Store(tracerProviderHolder{tp: tp})
}
Expand All @@ -73,10 +71,9 @@ func SetTextMapPropagator(p propagation.TextMapPropagator) {
// delegate to p.
delegateTextMapPropagatorOnce.Do(func() {
if current := TextMapPropagator(); current == p {
// Setting the provider to the prior default is nonsense, panic.
// Panic is acceptable because we are likely still early in the
// process lifetime.
panic("invalid TextMapPropagator, the global instance cannot be reinstalled")
// Setting the provider to the prior default results in a noop. Return
// early.
return
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
} else if def, ok := current.(*textMapPropagator); ok {
def.SetDelegate(p)
}
Expand Down
99 changes: 77 additions & 22 deletions internal/global/state_test.go
Expand Up @@ -12,36 +12,91 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package global_test
package global

import (
"testing"

"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)

func TestResetsOfGlobalsPanic(t *testing.T) {
global.ResetForTest()
tests := map[string]func(){
"SetTextMapPropagator": func() {
global.SetTextMapPropagator(global.TextMapPropagator())
},
"SetTracerProvider": func() {
global.SetTracerProvider(global.TracerProvider())
},
}

for name, test := range tests {
shouldPanic(t, name, test)
}
func TestSetTracerProvider(t *testing.T) {
t.Cleanup(ResetForTest)
pellared marked this conversation as resolved.
Show resolved Hide resolved

t.Run("Set With default is a noop", func(t *testing.T) {
ResetForTest()
SetTracerProvider(TracerProvider())

_, ok := TracerProvider().(*tracerProvider)
if !ok {
t.Error("Global Tracer Provider should be the default tracer provider")
return
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}
})

t.Run("First Set() should replace the delegate", func(t *testing.T) {
ResetForTest()

SetTracerProvider(trace.NewNoopTracerProvider())

_, ok := TracerProvider().(*tracerProvider)
if ok {
t.Error("Global Tracer Provider was not changed")
return
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}
})

t.Run("Set() should delegate existing Tracer Providers", func(t *testing.T) {
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
ResetForTest()

tp := TracerProvider()
SetTracerProvider(trace.NewNoopTracerProvider())

ntp := tp.(*tracerProvider)

if ntp.delegate == nil {
t.Error("The delegated tracer providers should have a delegate")
}
})
}

func shouldPanic(t *testing.T, name string, f func()) {
defer func() {
if r := recover(); r == nil {
t.Errorf("calling %s with default global did not panic", name)
func TestSetTextMapPropagator(t *testing.T) {
t.Cleanup(ResetForTest)

t.Run("Set With default is a noop", func(t *testing.T) {
ResetForTest()
SetTextMapPropagator(TextMapPropagator())

_, ok := TextMapPropagator().(*textMapPropagator)
if !ok {
t.Error("Global TextMap Propagator should be the default propagator")
return
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}
}()
})

f()
t.Run("First Set() should replace the delegate", func(t *testing.T) {
ResetForTest()

SetTextMapPropagator(propagation.TraceContext{})

_, ok := TextMapPropagator().(*textMapPropagator)
if ok {
t.Error("Global TextMap Propagator was not changed")
return
}
})

t.Run("Set() should delegate existing propagators", func(t *testing.T) {
ResetForTest()

p := TextMapPropagator()
SetTextMapPropagator(propagation.TraceContext{})

np := p.(*textMapPropagator)

if np.delegate == nil {
t.Error("The delegated TextMap propagators should have a delegate")
}
})
}
7 changes: 3 additions & 4 deletions metric/internal/global/state.go
Expand Up @@ -41,10 +41,9 @@ func SetMeterProvider(mp metric.MeterProvider) {
delegateMeterOnce.Do(func() {
current := MeterProvider()
if current == mp {
// Setting the provider to the prior default is nonsense, panic.
// Panic is acceptable because we are likely still early in the
// process lifetime.
panic("invalid MeterProvider, the global instance cannot be reinstalled")
// Setting the provider to the prior default results in a noop. Return
// early.
return
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
} else if def, ok := current.(*meterProvider); ok {
def.setDelegate(mp)
}
Expand Down
14 changes: 7 additions & 7 deletions metric/internal/global/state_test.go
Expand Up @@ -18,8 +18,6 @@ import (
"sync"
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/metric/nonrecording"
)

Expand All @@ -31,13 +29,15 @@ func resetGlobalMeterProvider() {
func TestSetMeterProvider(t *testing.T) {
t.Cleanup(resetGlobalMeterProvider)

t.Run("Set With default panics", func(t *testing.T) {
t.Run("Set With default is a noop", func(t *testing.T) {
resetGlobalMeterProvider()
SetMeterProvider(MeterProvider())

assert.Panics(t, func() {
SetMeterProvider(MeterProvider())
})

_, ok := MeterProvider().(*meterProvider)
if !ok {
t.Error("Global Meter Provider should be the default meter provider")
return
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
})

t.Run("First Set() should replace the delegate", func(t *testing.T) {
Expand Down