1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-11-28 08:38:51 +02:00

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

* Revert "Remove the deprecated `otel/metric/global` pkg (#3829)"

This reverts commit 60f7d42d1e.

* Revert "Support a global MeterProvider in `go.opentelemetry.io/otel` (#3818)"

This reverts commit 813936187e.

* Remove top level metric global

* Add change to changelog
This commit is contained in:
Tyler Yahn 2023-04-13 07:39:39 -07:00 committed by GitHub
parent 51345570a0
commit 8dba38e02f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
37 changed files with 207 additions and 198 deletions

View File

@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Move No-Op implementation from `go.opentelemetry.io/otel/metric` into its own package `go.opentelemetry.io/otel/metric/noop`. (#3941)
- `metric.NewNoopMeterProvider` is replaced with `noop.NewMeterProvider`
- Wrap `UploadMetrics` error in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/` to improve error message when encountering generic grpc errors. (#3974)
- Move global metric back to `go.opentelemetry.io/otel/metric/global` from `go.opentelemetry.io/otel`. (#3986)
### Fixed

View File

@ -18,8 +18,5 @@ 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 v1.15.0-rc.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
replace go.opentelemetry.io/otel/metric => ../../metric

View File

@ -23,7 +23,6 @@ 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 v1.15.0-rc.2 // indirect
go.opentelemetry.io/otel/trace v1.15.0-rc.2 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
@ -32,5 +31,3 @@ 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,7 +12,6 @@ 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 v1.15.0-rc.2 // indirect
golang.org/x/sys v0.7.0 // indirect
)
@ -23,5 +22,3 @@ 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,11 +17,8 @@ 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 v1.15.0-rc.2 // indirect
go.opentelemetry.io/otel/trace v1.15.0-rc.2 // 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,12 +17,9 @@ require (
require (
github.com/go-logr/logr v1.2.4 // indirect
go.opentelemetry.io/otel/metric v1.15.0-rc.2 // 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,7 +23,6 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.15.0-rc.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.15.0-rc.2 // indirect
go.opentelemetry.io/otel/metric v1.15.0-rc.2 // 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
@ -39,5 +38,3 @@ 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,7 +12,6 @@ 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 v1.15.0-rc.2 // indirect
golang.org/x/sys v0.7.0 // indirect
)
@ -23,5 +22,3 @@ replace (
)
replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace
replace go.opentelemetry.io/otel/metric => ../../metric

View File

@ -19,10 +19,7 @@ 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 v1.15.0-rc.2 // 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,7 +16,6 @@ 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 v1.15.0-rc.2 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
@ -26,5 +25,3 @@ 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)
}
}()
otel.SetMeterProvider(meterProvider)
global.SetMeterProvider(meterProvider)
// From here, the meterProvider can be used by instrumentation to collect
// telemetry.

View File

@ -9,6 +9,7 @@ require (
go.opentelemetry.io/otel v1.15.0-rc.2
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.15.0-rc.2
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.38.0-rc.2
go.opentelemetry.io/otel/metric v1.15.0-rc.2
go.opentelemetry.io/otel/sdk/metric v0.38.0-rc.2
go.opentelemetry.io/proto/otlp v0.19.0
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f
@ -25,7 +26,6 @@ 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 v1.15.0-rc.2 // indirect
go.opentelemetry.io/otel/sdk v1.15.0-rc.2 // indirect
go.opentelemetry.io/otel/trace v1.15.0-rc.2 // 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)
}
}()
otel.SetMeterProvider(meterProvider)
global.SetMeterProvider(meterProvider)
// From here, the meterProvider can be used by instrumentation to collect
// telemetry.

View File

@ -9,6 +9,7 @@ require (
go.opentelemetry.io/otel v1.15.0-rc.2
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.15.0-rc.2
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.38.0-rc.2
go.opentelemetry.io/otel/metric v1.15.0-rc.2
go.opentelemetry.io/otel/sdk/metric v0.38.0-rc.2
go.opentelemetry.io/proto/otlp v0.19.0
google.golang.org/protobuf v1.30.0
@ -23,7 +24,6 @@ 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 v1.15.0-rc.2 // indirect
go.opentelemetry.io/otel/sdk v1.15.0-rc.2 // indirect
go.opentelemetry.io/otel/trace v1.15.0-rc.2 // indirect
golang.org/x/net v0.8.0 // indirect

View File

@ -22,7 +22,6 @@ 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 v1.15.0-rc.2 // 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
@ -37,5 +36,3 @@ 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,7 +23,6 @@ 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 v1.15.0-rc.2 // indirect
go.opentelemetry.io/otel/trace v1.15.0-rc.2 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.7.0 // indirect
@ -40,5 +39,3 @@ 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,7 +21,6 @@ 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 v1.15.0-rc.2 // 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
@ -39,5 +38,3 @@ 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,11 +19,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 v1.15.0-rc.2 // 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,7 +16,6 @@ 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 v1.15.0-rc.2 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
@ -26,5 +25,3 @@ 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,7 +7,6 @@ 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 v1.15.0-rc.2
go.opentelemetry.io/otel/trace v1.15.0-rc.2
)
@ -18,5 +17,3 @@ require (
)
replace go.opentelemetry.io/otel/trace => ./trace
replace go.opentelemetry.io/otel/metric => ./metric

View File

@ -19,7 +19,6 @@ import (
"sync"
"sync/atomic"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
@ -32,20 +31,14 @@ type (
propagatorsHolder struct {
tm propagation.TextMapPropagator
}
meterProviderHolder struct {
mp metric.MeterProvider
}
)
var (
globalTracer = defaultTracerValue()
globalPropagators = defaultPropagatorsValue()
globalMeterProvider = defaultMeterProvider()
globalTracer = defaultTracerValue()
globalPropagators = defaultPropagatorsValue()
delegateTraceOnce sync.Once
delegateTextMapPropagatorOnce sync.Once
delegateMeterOnce sync.Once
)
// TracerProvider is the internal implementation for global.TracerProvider.
@ -109,34 +102,6 @@ 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{}})
@ -148,9 +113,3 @@ 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,8 +19,6 @@ 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"
)
@ -31,12 +29,6 @@ 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)
@ -133,53 +125,3 @@ 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,9 +25,7 @@ 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

@ -20,15 +20,15 @@ import (
"runtime"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
)
func ExampleMeter_synchronous() {
// Create a histogram using the global MeterProvider.
workDuration, err := otel.Meter("go.opentelemetry.io/otel/metric#SyncExample").Int64Histogram(
workDuration, err := global.Meter("go.opentelemetry.io/otel/metric#SyncExample").Int64Histogram(
"workDuration",
instrument.WithUnit("ms"))
if err != nil {
@ -44,7 +44,7 @@ func ExampleMeter_synchronous() {
}
func ExampleMeter_asynchronous_single() {
meter := otel.Meter("go.opentelemetry.io/otel/metric#AsyncExample")
meter := global.Meter("go.opentelemetry.io/otel/metric#AsyncExample")
_, err := meter.Int64ObservableGauge(
"DiskUsage",
@ -74,7 +74,7 @@ func ExampleMeter_asynchronous_single() {
}
func ExampleMeter_asynchronous_multiple() {
meter := otel.Meter("go.opentelemetry.io/otel/metric#MultiAsyncExample")
meter := global.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

@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package otel // import "go.opentelemetry.io/otel"
package global // import "go.opentelemetry.io/otel/metric/global"
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,18 +32,14 @@ import (
// the new MeterProvider.
//
// This is short for GetMeterProvider().Meter(name).
func Meter(name string, opts ...metric.MeterOption) metric.Meter {
return GetMeterProvider().Meter(name, opts...)
func Meter(instrumentationName string, opts ...metric.MeterOption) metric.Meter {
return MeterProvider().Meter(instrumentationName, opts...)
}
// GetMeterProvider returns the registered global meter provider.
// MeterProvider returns the registered global meter provider.
//
// 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 {
// 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 {
return global.MeterProvider()
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package otel // import "go.opentelemetry.io/otel"
package global // import "go.opentelemetry.io/otel/metric/global"
import (
"testing"
@ -38,6 +38,6 @@ func TestMultipleGlobalMeterProvider(t *testing.T) {
SetMeterProvider(&p1)
SetMeterProvider(p2)
got := GetMeterProvider()
got := MeterProvider()
assert.Equal(t, p2, got)
}

View File

@ -12,12 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package global // import "go.opentelemetry.io/otel/internal/global"
package global // import "go.opentelemetry.io/otel/metric/internal/global"
import (
"context"
"sync/atomic"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/embedded"
@ -45,7 +46,7 @@ var _ instrument.Float64ObservableCounter = (*afCounter)(nil)
func (i *afCounter) setDelegate(m metric.Meter) {
ctr, err := m.Float64ObservableCounter(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -74,7 +75,7 @@ var _ instrument.Float64ObservableUpDownCounter = (*afUpDownCounter)(nil)
func (i *afUpDownCounter) setDelegate(m metric.Meter) {
ctr, err := m.Float64ObservableUpDownCounter(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -103,7 +104,7 @@ var _ instrument.Float64ObservableGauge = (*afGauge)(nil)
func (i *afGauge) setDelegate(m metric.Meter) {
ctr, err := m.Float64ObservableGauge(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -132,7 +133,7 @@ var _ instrument.Int64ObservableCounter = (*aiCounter)(nil)
func (i *aiCounter) setDelegate(m metric.Meter) {
ctr, err := m.Int64ObservableCounter(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -161,7 +162,7 @@ var _ instrument.Int64ObservableUpDownCounter = (*aiUpDownCounter)(nil)
func (i *aiUpDownCounter) setDelegate(m metric.Meter) {
ctr, err := m.Int64ObservableUpDownCounter(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -190,7 +191,7 @@ var _ instrument.Int64ObservableGauge = (*aiGauge)(nil)
func (i *aiGauge) setDelegate(m metric.Meter) {
ctr, err := m.Int64ObservableGauge(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -218,7 +219,7 @@ var _ instrument.Float64Counter = (*sfCounter)(nil)
func (i *sfCounter) setDelegate(m metric.Meter) {
ctr, err := m.Float64Counter(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -244,7 +245,7 @@ var _ instrument.Float64UpDownCounter = (*sfUpDownCounter)(nil)
func (i *sfUpDownCounter) setDelegate(m metric.Meter) {
ctr, err := m.Float64UpDownCounter(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -270,7 +271,7 @@ var _ instrument.Float64Histogram = (*sfHistogram)(nil)
func (i *sfHistogram) setDelegate(m metric.Meter) {
ctr, err := m.Float64Histogram(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -296,7 +297,7 @@ var _ instrument.Int64Counter = (*siCounter)(nil)
func (i *siCounter) setDelegate(m metric.Meter) {
ctr, err := m.Int64Counter(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -322,7 +323,7 @@ var _ instrument.Int64UpDownCounter = (*siUpDownCounter)(nil)
func (i *siUpDownCounter) setDelegate(m metric.Meter) {
ctr, err := m.Int64UpDownCounter(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)
@ -348,7 +349,7 @@ var _ instrument.Int64Histogram = (*siHistogram)(nil)
func (i *siHistogram) setDelegate(m metric.Meter) {
ctr, err := m.Int64Histogram(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.Handle(err)
return
}
i.delegate.Store(ctr)

View File

@ -12,13 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package global // import "go.opentelemetry.io/otel/internal/global"
package global // import "go.opentelemetry.io/otel/metric/internal/global"
import (
"container/list"
"sync"
"sync/atomic"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/embedded"
"go.opentelemetry.io/otel/metric/instrument"
@ -37,6 +38,11 @@ type meterProvider struct {
delegate metric.MeterProvider
}
type il struct {
name string
version string
}
// setDelegate configures p to delegate all MeterProvider functionality to
// provider.
//
@ -335,7 +341,7 @@ func (c *registration) setDelegate(m metric.Meter) {
reg, err := m.RegisterCallback(c.function, insts...)
if err != nil {
GetErrorHandler().Handle(err)
otel.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/internal/global"
package global // import "go.opentelemetry.io/otel/metric/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/internal/global"
package global // import "go.opentelemetry.io/otel/metric/internal/global"
import (
"context"

View File

@ -0,0 +1,68 @@
// 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

@ -0,0 +1,87 @@
// 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

@ -17,10 +17,7 @@ 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 v1.15.0-rc.2 // 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"
"go.opentelemetry.io/otel/metric/global"
"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),
)
otel.SetMeterProvider(meterProvider)
global.SetMeterProvider(meterProvider)
defer func() {
err := meterProvider.Shutdown(context.Background())
if err != nil {

View File

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

View File

@ -15,5 +15,3 @@ 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