Skip to content

Commit

Permalink
Support a global MeterProvider in go.opentelemetry.io/otel (#3818)
Browse files Browse the repository at this point in the history
* Move ErrorHandler impl to internal

To avoid the import cycle, the otel/metric package needs to not import
otel. To achieve this, the error handling implementation is moved to the
otel/internal/global package where both can import the needed
functionality.

* Add global metric to go.opentelemetry.io/otel

* Crosslink and update to global metric in otel

* Add changes to changelog

* Set PR number in changelog

* Add global metric unit tests

* Rename MeterProivder() to GetMeterProivder()

* Add TODO to remove nolint comments
  • Loading branch information
MrAlias authored Mar 1, 2023
1 parent c39e625 commit 8139361
Show file tree
Hide file tree
Showing 33 changed files with 551 additions and 282 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Added

- Support global `MeterProvider` in `go.opentelemetry.io/otel`. (#3818)
- 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

- Dropped compatibility testing for [Go 1.18].
Expand All @@ -17,6 +24,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Handle empty environment variable as it they were not set. (#3764)

### Deprecated

- The `go.opentelemetry.io/otel/metric/global` package is deprecated.
Use `go.opentelemetry.io/otel` instead. (#3818)

### Removed

- The deprecated `go.opentelemetry.io/otel/metric/unit` package is removed. (#3814)
Expand Down
3 changes: 3 additions & 0 deletions bridge/opentracing/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace go.opentelemetry.io/otel/metric => ../../metric
3 changes: 3 additions & 0 deletions bridge/opentracing/test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.37.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
Expand All @@ -31,3 +32,5 @@ require (
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace go.opentelemetry.io/otel/metric => ../../../metric
3 changes: 3 additions & 0 deletions example/fib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
golang.org/x/sys v0.5.0 // indirect
)

Expand All @@ -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
3 changes: 3 additions & 0 deletions example/jaeger/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ require (
require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
golang.org/x/sys v0.5.0 // indirect
)

replace go.opentelemetry.io/otel/trace => ../../trace

replace go.opentelemetry.io/otel/metric => ../../metric
3 changes: 3 additions & 0 deletions example/namedtracer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ require (

require (
github.com/go-logr/logr v1.2.3 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
golang.org/x/sys v0.5.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
3 changes: 3 additions & 0 deletions example/otel-collector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
Expand All @@ -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
3 changes: 3 additions & 0 deletions example/passthrough/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
golang.org/x/sys v0.5.0 // indirect
)

Expand All @@ -22,3 +23,5 @@ replace (
)

replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace

replace go.opentelemetry.io/otel/metric => ../../metric
3 changes: 3 additions & 0 deletions example/zipkin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ require (
github.com/go-logr/logr v1.2.3 // 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.37.0 // indirect
golang.org/x/sys v0.5.0 // indirect
)

replace go.opentelemetry.io/otel/trace => ../../trace

replace go.opentelemetry.io/otel/metric => ../../metric
3 changes: 3 additions & 0 deletions exporters/jaeger/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.37.0 // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand All @@ -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
4 changes: 2 additions & 2 deletions exporters/otlp/otlpmetric/otlpmetricgrpc/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/otlpmetric/otlpmetricgrpc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.37.0
go.opentelemetry.io/otel/metric v0.37.0
go.opentelemetry.io/otel/sdk/metric v0.37.0
go.opentelemetry.io/proto/otlp v0.19.0
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f
Expand All @@ -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.37.0 // indirect
go.opentelemetry.io/otel/sdk v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
golang.org/x/net v0.7.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/otlpmetric/otlpmetrichttp/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/otlpmetric/otlpmetrichttp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.37.0
go.opentelemetry.io/otel/metric v0.37.0
go.opentelemetry.io/otel/sdk/metric v0.37.0
go.opentelemetry.io/proto/otlp v0.19.0
google.golang.org/protobuf v1.28.1
Expand All @@ -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.37.0 // indirect
go.opentelemetry.io/otel/sdk v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
golang.org/x/net v0.7.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions exporters/otlp/otlptrace/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.37.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
Expand All @@ -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
3 changes: 3 additions & 0 deletions exporters/otlp/otlptrace/otlptracegrpc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.37.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
Expand All @@ -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
3 changes: 3 additions & 0 deletions exporters/otlp/otlptrace/otlptracehttp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.37.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
Expand All @@ -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
3 changes: 3 additions & 0 deletions exporters/stdout/stdouttrace/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace go.opentelemetry.io/otel/trace => ../../../trace

replace go.opentelemetry.io/otel/metric => ../../../metric
3 changes: 3 additions & 0 deletions exporters/zipkin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.37.0 // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand All @@ -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 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.37.0
go.opentelemetry.io/otel/trace v1.14.0
)

Expand All @@ -17,3 +18,5 @@ require (
)

replace go.opentelemetry.io/otel/trace => ./trace

replace go.opentelemetry.io/otel/metric => ./metric
64 changes: 8 additions & 56 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,16 @@
package otel // import "go.opentelemetry.io/otel"

import (
"log"
"os"
"sync/atomic"
"unsafe"
"go.opentelemetry.io/otel/internal/global"
)

var (
// globalErrorHandler provides an ErrorHandler that can be used
// throughout an OpenTelemetry instrumented project. When a user
// specified ErrorHandler is registered (`SetErrorHandler`) all calls to
// `Handle` and will be delegated to the registered ErrorHandler.
globalErrorHandler = defaultErrorHandler()

// Compile-time check that delegator implements ErrorHandler.
_ ErrorHandler = (*delegator)(nil)
// Compile-time check that errLogger implements ErrorHandler.
_ ErrorHandler = (*errLogger)(nil)
// Compile-time check global.ErrDelegator implements ErrorHandler.
_ ErrorHandler = (*global.ErrDelegator)(nil)
// Compile-time check global.ErrLogger implements ErrorHandler.
_ ErrorHandler = (*global.ErrLogger)(nil)
)

type delegator struct {
delegate unsafe.Pointer
}

func (d *delegator) Handle(err error) {
d.getDelegate().Handle(err)
}

func (d *delegator) getDelegate() ErrorHandler {
return *(*ErrorHandler)(atomic.LoadPointer(&d.delegate))
}

// setDelegate sets the ErrorHandler delegate.
func (d *delegator) setDelegate(eh ErrorHandler) {
atomic.StorePointer(&d.delegate, unsafe.Pointer(&eh))
}

func defaultErrorHandler() *delegator {
d := &delegator{}
d.setDelegate(&errLogger{l: log.New(os.Stderr, "", log.LstdFlags)})
return d
}

// errLogger logs errors if no delegate is set, otherwise they are delegated.
type errLogger struct {
l *log.Logger
}

// Handle logs err if no delegate is set, otherwise it is delegated.
func (h *errLogger) Handle(err error) {
h.l.Print(err)
}

// GetErrorHandler returns the global ErrorHandler instance.
//
// The default ErrorHandler instance returned will log all errors to STDERR
Expand All @@ -76,21 +34,15 @@ func (h *errLogger) Handle(err error) {
//
// Subsequent calls to SetErrorHandler after the first will not forward errors
// to the new ErrorHandler for prior returned instances.
func GetErrorHandler() ErrorHandler {
return globalErrorHandler
}
func GetErrorHandler() ErrorHandler { return global.GetErrorHandler() }

// SetErrorHandler sets the global ErrorHandler to h.
//
// The first time this is called all ErrorHandler previously returned from
// GetErrorHandler will send errors to h instead of the default logging
// ErrorHandler. Subsequent calls will set the global ErrorHandler, but not
// delegate errors to h.
func SetErrorHandler(h ErrorHandler) {
globalErrorHandler.setDelegate(h)
}
func SetErrorHandler(h ErrorHandler) { global.SetErrorHandler(h) }

// Handle is a convenience function for ErrorHandler().Handle(err).
func Handle(err error) {
GetErrorHandler().Handle(err)
}
func Handle(err error) { global.Handle(err) }
Loading

0 comments on commit 8139361

Please sign in to comment.