Skip to content

Commit

Permalink
Remove the deprecated otel/metric/global pkg (#3829)
Browse files Browse the repository at this point in the history
* Remove the deprecated `otel/metric/global` pkg

* Add changelog entry

* Update PR number in changelog

* Fix lint

---------

Co-authored-by: Chester Cheung <[email protected]>
  • Loading branch information
MrAlias and hanyuancheung authored Mar 7, 2023
1 parent 3015c86 commit 60f7d42
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 248 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
File renamed without changes.
10 changes: 2 additions & 8 deletions metric/internal/global/meter.go → internal/global/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.
//
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 43 additions & 2 deletions internal/global/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sync"
"sync/atomic"

"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
Expand All @@ -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.
Expand Down Expand Up @@ -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{}})
Expand All @@ -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
}
57 changes: 57 additions & 0 deletions internal/global/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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) })
})
}
2 changes: 2 additions & 0 deletions internal/global/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 1 addition & 9 deletions metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...)
}

Expand All @@ -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)
}
Loading

0 comments on commit 60f7d42

Please sign in to comment.