From 17903bcdb6639836804ce0c1364814b59ddc5359 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Tue, 2 May 2023 11:15:39 -0700 Subject: [PATCH] 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 8dba38e02f16d29a315d2e6890d21f763677831a. * Add changes to changelog * Fix versions and go mod tidy * Run go-mod-tidy --- CHANGELOG.md | 12 +++ bridge/opentracing/go.mod | 3 + bridge/opentracing/test/go.mod | 3 + example/fib/go.mod | 3 + example/jaeger/go.mod | 3 + example/namedtracer/go.mod | 3 + example/otel-collector/go.mod | 3 + example/passthrough/go.mod | 3 + example/zipkin/go.mod | 3 + exporters/jaeger/go.mod | 3 + .../otlpmetric/otlpmetricgrpc/example_test.go | 4 +- .../otlp/otlpmetric/otlpmetricgrpc/go.mod | 2 +- .../otlpmetric/otlpmetrichttp/example_test.go | 4 +- .../otlp/otlpmetric/otlpmetrichttp/go.mod | 2 +- exporters/otlp/otlptrace/go.mod | 3 + exporters/otlp/otlptrace/otlptracegrpc/go.mod | 3 + exporters/otlp/otlptrace/otlptracehttp/go.mod | 3 + exporters/stdout/stdouttrace/go.mod | 3 + exporters/zipkin/go.mod | 3 + go.mod | 3 + .../global/instruments.go | 27 +++--- .../global/instruments_test.go | 0 {metric/internal => internal}/global/meter.go | 10 +-- .../global/meter_test.go | 2 +- .../global/meter_types_test.go | 2 +- internal/global/state.go | 45 +++++++++- internal/global/state_test.go | 58 +++++++++++++ internal/global/util_test.go | 2 + metric/global/metric.go => metric.go | 18 ++-- metric/example_test.go | 8 +- metric/internal/global/state.go | 68 --------------- metric/internal/global/state_test.go | 87 ------------------- .../global/metric_test.go => metric_test.go | 4 +- sdk/go.mod | 3 + sdk/metric/example_test.go | 4 +- sdk/metric/meter_test.go | 7 +- trace/go.mod | 2 + 37 files changed, 210 insertions(+), 206 deletions(-) rename {metric/internal => internal}/global/instruments.go (94%) rename {metric/internal => internal}/global/instruments_test.go (100%) rename {metric/internal => internal}/global/meter.go (98%) rename {metric/internal => internal}/global/meter_test.go (99%) rename {metric/internal => internal}/global/meter_types_test.go (98%) rename metric/global/metric.go => metric.go (69%) delete mode 100644 metric/internal/global/state.go delete mode 100644 metric/internal/global/state_test.go rename metric/global/metric_test.go => metric_test.go (93%) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1ea65e9ab4..40fb958cd33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/bridge/opentracing/go.mod b/bridge/opentracing/go.mod index 74034deeeb6..702d1494036 100644 --- a/bridge/opentracing/go.mod +++ b/bridge/opentracing/go.mod @@ -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 diff --git a/bridge/opentracing/test/go.mod b/bridge/opentracing/test/go.mod index 978bba3be78..a333b58894c 100644 --- a/bridge/opentracing/test/go.mod +++ b/bridge/opentracing/test/go.mod @@ -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 diff --git a/example/fib/go.mod b/example/fib/go.mod index bb6a3cc8add..0b3b6be5e76 100644 --- a/example/fib/go.mod +++ b/example/fib/go.mod @@ -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 diff --git a/example/jaeger/go.mod b/example/jaeger/go.mod index 1715c2742f9..02f7f9b92c9 100644 --- a/example/jaeger/go.mod +++ b/example/jaeger/go.mod @@ -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 diff --git a/example/namedtracer/go.mod b/example/namedtracer/go.mod index ad6b00abd2d..39307d9a832 100644 --- a/example/namedtracer/go.mod +++ b/example/namedtracer/go.mod @@ -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 diff --git a/example/otel-collector/go.mod b/example/otel-collector/go.mod index 3b2643148d3..e6eaa803177 100644 --- a/example/otel-collector/go.mod +++ b/example/otel-collector/go.mod @@ -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 diff --git a/example/passthrough/go.mod b/example/passthrough/go.mod index 9f55560dc07..2532acd779a 100644 --- a/example/passthrough/go.mod +++ b/example/passthrough/go.mod @@ -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 diff --git a/example/zipkin/go.mod b/example/zipkin/go.mod index 17706bb03dc..065691c0248 100644 --- a/example/zipkin/go.mod +++ b/example/zipkin/go.mod @@ -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 diff --git a/exporters/jaeger/go.mod b/exporters/jaeger/go.mod index 936c7c28177..e7a9c677c29 100644 --- a/exporters/jaeger/go.mod +++ b/exporters/jaeger/go.mod @@ -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 diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/example_test.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/example_test.go index f7630760678..8307b51fc69 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/example_test.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/example_test.go @@ -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. diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/go.mod b/exporters/otlp/otlpmetric/otlpmetricgrpc/go.mod index 0b504d1f698..d9e5f17dbf5 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/go.mod +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/go.mod @@ -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 diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/example_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/example_test.go index d3397167c70..398c834a5f1 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/example_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/example_test.go @@ -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. diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/go.mod b/exporters/otlp/otlpmetric/otlpmetrichttp/go.mod index 7668ea6dc95..b7301f20bdb 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/go.mod +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/go.mod @@ -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 diff --git a/exporters/otlp/otlptrace/go.mod b/exporters/otlp/otlptrace/go.mod index a9ddff0ecda..659f4a03007 100644 --- a/exporters/otlp/otlptrace/go.mod +++ b/exporters/otlp/otlptrace/go.mod @@ -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 diff --git a/exporters/otlp/otlptrace/otlptracegrpc/go.mod b/exporters/otlp/otlptrace/otlptracegrpc/go.mod index 7d6d881f445..a29a11772b9 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/go.mod +++ b/exporters/otlp/otlptrace/otlptracegrpc/go.mod @@ -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 diff --git a/exporters/otlp/otlptrace/otlptracehttp/go.mod b/exporters/otlp/otlptrace/otlptracehttp/go.mod index 33803beedfa..28f82125ead 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/go.mod +++ b/exporters/otlp/otlptrace/otlptracehttp/go.mod @@ -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 diff --git a/exporters/stdout/stdouttrace/go.mod b/exporters/stdout/stdouttrace/go.mod index 6abcb2da833..5af8e60d1eb 100644 --- a/exporters/stdout/stdouttrace/go.mod +++ b/exporters/stdout/stdouttrace/go.mod @@ -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 diff --git a/exporters/zipkin/go.mod b/exporters/zipkin/go.mod index 53892667f87..ead81625513 100644 --- a/exporters/zipkin/go.mod +++ b/exporters/zipkin/go.mod @@ -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 diff --git a/go.mod b/go.mod index af21177ff74..d2731a9ff1c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/metric/internal/global/instruments.go b/internal/global/instruments.go similarity index 94% rename from metric/internal/global/instruments.go rename to internal/global/instruments.go index a3b898bfd5b..a33eded872a 100644 --- a/metric/internal/global/instruments.go +++ b/internal/global/instruments.go @@ -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) diff --git a/metric/internal/global/instruments_test.go b/internal/global/instruments_test.go similarity index 100% rename from metric/internal/global/instruments_test.go rename to internal/global/instruments_test.go diff --git a/metric/internal/global/meter.go b/internal/global/meter.go similarity index 98% rename from metric/internal/global/meter.go rename to internal/global/meter.go index a00b99085c4..0097db478c6 100644 --- a/metric/internal/global/meter.go +++ b/internal/global/meter.go @@ -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 diff --git a/metric/internal/global/meter_test.go b/internal/global/meter_test.go similarity index 99% rename from metric/internal/global/meter_test.go rename to internal/global/meter_test.go index 5bae7acc453..b91f4fd57c8 100644 --- a/metric/internal/global/meter_test.go +++ b/internal/global/meter_test.go @@ -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" diff --git a/metric/internal/global/meter_types_test.go b/internal/global/meter_types_test.go similarity index 98% rename from metric/internal/global/meter_types_test.go rename to internal/global/meter_types_test.go index a91a26a4915..9929e12353d 100644 --- a/metric/internal/global/meter_types_test.go +++ b/internal/global/meter_types_test.go @@ -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" diff --git a/internal/global/state.go b/internal/global/state.go index 1ad38f828ec..7985005bcb6 100644 --- a/internal/global/state.go +++ b/internal/global/state.go @@ -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 +} diff --git a/internal/global/state_test.go b/internal/global/state_test.go index f26f9d8c5a4..93bf6b8aae2 100644 --- a/internal/global/state_test.go +++ b/internal/global/state_test.go @@ -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) }) + }) +} diff --git a/internal/global/util_test.go b/internal/global/util_test.go index d0bca92f6c6..bc88508184c 100644 --- a/internal/global/util_test.go +++ b/internal/global/util_test.go @@ -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{} }) } diff --git a/metric/global/metric.go b/metric.go similarity index 69% rename from metric/global/metric.go rename to metric.go index 2723df2925f..f955171951f 100644 --- a/metric/global/metric.go +++ b/metric.go @@ -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() } diff --git a/metric/example_test.go b/metric/example_test.go index 13cd22d48d8..c1f67a1af18 100644 --- a/metric/example_test.go +++ b/metric/example_test.go @@ -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") diff --git a/metric/internal/global/state.go b/metric/internal/global/state.go deleted file mode 100644 index 47c0d787d8a..00000000000 --- a/metric/internal/global/state.go +++ /dev/null @@ -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 -} diff --git a/metric/internal/global/state_test.go b/metric/internal/global/state_test.go deleted file mode 100644 index d714c60f4e6..00000000000 --- a/metric/internal/global/state_test.go +++ /dev/null @@ -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) }) - }) -} diff --git a/metric/global/metric_test.go b/metric_test.go similarity index 93% rename from metric/global/metric_test.go rename to metric_test.go index d319a0c2da4..5e6e48fb8a0 100644 --- a/metric/global/metric_test.go +++ b/metric_test.go @@ -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) } diff --git a/sdk/go.mod b/sdk/go.mod index ddef1c9c42d..f44f104ed17 100644 --- a/sdk/go.mod +++ b/sdk/go.mod @@ -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 diff --git a/sdk/metric/example_test.go b/sdk/metric/example_test.go index c0704509a26..0086dba7687 100644 --- a/sdk/metric/example_test.go +++ b/sdk/metric/example_test.go @@ -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 { diff --git a/sdk/metric/meter_test.go b/sdk/metric/meter_test.go index f68f345103e..723e5bffbd2 100644 --- a/sdk/metric/meter_test.go +++ b/sdk/metric/meter_test.go @@ -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") diff --git a/trace/go.mod b/trace/go.mod index 090c2a71295..555a3494fab 100644 --- a/trace/go.mod +++ b/trace/go.mod @@ -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