1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-11-24 08:22:25 +02:00

Revert "Move global metric back to otel/metric/global for minor release (#3986)" (#4039)

* Revert "Move global metric back to `otel/metric/global` for minor release (#3986)"

This reverts commit 8dba38e02f.

* Add changes to changelog

* Fix versions and go mod tidy

* Run go-mod-tidy
This commit is contained in:
Tyler Yahn 2023-05-02 11:15:39 -07:00 committed by GitHub
parent 5814858e4e
commit 17903bcdb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
37 changed files with 210 additions and 206 deletions

View File

@ -8,11 +8,23 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [Unreleased]
### Added
- Support global `MeterProvider` in `go.opentelemetry.io/otel`. (#4039)
- Use `Meter` for a `metric.Meter` from the global `metric.MeterProvider`.
- Use `GetMeterProivder` for a global `metric.MeterProvider`.
- Use `SetMeterProivder` to set the global `metric.MeterProvider`.
### Changed
- Move the `go.opentelemetry.io/otel/metric` module to the `stable-v1` module set.
This stages the metric API to be released as a stable module. (#4038)
### Removed
- The `go.opentelemetry.io/otel/metric/global` package is removed.
Use `go.opentelemetry.io/otel` instead. (#4039)
## [1.15.1/0.38.1] 2023-05-02
### Fixed

View File

@ -18,5 +18,8 @@ require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
replace go.opentelemetry.io/otel/metric => ../../metric

View File

@ -23,6 +23,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
go.opentelemetry.io/otel/trace v1.15.1 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
@ -31,3 +32,5 @@ require (
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
replace go.opentelemetry.io/otel/metric => ../../../metric

View File

@ -12,6 +12,7 @@ require (
require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
golang.org/x/sys v0.7.0 // indirect
)
@ -22,3 +23,5 @@ replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters
replace go.opentelemetry.io/otel/sdk => ../../sdk
replace go.opentelemetry.io/otel/trace => ../../trace
replace go.opentelemetry.io/otel/metric => ../../metric

View File

@ -17,8 +17,11 @@ require (
require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
go.opentelemetry.io/otel/trace v1.15.1 // indirect
golang.org/x/sys v0.7.0 // indirect
)
replace go.opentelemetry.io/otel/trace => ../../trace
replace go.opentelemetry.io/otel/metric => ../../metric

View File

@ -17,9 +17,12 @@ require (
require (
github.com/go-logr/logr v1.2.4 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
golang.org/x/sys v0.7.0 // indirect
)
replace go.opentelemetry.io/otel/trace => ../../trace
replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace
replace go.opentelemetry.io/otel/metric => ../../metric

View File

@ -23,6 +23,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.15.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.15.1 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
@ -38,3 +39,5 @@ replace go.opentelemetry.io/otel/exporters/otlp/otlptrace => ../../exporters/otl
replace go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc => ../../exporters/otlp/otlptrace/otlptracegrpc
replace go.opentelemetry.io/otel/exporters/otlp/internal/retry => ../../exporters/otlp/internal/retry
replace go.opentelemetry.io/otel/metric => ../../metric

View File

@ -12,6 +12,7 @@ require (
require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
golang.org/x/sys v0.7.0 // indirect
)
@ -22,3 +23,5 @@ replace (
)
replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace
replace go.opentelemetry.io/otel/metric => ../../metric

View File

@ -19,7 +19,10 @@ require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/openzipkin/zipkin-go v0.4.1 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
golang.org/x/sys v0.7.0 // indirect
)
replace go.opentelemetry.io/otel/trace => ../../trace
replace go.opentelemetry.io/otel/metric => ../../metric

View File

@ -16,6 +16,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
@ -25,3 +26,5 @@ replace go.opentelemetry.io/otel/trace => ../../trace
replace go.opentelemetry.io/otel => ../..
replace go.opentelemetry.io/otel/sdk => ../../sdk
replace go.opentelemetry.io/otel/metric => ../../metric

View File

@ -17,8 +17,8 @@ package otlpmetricgrpc_test
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/metric"
)
@ -35,7 +35,7 @@ func Example() {
panic(err)
}
}()
global.SetMeterProvider(meterProvider)
otel.SetMeterProvider(meterProvider)
// From here, the meterProvider can be used by instrumentation to collect
// telemetry.

View File

@ -9,7 +9,6 @@ require (
go.opentelemetry.io/otel v1.15.1
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.15.1
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.38.1
go.opentelemetry.io/otel/metric v0.38.1
go.opentelemetry.io/otel/sdk/metric v0.38.1
go.opentelemetry.io/proto/otlp v0.19.0
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f
@ -26,6 +25,7 @@ require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
go.opentelemetry.io/otel/sdk v1.15.1 // indirect
go.opentelemetry.io/otel/trace v1.15.1 // indirect
golang.org/x/net v0.8.0 // indirect

View File

@ -17,8 +17,8 @@ package otlpmetrichttp_test
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/metric"
)
@ -35,7 +35,7 @@ func Example() {
panic(err)
}
}()
global.SetMeterProvider(meterProvider)
otel.SetMeterProvider(meterProvider)
// From here, the meterProvider can be used by instrumentation to collect
// telemetry.

View File

@ -9,7 +9,6 @@ require (
go.opentelemetry.io/otel v1.15.1
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.15.1
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.38.1
go.opentelemetry.io/otel/metric v0.38.1
go.opentelemetry.io/otel/sdk/metric v0.38.1
go.opentelemetry.io/proto/otlp v0.19.0
google.golang.org/protobuf v1.30.0
@ -24,6 +23,7 @@ require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
go.opentelemetry.io/otel/sdk v1.15.1 // indirect
go.opentelemetry.io/otel/trace v1.15.1 // indirect
golang.org/x/net v0.8.0 // indirect

View File

@ -22,6 +22,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.8.0 // indirect
@ -36,3 +37,5 @@ replace go.opentelemetry.io/otel/sdk => ../../../sdk
replace go.opentelemetry.io/otel/trace => ../../../trace
replace go.opentelemetry.io/otel/exporters/otlp/internal/retry => ../internal/retry
replace go.opentelemetry.io/otel/metric => ../../../metric

View File

@ -23,6 +23,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
go.opentelemetry.io/otel/trace v1.15.1 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
@ -39,3 +40,5 @@ replace go.opentelemetry.io/otel/exporters/otlp/otlptrace => ../
replace go.opentelemetry.io/otel/trace => ../../../../trace
replace go.opentelemetry.io/otel/exporters/otlp/internal/retry => ../../internal/retry
replace go.opentelemetry.io/otel/metric => ../../../../metric

View File

@ -21,6 +21,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.8.0 // indirect
@ -38,3 +39,5 @@ replace go.opentelemetry.io/otel/sdk => ../../../../sdk
replace go.opentelemetry.io/otel/trace => ../../../../trace
replace go.opentelemetry.io/otel/exporters/otlp/internal/retry => ../../internal/retry
replace go.opentelemetry.io/otel/metric => ../../../../metric

View File

@ -19,8 +19,11 @@ require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
replace go.opentelemetry.io/otel/trace => ../../../trace
replace go.opentelemetry.io/otel/metric => ../../../metric

View File

@ -16,6 +16,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
@ -25,3 +26,5 @@ replace go.opentelemetry.io/otel/trace => ../../trace
replace go.opentelemetry.io/otel => ../..
replace go.opentelemetry.io/otel/sdk => ../../sdk
replace go.opentelemetry.io/otel/metric => ../../metric

3
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/go-logr/stdr v1.2.2
github.com/google/go-cmp v0.5.9
github.com/stretchr/testify v1.8.2
go.opentelemetry.io/otel/metric v0.38.1
go.opentelemetry.io/otel/trace v1.15.1
)
@ -17,3 +18,5 @@ require (
)
replace go.opentelemetry.io/otel/trace => ./trace
replace go.opentelemetry.io/otel/metric => ./metric

View File

@ -12,13 +12,12 @@
// 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"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/embedded"
)
@ -44,7 +43,7 @@ var _ metric.Float64ObservableCounter = (*afCounter)(nil)
func (i *afCounter) setDelegate(m metric.Meter) {
ctr, err := m.Float64ObservableCounter(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -73,7 +72,7 @@ var _ metric.Float64ObservableUpDownCounter = (*afUpDownCounter)(nil)
func (i *afUpDownCounter) setDelegate(m metric.Meter) {
ctr, err := m.Float64ObservableUpDownCounter(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -102,7 +101,7 @@ var _ metric.Float64ObservableGauge = (*afGauge)(nil)
func (i *afGauge) setDelegate(m metric.Meter) {
ctr, err := m.Float64ObservableGauge(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -131,7 +130,7 @@ var _ metric.Int64ObservableCounter = (*aiCounter)(nil)
func (i *aiCounter) setDelegate(m metric.Meter) {
ctr, err := m.Int64ObservableCounter(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -160,7 +159,7 @@ var _ metric.Int64ObservableUpDownCounter = (*aiUpDownCounter)(nil)
func (i *aiUpDownCounter) setDelegate(m metric.Meter) {
ctr, err := m.Int64ObservableUpDownCounter(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -189,7 +188,7 @@ var _ metric.Int64ObservableGauge = (*aiGauge)(nil)
func (i *aiGauge) setDelegate(m metric.Meter) {
ctr, err := m.Int64ObservableGauge(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -217,7 +216,7 @@ var _ metric.Float64Counter = (*sfCounter)(nil)
func (i *sfCounter) setDelegate(m metric.Meter) {
ctr, err := m.Float64Counter(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -243,7 +242,7 @@ var _ metric.Float64UpDownCounter = (*sfUpDownCounter)(nil)
func (i *sfUpDownCounter) setDelegate(m metric.Meter) {
ctr, err := m.Float64UpDownCounter(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -269,7 +268,7 @@ var _ metric.Float64Histogram = (*sfHistogram)(nil)
func (i *sfHistogram) setDelegate(m metric.Meter) {
ctr, err := m.Float64Histogram(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -295,7 +294,7 @@ var _ metric.Int64Counter = (*siCounter)(nil)
func (i *siCounter) setDelegate(m metric.Meter) {
ctr, err := m.Int64Counter(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -321,7 +320,7 @@ var _ metric.Int64UpDownCounter = (*siUpDownCounter)(nil)
func (i *siUpDownCounter) setDelegate(m metric.Meter) {
ctr, err := m.Int64UpDownCounter(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
@ -347,7 +346,7 @@ var _ metric.Int64Histogram = (*siHistogram)(nil)
func (i *siHistogram) setDelegate(m metric.Meter) {
ctr, err := m.Int64Histogram(i.name, i.opts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)

View File

@ -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"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/embedded"
)
@ -37,11 +36,6 @@ type meterProvider struct {
delegate metric.MeterProvider
}
type il struct {
name string
version string
}
// setDelegate configures p to delegate all MeterProvider functionality to
// provider.
//
@ -340,7 +334,7 @@ func (c *registration) setDelegate(m metric.Meter) {
reg, err := m.RegisterCallback(c.function, insts...)
if err != nil {
otel.Handle(err)
GetErrorHandler().Handle(err)
}
c.unreg = reg.Unregister

View File

@ -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"

View File

@ -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"

View File

@ -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
}

View File

@ -19,6 +19,8 @@ import (
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
@ -29,6 +31,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 +133,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(noop.NewMeterProvider())
_, 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(noop.NewMeterProvider())
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) })
})
}

View File

@ -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{}
})
}

View File

@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package global // import "go.opentelemetry.io/otel/metric/global"
package otel // import "go.opentelemetry.io/otel"
import (
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/internal/global"
)
// Meter returns a Meter from the global MeterProvider. The name must be the
@ -32,14 +32,18 @@ import (
// the new MeterProvider.
//
// This is short for GetMeterProvider().Meter(name).
func Meter(instrumentationName string, opts ...metric.MeterOption) metric.Meter {
return MeterProvider().Meter(instrumentationName, opts...)
func Meter(name string, opts ...metric.MeterOption) metric.Meter {
return GetMeterProvider().Meter(name, opts...)
}
// MeterProvider returns the registered global meter provider.
// GetMeterProvider returns the registered global meter provider.
//
// If no global MeterProvider has been registered, a No-op MeterProvider implementation is returned. When a global MeterProvider is registered for the first time, the returned MeterProvider, and all the Meters it has created or will create, are recreated automatically from the new MeterProvider.
func MeterProvider() metric.MeterProvider {
// If no global GetMeterProvider has been registered, a No-op GetMeterProvider
// implementation is returned. When a global GetMeterProvider is registered for
// the first time, the returned GetMeterProvider, and all the Meters it has
// created or will create, are recreated automatically from the new
// GetMeterProvider.
func GetMeterProvider() metric.MeterProvider {
return global.MeterProvider()
}

View File

@ -20,14 +20,14 @@ import (
"runtime"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
)
func ExampleMeter_synchronous() {
// Create a histogram using the global MeterProvider.
workDuration, err := global.Meter("go.opentelemetry.io/otel/metric#SyncExample").Int64Histogram(
workDuration, err := otel.Meter("go.opentelemetry.io/otel/metric#SyncExample").Int64Histogram(
"workDuration",
metric.WithUnit("ms"))
if err != nil {
@ -43,7 +43,7 @@ func ExampleMeter_synchronous() {
}
func ExampleMeter_asynchronous_single() {
meter := global.Meter("go.opentelemetry.io/otel/metric#AsyncExample")
meter := otel.Meter("go.opentelemetry.io/otel/metric#AsyncExample")
_, err := meter.Int64ObservableGauge(
"DiskUsage",
@ -73,7 +73,7 @@ func ExampleMeter_asynchronous_single() {
}
func ExampleMeter_asynchronous_multiple() {
meter := global.Meter("go.opentelemetry.io/otel/metric#MultiAsyncExample")
meter := otel.Meter("go.opentelemetry.io/otel/metric#MultiAsyncExample")
// This is just a sample of memory stats to record from the Memstats
heapAlloc, _ := meter.Int64ObservableUpDownCounter("heapAllocs")

View File

@ -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
}

View File

@ -1,87 +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"
"go.opentelemetry.io/otel/metric/noop"
)
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(noop.NewMeterProvider())
_, 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(noop.NewMeterProvider())
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) })
})
}

View File

@ -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/global"
package otel // import "go.opentelemetry.io/otel"
import (
"testing"
@ -38,6 +38,6 @@ func TestMultipleGlobalMeterProvider(t *testing.T) {
SetMeterProvider(&p1)
SetMeterProvider(p2)
got := MeterProvider()
got := GetMeterProvider()
assert.Equal(t, p2, got)
}

View File

@ -17,7 +17,10 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.38.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
replace go.opentelemetry.io/otel/trace => ../trace
replace go.opentelemetry.io/otel/metric => ../metric

View File

@ -18,7 +18,7 @@ import (
"context"
"log"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
@ -42,7 +42,7 @@ func Example() {
metric.WithResource(res),
metric.WithReader(reader),
)
global.SetMeterProvider(meterProvider)
otel.SetMeterProvider(meterProvider)
defer func() {
err := meterProvider.Shutdown(context.Background())
if err != nil {

View File

@ -29,7 +29,6 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
@ -630,7 +629,7 @@ func TestGlobalInstRegisterCallback(t *testing.T) {
otel.SetLogger(logr.New(l))
const mtrName = "TestGlobalInstRegisterCallback"
preMtr := global.Meter(mtrName)
preMtr := otel.Meter(mtrName)
preInt64Ctr, err := preMtr.Int64ObservableCounter("pre.int64.counter")
require.NoError(t, err)
preFloat64Ctr, err := preMtr.Float64ObservableCounter("pre.float64.counter")
@ -638,9 +637,9 @@ func TestGlobalInstRegisterCallback(t *testing.T) {
rdr := NewManualReader()
mp := NewMeterProvider(WithReader(rdr), WithResource(resource.Empty()))
global.SetMeterProvider(mp)
otel.SetMeterProvider(mp)
postMtr := global.Meter(mtrName)
postMtr := otel.Meter(mtrName)
postInt64Ctr, err := postMtr.Int64ObservableCounter("post.int64.counter")
require.NoError(t, err)
postFloat64Ctr, err := postMtr.Float64ObservableCounter("post.float64.counter")

View File

@ -15,3 +15,5 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
replace go.opentelemetry.io/otel/metric => ../metric