mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-11-24 08:22:25 +02:00
Remove the deprecated otel/metric/global
pkg (#3829)
* Remove the deprecated `otel/metric/global` pkg * Add changelog entry * Update PR number in changelog * Fix lint --------- Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
This commit is contained in:
parent
3015c86cfd
commit
60f7d42d1e
@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||
|
||||
- Avoid creating new objects on all calls to `WithDeferredSetup` and `SkipContextSetup` in OpenTracing bridge. (#3833)
|
||||
|
||||
### Removed
|
||||
|
||||
- The deprecated `go.opentelemetry.io/otel/metric/global` package is removed. (#3829)
|
||||
|
||||
## [1.15.0-rc.1/0.38.0-rc.1] 2023-03-01
|
||||
|
||||
This is a release candidate for the v1.15.0/v0.38.0 release.
|
||||
|
@ -12,14 +12,13 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package global // import "go.opentelemetry.io/otel/metric/internal/global"
|
||||
package global // import "go.opentelemetry.io/otel/internal/global"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
oGlob "go.opentelemetry.io/otel/internal/global"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/metric/instrument"
|
||||
)
|
||||
@ -44,7 +43,7 @@ var _ instrument.Float64ObservableCounter = (*afCounter)(nil)
|
||||
func (i *afCounter) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Float64ObservableCounter(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -72,7 +71,7 @@ var _ instrument.Float64ObservableUpDownCounter = (*afUpDownCounter)(nil)
|
||||
func (i *afUpDownCounter) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Float64ObservableUpDownCounter(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -100,7 +99,7 @@ var _ instrument.Float64ObservableGauge = (*afGauge)(nil)
|
||||
func (i *afGauge) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Float64ObservableGauge(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -128,7 +127,7 @@ var _ instrument.Int64ObservableCounter = (*aiCounter)(nil)
|
||||
func (i *aiCounter) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Int64ObservableCounter(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -156,7 +155,7 @@ var _ instrument.Int64ObservableUpDownCounter = (*aiUpDownCounter)(nil)
|
||||
func (i *aiUpDownCounter) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Int64ObservableUpDownCounter(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -184,7 +183,7 @@ var _ instrument.Int64ObservableGauge = (*aiGauge)(nil)
|
||||
func (i *aiGauge) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Int64ObservableGauge(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -212,7 +211,7 @@ var _ instrument.Float64Counter = (*sfCounter)(nil)
|
||||
func (i *sfCounter) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Float64Counter(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -238,7 +237,7 @@ var _ instrument.Float64UpDownCounter = (*sfUpDownCounter)(nil)
|
||||
func (i *sfUpDownCounter) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Float64UpDownCounter(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -264,7 +263,7 @@ var _ instrument.Float64Histogram = (*sfHistogram)(nil)
|
||||
func (i *sfHistogram) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Float64Histogram(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -290,7 +289,7 @@ var _ instrument.Int64Counter = (*siCounter)(nil)
|
||||
func (i *siCounter) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Int64Counter(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -316,7 +315,7 @@ var _ instrument.Int64UpDownCounter = (*siUpDownCounter)(nil)
|
||||
func (i *siUpDownCounter) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Int64UpDownCounter(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
||||
@ -342,7 +341,7 @@ var _ instrument.Int64Histogram = (*siHistogram)(nil)
|
||||
func (i *siHistogram) setDelegate(m metric.Meter) {
|
||||
ctr, err := m.Int64Histogram(i.name, i.opts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
return
|
||||
}
|
||||
i.delegate.Store(ctr)
|
@ -12,14 +12,13 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package global // import "go.opentelemetry.io/otel/metric/internal/global"
|
||||
package global // import "go.opentelemetry.io/otel/internal/global"
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
oGlob "go.opentelemetry.io/otel/internal/global"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/metric/instrument"
|
||||
)
|
||||
@ -35,11 +34,6 @@ type meterProvider struct {
|
||||
delegate metric.MeterProvider
|
||||
}
|
||||
|
||||
type il struct {
|
||||
name string
|
||||
version string
|
||||
}
|
||||
|
||||
// setDelegate configures p to delegate all MeterProvider functionality to
|
||||
// provider.
|
||||
//
|
||||
@ -334,7 +328,7 @@ func (c *registration) setDelegate(m metric.Meter) {
|
||||
|
||||
reg, err := m.RegisterCallback(c.function, insts...)
|
||||
if err != nil {
|
||||
oGlob.GetErrorHandler().Handle(err)
|
||||
GetErrorHandler().Handle(err)
|
||||
}
|
||||
|
||||
c.unreg = reg.Unregister
|
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package global // import "go.opentelemetry.io/otel/metric/internal/global"
|
||||
package global // import "go.opentelemetry.io/otel/internal/global"
|
||||
|
||||
import (
|
||||
"context"
|
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package global // import "go.opentelemetry.io/otel/metric/internal/global"
|
||||
package global // import "go.opentelemetry.io/otel/internal/global"
|
||||
|
||||
import (
|
||||
"context"
|
@ -19,6 +19,7 @@ import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
@ -31,14 +32,20 @@ type (
|
||||
propagatorsHolder struct {
|
||||
tm propagation.TextMapPropagator
|
||||
}
|
||||
|
||||
meterProviderHolder struct {
|
||||
mp metric.MeterProvider
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
globalTracer = defaultTracerValue()
|
||||
globalPropagators = defaultPropagatorsValue()
|
||||
globalTracer = defaultTracerValue()
|
||||
globalPropagators = defaultPropagatorsValue()
|
||||
globalMeterProvider = defaultMeterProvider()
|
||||
|
||||
delegateTraceOnce sync.Once
|
||||
delegateTextMapPropagatorOnce sync.Once
|
||||
delegateMeterOnce sync.Once
|
||||
)
|
||||
|
||||
// TracerProvider is the internal implementation for global.TracerProvider.
|
||||
@ -102,6 +109,34 @@ func SetTextMapPropagator(p propagation.TextMapPropagator) {
|
||||
globalPropagators.Store(propagatorsHolder{tm: p})
|
||||
}
|
||||
|
||||
// MeterProvider is the internal implementation for global.MeterProvider.
|
||||
func MeterProvider() metric.MeterProvider {
|
||||
return globalMeterProvider.Load().(meterProviderHolder).mp
|
||||
}
|
||||
|
||||
// SetMeterProvider is the internal implementation for global.SetMeterProvider.
|
||||
func SetMeterProvider(mp metric.MeterProvider) {
|
||||
current := MeterProvider()
|
||||
if _, cOk := current.(*meterProvider); cOk {
|
||||
if _, mpOk := mp.(*meterProvider); mpOk && current == mp {
|
||||
// Do not assign the default delegating MeterProvider to delegate
|
||||
// to itself.
|
||||
Error(
|
||||
errors.New("no delegate configured in meter provider"),
|
||||
"Setting meter provider to it's current value. No delegate will be configured",
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
delegateMeterOnce.Do(func() {
|
||||
if def, ok := current.(*meterProvider); ok {
|
||||
def.setDelegate(mp)
|
||||
}
|
||||
})
|
||||
globalMeterProvider.Store(meterProviderHolder{mp: mp})
|
||||
}
|
||||
|
||||
func defaultTracerValue() *atomic.Value {
|
||||
v := &atomic.Value{}
|
||||
v.Store(tracerProviderHolder{tp: &tracerProvider{}})
|
||||
@ -113,3 +148,9 @@ func defaultPropagatorsValue() *atomic.Value {
|
||||
v.Store(propagatorsHolder{tm: newTextMapPropagator()})
|
||||
return v
|
||||
}
|
||||
|
||||
func defaultMeterProvider() *atomic.Value {
|
||||
v := &atomic.Value{}
|
||||
v.Store(meterProviderHolder{mp: &meterProvider{}})
|
||||
return v
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
@ -29,6 +30,12 @@ type nonComparableTracerProvider struct {
|
||||
nonComparable func() //nolint:structcheck,unused // This is not called.
|
||||
}
|
||||
|
||||
type nonComparableMeterProvider struct {
|
||||
metric.MeterProvider
|
||||
|
||||
nonComparable func() //nolint:structcheck,unused // This is not called.
|
||||
}
|
||||
|
||||
func TestSetTracerProvider(t *testing.T) {
|
||||
t.Run("Set With default is a noop", func(t *testing.T) {
|
||||
ResetForTest(t)
|
||||
@ -125,3 +132,53 @@ func TestSetTextMapPropagator(t *testing.T) {
|
||||
assert.NotPanics(t, func() { SetTextMapPropagator(prop) })
|
||||
})
|
||||
}
|
||||
|
||||
func TestSetMeterProvider(t *testing.T) {
|
||||
t.Run("Set With default is a noop", func(t *testing.T) {
|
||||
ResetForTest(t)
|
||||
|
||||
SetMeterProvider(MeterProvider())
|
||||
|
||||
mp, ok := MeterProvider().(*meterProvider)
|
||||
if !ok {
|
||||
t.Fatal("Global MeterProvider should be the default meter provider")
|
||||
}
|
||||
|
||||
if mp.delegate != nil {
|
||||
t.Fatal("meter provider should not delegate when setting itself")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("First Set() should replace the delegate", func(t *testing.T) {
|
||||
ResetForTest(t)
|
||||
|
||||
SetMeterProvider(metric.NewNoopMeterProvider())
|
||||
|
||||
_, ok := MeterProvider().(*meterProvider)
|
||||
if ok {
|
||||
t.Fatal("Global MeterProvider was not changed")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Set() should delegate existing Meter Providers", func(t *testing.T) {
|
||||
ResetForTest(t)
|
||||
|
||||
mp := MeterProvider()
|
||||
|
||||
SetMeterProvider(metric.NewNoopMeterProvider())
|
||||
|
||||
dmp := mp.(*meterProvider)
|
||||
|
||||
if dmp.delegate == nil {
|
||||
t.Fatal("The delegated meter providers should have a delegate")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("non-comparable types should not panic", func(t *testing.T) {
|
||||
ResetForTest(t)
|
||||
|
||||
mp := nonComparableMeterProvider{}
|
||||
SetMeterProvider(mp)
|
||||
assert.NotPanics(t, func() { SetMeterProvider(mp) })
|
||||
})
|
||||
}
|
||||
|
@ -25,7 +25,9 @@ func ResetForTest(t testing.TB) {
|
||||
t.Cleanup(func() {
|
||||
globalTracer = defaultTracerValue()
|
||||
globalPropagators = defaultPropagatorsValue()
|
||||
globalMeterProvider = defaultMeterProvider()
|
||||
delegateTraceOnce = sync.Once{}
|
||||
delegateTextMapPropagatorOnce = sync.Once{}
|
||||
delegateMeterOnce = sync.Once{}
|
||||
})
|
||||
}
|
||||
|
10
metric.go
10
metric.go
@ -15,10 +15,8 @@
|
||||
package otel // import "go.opentelemetry.io/otel"
|
||||
|
||||
import (
|
||||
"go.opentelemetry.io/otel/internal/global"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
// TODO (#3819): Remove this disablement.
|
||||
// nolint: staticcheck // Temporary, while metric/global is deprecated.
|
||||
"go.opentelemetry.io/otel/metric/global"
|
||||
)
|
||||
|
||||
// Meter returns a Meter from the global MeterProvider. The name must be the
|
||||
@ -35,8 +33,6 @@ import (
|
||||
//
|
||||
// This is short for GetMeterProvider().Meter(name).
|
||||
func Meter(name string, opts ...metric.MeterOption) metric.Meter {
|
||||
// TODO (#3819): Remove this disablement.
|
||||
// nolint: staticcheck // Temporary, while metric/global is deprecated.
|
||||
return GetMeterProvider().Meter(name, opts...)
|
||||
}
|
||||
|
||||
@ -48,14 +44,10 @@ func Meter(name string, opts ...metric.MeterOption) metric.Meter {
|
||||
// created or will create, are recreated automatically from the new
|
||||
// GetMeterProvider.
|
||||
func GetMeterProvider() metric.MeterProvider {
|
||||
// TODO (#3819): Remove this disablement.
|
||||
// nolint: staticcheck // Temporary, while metric/global is deprecated.
|
||||
return global.MeterProvider()
|
||||
}
|
||||
|
||||
// SetMeterProvider registers mp as the global MeterProvider.
|
||||
func SetMeterProvider(mp metric.MeterProvider) {
|
||||
// TODO (#3819): Remove this disablement.
|
||||
// nolint: staticcheck // Temporary, while metric/global is deprecated.
|
||||
global.SetMeterProvider(mp)
|
||||
}
|
||||
|
@ -1,51 +0,0 @@
|
||||
// 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
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// 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 provides a global MeterProvider for OpenTelemetry.
|
||||
//
|
||||
// Deprecated: Use go.opentelemetry.io/otel instead.
|
||||
package global // import "go.opentelemetry.io/otel/metric/global"
|
||||
|
||||
import (
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/metric/internal/global"
|
||||
)
|
||||
|
||||
// Meter returns a Meter from the global MeterProvider. The
|
||||
// instrumentationName must be the name of the library providing
|
||||
// instrumentation. This name may be the same as the instrumented code only if
|
||||
// that code provides built-in instrumentation. If the instrumentationName is
|
||||
// empty, then a implementation defined default name will be used instead.
|
||||
//
|
||||
// This is short for MeterProvider().Meter(name).
|
||||
//
|
||||
// Deprecated: Use Meter from go.opentelemetry.io/otel instead.
|
||||
func Meter(instrumentationName string, opts ...metric.MeterOption) metric.Meter {
|
||||
return MeterProvider().Meter(instrumentationName, opts...)
|
||||
}
|
||||
|
||||
// MeterProvider returns the registered global meter provider.
|
||||
// If none is registered then a No-op MeterProvider is returned.
|
||||
//
|
||||
// Deprecated: Use MeterProvider from go.opentelemetry.io/otel instead.
|
||||
func MeterProvider() metric.MeterProvider {
|
||||
return global.MeterProvider()
|
||||
}
|
||||
|
||||
// SetMeterProvider registers `mp` as the global meter provider.
|
||||
//
|
||||
// Deprecated: Use SetMeterProvider from go.opentelemetry.io/otel instead.
|
||||
func SetMeterProvider(mp metric.MeterProvider) {
|
||||
global.SetMeterProvider(mp)
|
||||
}
|
@ -9,10 +9,7 @@ require (
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/go-logr/logr v1.2.3 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.15.0-rc.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
|
@ -1,11 +1,6 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
|
||||
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
|
@ -1,68 +0,0 @@
|
||||
// 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
|
||||
//
|
||||
// htmp://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// 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 (
|
||||
"errors"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"go.opentelemetry.io/otel/internal/global"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
var (
|
||||
globalMeterProvider = defaultMeterProvider()
|
||||
|
||||
delegateMeterOnce sync.Once
|
||||
)
|
||||
|
||||
type meterProviderHolder struct {
|
||||
mp metric.MeterProvider
|
||||
}
|
||||
|
||||
// MeterProvider is the internal implementation for global.MeterProvider.
|
||||
func MeterProvider() metric.MeterProvider {
|
||||
return globalMeterProvider.Load().(meterProviderHolder).mp
|
||||
}
|
||||
|
||||
// SetMeterProvider is the internal implementation for global.SetMeterProvider.
|
||||
func SetMeterProvider(mp metric.MeterProvider) {
|
||||
current := MeterProvider()
|
||||
if _, cOk := current.(*meterProvider); cOk {
|
||||
if _, mpOk := mp.(*meterProvider); mpOk && current == mp {
|
||||
// Do not assign the default delegating MeterProvider to delegate
|
||||
// to itself.
|
||||
global.Error(
|
||||
errors.New("no delegate configured in meter provider"),
|
||||
"Setting meter provider to it's current value. No delegate will be configured",
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
delegateMeterOnce.Do(func() {
|
||||
if def, ok := current.(*meterProvider); ok {
|
||||
def.setDelegate(mp)
|
||||
}
|
||||
})
|
||||
globalMeterProvider.Store(meterProviderHolder{mp: mp})
|
||||
}
|
||||
|
||||
func defaultMeterProvider() *atomic.Value {
|
||||
v := &atomic.Value{}
|
||||
v.Store(meterProviderHolder{mp: &meterProvider{}})
|
||||
return v
|
||||
}
|
@ -1,86 +0,0 @@
|
||||
// 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
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// 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"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
func resetGlobalMeterProvider() {
|
||||
globalMeterProvider = defaultMeterProvider()
|
||||
delegateMeterOnce = sync.Once{}
|
||||
}
|
||||
|
||||
type nonComparableMeterProvider struct {
|
||||
metric.MeterProvider
|
||||
|
||||
nonComparable func() //nolint:structcheck,unused // This is not called.
|
||||
}
|
||||
|
||||
func TestSetMeterProvider(t *testing.T) {
|
||||
t.Cleanup(resetGlobalMeterProvider)
|
||||
|
||||
t.Run("Set With default is a noop", func(t *testing.T) {
|
||||
resetGlobalMeterProvider()
|
||||
SetMeterProvider(MeterProvider())
|
||||
|
||||
mp, ok := MeterProvider().(*meterProvider)
|
||||
if !ok {
|
||||
t.Fatal("Global MeterProvider should be the default meter provider")
|
||||
}
|
||||
|
||||
if mp.delegate != nil {
|
||||
t.Fatal("meter provider should not delegate when setting itself")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("First Set() should replace the delegate", func(t *testing.T) {
|
||||
resetGlobalMeterProvider()
|
||||
|
||||
SetMeterProvider(metric.NewNoopMeterProvider())
|
||||
|
||||
_, ok := MeterProvider().(*meterProvider)
|
||||
if ok {
|
||||
t.Fatal("Global MeterProvider was not changed")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Set() should delegate existing Meter Providers", func(t *testing.T) {
|
||||
resetGlobalMeterProvider()
|
||||
|
||||
mp := MeterProvider()
|
||||
|
||||
SetMeterProvider(metric.NewNoopMeterProvider())
|
||||
|
||||
dmp := mp.(*meterProvider)
|
||||
|
||||
if dmp.delegate == nil {
|
||||
t.Fatal("The delegated meter providers should have a delegate")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("non-comparable types should not panic", func(t *testing.T) {
|
||||
resetGlobalMeterProvider()
|
||||
|
||||
mp := nonComparableMeterProvider{}
|
||||
SetMeterProvider(mp)
|
||||
assert.NotPanics(t, func() { SetMeterProvider(mp) })
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user