2022-03-22 17:33:13 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-07-05 17:35:21 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2022-03-22 17:33:13 +02:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package global // import "go.opentelemetry.io/otel/metric/internal/global"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-07 22:22:19 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/metric"
|
2022-03-22 17:33:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resetGlobalMeterProvider() {
|
|
|
|
globalMeterProvider = defaultMeterProvider()
|
|
|
|
delegateMeterOnce = sync.Once{}
|
|
|
|
}
|
|
|
|
|
2022-04-07 22:22:19 +02:00
|
|
|
type nonComparableMeterProvider struct {
|
|
|
|
metric.MeterProvider
|
|
|
|
|
|
|
|
nonComparable func() //nolint:structcheck,unused // This is not called.
|
|
|
|
}
|
|
|
|
|
2022-03-22 17:33:13 +02:00
|
|
|
func TestSetMeterProvider(t *testing.T) {
|
|
|
|
t.Cleanup(resetGlobalMeterProvider)
|
|
|
|
|
2022-03-31 20:16:51 +02:00
|
|
|
t.Run("Set With default is a noop", func(t *testing.T) {
|
2022-03-22 17:33:13 +02:00
|
|
|
resetGlobalMeterProvider()
|
2022-03-31 20:16:51 +02:00
|
|
|
SetMeterProvider(MeterProvider())
|
2022-03-22 17:33:13 +02:00
|
|
|
|
2022-03-31 20:16:51 +02:00
|
|
|
mp, ok := MeterProvider().(*meterProvider)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Global MeterProvider should be the default meter provider")
|
|
|
|
}
|
2022-03-22 17:33:13 +02:00
|
|
|
|
2022-03-31 20:16:51 +02:00
|
|
|
if mp.delegate != nil {
|
|
|
|
t.Fatal("meter provider should not delegate when setting itself")
|
|
|
|
}
|
2022-03-22 17:33:13 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("First Set() should replace the delegate", func(t *testing.T) {
|
|
|
|
resetGlobalMeterProvider()
|
|
|
|
|
2022-04-28 21:52:10 +02:00
|
|
|
SetMeterProvider(metric.NewNoopMeterProvider())
|
2022-03-22 17:33:13 +02:00
|
|
|
|
|
|
|
_, ok := MeterProvider().(*meterProvider)
|
|
|
|
if ok {
|
2022-03-31 20:16:51 +02:00
|
|
|
t.Fatal("Global MeterProvider was not changed")
|
2022-03-22 17:33:13 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Set() should delegate existing Meter Providers", func(t *testing.T) {
|
|
|
|
resetGlobalMeterProvider()
|
|
|
|
|
|
|
|
mp := MeterProvider()
|
|
|
|
|
2022-04-28 21:52:10 +02:00
|
|
|
SetMeterProvider(metric.NewNoopMeterProvider())
|
2022-03-22 17:33:13 +02:00
|
|
|
|
|
|
|
dmp := mp.(*meterProvider)
|
|
|
|
|
|
|
|
if dmp.delegate == nil {
|
2022-03-31 20:16:51 +02:00
|
|
|
t.Fatal("The delegated meter providers should have a delegate")
|
2022-03-22 17:33:13 +02:00
|
|
|
}
|
|
|
|
})
|
2022-04-07 22:22:19 +02:00
|
|
|
|
|
|
|
t.Run("non-comparable types should not panic", func(t *testing.T) {
|
|
|
|
resetGlobalMeterProvider()
|
|
|
|
|
|
|
|
mp := nonComparableMeterProvider{}
|
|
|
|
SetMeterProvider(mp)
|
|
|
|
assert.NotPanics(t, func() { SetMeterProvider(mp) })
|
|
|
|
})
|
2022-03-22 17:33:13 +02:00
|
|
|
}
|