Skip to content

Commit

Permalink
Move metric-related public API out of otel/ pkg
Browse files Browse the repository at this point in the history
Move GetMeterProvider, Meter and SetMeterProvider to new package
otel/metric/global in the otel/metric module.

This will allow otel/ module to be released as v1.0.
  • Loading branch information
punya committed Feb 11, 2021
1 parent 59ac2a7 commit 420e00a
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Reverse order in which `Resource` attributes are merged, per change in spec. (#1501)
- Add tooling to maintain "replace" directives in go.mod files automatically.
- Create new modules: otel/metric, otel/trace, otel/oteltest, otel/sdk/export/metric, otel/sdk/metric
- Move metric-related public APIs from otel to otel/metric/global.

## [0.16.0] - 2020-01-13

Expand Down
5 changes: 3 additions & 2 deletions example/otel-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"go.opentelemetry.io/otel/exporters/otlp/otlpgrpc"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/propagation"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
Expand Down Expand Up @@ -85,7 +86,7 @@ func initProvider() func() {
// set global propagator to tracecontext (the default is no-op).
otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTracerProvider(tracerProvider)
otel.SetMeterProvider(cont.MeterProvider())
global.SetMeterProvider(cont.MeterProvider())
handleErr(cont.Start(context.Background()), "failed to start controller")

return func() {
Expand All @@ -104,7 +105,7 @@ func main() {
defer shutdown()

tracer := otel.Tracer("test-tracer")
meter := otel.Meter("test-meter")
meter := global.Meter("test-meter")

// labels represent additional key-value descriptors that can be bound to a
// metric observer or recorder.
Expand Down
6 changes: 3 additions & 3 deletions example/prom-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import (

"google.golang.org/grpc"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/metric/prometheus"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/exporters/otlp/otlpgrpc"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
Expand Down Expand Up @@ -85,7 +85,7 @@ func initMeter() {
log.Fatal(http.ListenAndServe(":17000", nil))
}()

otel.SetMeterProvider(cont.MeterProvider())
global.SetMeterProvider(cont.MeterProvider())

log.Println("Prometheus server running on :17000")
log.Println("Exporting OTLP to :30080")
Expand All @@ -98,7 +98,7 @@ func main() {
label.String("label1", "value1"),
}

meter := otel.Meter("ex.com/prom-collector")
meter := global.Meter("ex.com/prom-collector")
_ = metric.Must(meter).NewFloat64ValueObserver(
"randval",
func(_ context.Context, result metric.Float64ObserverResult) {
Expand Down
4 changes: 2 additions & 2 deletions example/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"sync"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/metric/prometheus"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
)

var (
Expand All @@ -48,7 +48,7 @@ func initMeter() {
func main() {
initMeter()

meter := otel.Meter("ex.com/basic")
meter := global.Meter("ex.com/basic")
observerLock := new(sync.RWMutex)
observerValueToReport := new(float64)
observerLabelsToReport := new([]label.KeyValue)
Expand Down
3 changes: 2 additions & 1 deletion exporters/metric/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/number"
export "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
Expand Down Expand Up @@ -142,7 +143,7 @@ func InstallNewPipeline(config Config, options ...controller.Option) (*Exporter,
if err != nil {
return nil, err
}
otel.SetMeterProvider(exp.MeterProvider())
global.SetMeterProvider(exp.MeterProvider())
return exp, nil
}

Expand Down
5 changes: 3 additions & 2 deletions exporters/otlp/otlpgrpc/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/exporters/otlp/otlpgrpc"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
Expand Down Expand Up @@ -187,7 +188,7 @@ func Example_withDifferentSignalCollectors() {
controller.WithPusher(exp),
controller.WithCollectPeriod(2*time.Second),
)
otel.SetMeterProvider(pusher.MeterProvider())
global.SetMeterProvider(pusher.MeterProvider())

if err := pusher.Start(ctx); err != nil {
log.Fatalf("could not start metric controoler: %v", err)
Expand All @@ -202,7 +203,7 @@ func Example_withDifferentSignalCollectors() {
}()

tracer := otel.Tracer("test-tracer")
meter := otel.Meter("test-meter")
meter := global.Meter("test-meter")

// Recorder metric example
counter := metric.Must(meter).
Expand Down
3 changes: 2 additions & 1 deletion exporters/stdout/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"go.opentelemetry.io/otel/exporters/stdout"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/trace"
)

Expand All @@ -36,7 +37,7 @@ var (
trace.WithInstrumentationVersion(instrumentationVersion),
)

meter = otel.GetMeterProvider().Meter(
meter = global.GetMeterProvider().Meter(
instrumentationName,
metric.WithInstrumentationVersion(instrumentationVersion),
)
Expand Down
3 changes: 2 additions & 1 deletion exporters/stdout/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/export/metric"
exporttrace "go.opentelemetry.io/otel/sdk/export/trace"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
Expand Down Expand Up @@ -92,6 +93,6 @@ func InstallNewPipeline(exportOpts []Option, pushOpts []controller.Option) (*con
return controller, err
}
otel.SetTracerProvider(tracerProvider)
otel.SetMeterProvider(controller.MeterProvider())
global.SetMeterProvider(controller.MeterProvider())
return controller, err
}
3 changes: 2 additions & 1 deletion internal/global/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/label"
metricglobal "go.opentelemetry.io/otel/metric/global"
)

func BenchmarkGlobalInt64CounterAddNoSDK(b *testing.B) {
Expand All @@ -29,7 +30,7 @@ func BenchmarkGlobalInt64CounterAddNoSDK(b *testing.B) {
// global no-op system against a registered SDK.
global.ResetForTest()
ctx := context.Background()
sdk := otel.Meter("test")
sdk := metricglobal.Meter("test")
labs := []label.KeyValue{label.String("A", "B")}
cnt := Must(sdk).NewInt64Counter("int64.counter")

Expand Down
30 changes: 15 additions & 15 deletions internal/global/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (

"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
metricglobal "go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/number"
"go.opentelemetry.io/otel/oteltest"
)
Expand All @@ -38,8 +38,8 @@ func TestDirect(t *testing.T) {
global.ResetForTest()

ctx := context.Background()
meter1 := otel.Meter("test1", metric.WithInstrumentationVersion("semver:v1.0.0"))
meter2 := otel.Meter("test2")
meter1 := metricglobal.Meter("test1", metric.WithInstrumentationVersion("semver:v1.0.0"))
meter2 := metricglobal.Meter("test2")
labels1 := []label.KeyValue{label.String("A", "B")}
labels2 := []label.KeyValue{label.String("C", "D")}
labels3 := []label.KeyValue{label.String("E", "F")}
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestDirect(t *testing.T) {
second.Record(ctx, 2, labels3...)

mock, provider := oteltest.NewMeterProvider()
otel.SetMeterProvider(provider)
metricglobal.SetMeterProvider(provider)

counter.Add(ctx, 1, labels1...)
valuerecorder.Record(ctx, 3, labels1...)
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestBound(t *testing.T) {
// Note: this test uses opposite Float64/Int64 number kinds
// vs. the above, to cover all the instruments.
ctx := context.Background()
glob := otel.Meter("test")
glob := metricglobal.Meter("test")
labels1 := []label.KeyValue{label.String("A", "B")}

counter := Must(glob).NewFloat64Counter("test.counter")
Expand All @@ -152,7 +152,7 @@ func TestBound(t *testing.T) {
boundM.Record(ctx, 2)

mock, provider := oteltest.NewMeterProvider()
otel.SetMeterProvider(provider)
metricglobal.SetMeterProvider(provider)

boundC.Add(ctx, 1)
boundM.Record(ctx, 3)
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestUnbind(t *testing.T) {
// Tests Unbind with SDK never installed.
global.ResetForTest()

glob := otel.Meter("test")
glob := metricglobal.Meter("test")
labels1 := []label.KeyValue{label.String("A", "B")}

counter := Must(glob).NewFloat64Counter("test.counter")
Expand All @@ -201,10 +201,10 @@ func TestUnbindThenRecordOne(t *testing.T) {
ctx := context.Background()
mock, provider := oteltest.NewMeterProvider()

meter := otel.Meter("test")
meter := metricglobal.Meter("test")
counter := Must(meter).NewInt64Counter("test.counter")
boundC := counter.Bind()
otel.SetMeterProvider(provider)
metricglobal.SetMeterProvider(provider)
boundC.Unbind()

require.NotPanics(t, func() {
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestErrorInDeferredConstructor(t *testing.T) {
global.ResetForTest()

ctx := context.Background()
meter := otel.GetMeterProvider().Meter("builtin")
meter := metricglobal.GetMeterProvider().Meter("builtin")

c1 := Must(meter).NewInt64Counter("test")
c2 := Must(meter).NewInt64Counter("test")
Expand All @@ -242,7 +242,7 @@ func TestErrorInDeferredConstructor(t *testing.T) {
sdk := &meterProviderWithConstructorError{provider}

require.Panics(t, func() {
otel.SetMeterProvider(sdk)
metricglobal.SetMeterProvider(sdk)
})

c1.Add(ctx, 1)
Expand All @@ -256,7 +256,7 @@ func TestImplementationIndirection(t *testing.T) {
// returns the implementation interface not the global, after
// registered.

meter1 := otel.Meter("test1")
meter1 := metricglobal.Meter("test1")

// Sync: no SDK yet
counter := Must(meter1).NewInt64Counter("interface.counter")
Expand All @@ -281,7 +281,7 @@ func TestImplementationIndirection(t *testing.T) {

// Register the SDK
_, provider := oteltest.NewMeterProvider()
otel.SetMeterProvider(provider)
metricglobal.SetMeterProvider(provider)

// Repeat the above tests

Expand All @@ -303,14 +303,14 @@ func TestImplementationIndirection(t *testing.T) {
func TestRecordBatchMock(t *testing.T) {
global.ResetForTest()

meter := otel.GetMeterProvider().Meter("builtin")
meter := metricglobal.GetMeterProvider().Meter("builtin")

counter := Must(meter).NewInt64Counter("test.counter")

meter.RecordBatch(context.Background(), nil, counter.Measurement(1))

mock, provider := oteltest.NewMeterProvider()
otel.SetMeterProvider(provider)
metricglobal.SetMeterProvider(provider)

meter.RecordBatch(context.Background(), nil, counter.Measurement(1))

Expand Down
2 changes: 1 addition & 1 deletion metric.go → metric/global/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package otel // import "go.opentelemetry.io/otel"
package global // import "go.opentelemetry.io/otel/metric/global"

import (
"go.opentelemetry.io/otel/internal/global"
Expand Down
2 changes: 1 addition & 1 deletion metric_test.go → metric/global/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package otel
package global

import (
"testing"
Expand Down
6 changes: 3 additions & 3 deletions sdk/metric/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"math/rand"
"testing"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
export "go.opentelemetry.io/otel/sdk/export/metric"
sdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/processor/processortest"
Expand Down Expand Up @@ -214,8 +214,8 @@ func BenchmarkGlobalInt64CounterAddWithSDK(b *testing.B) {
ctx := context.Background()
fix := newFixture(b)

sdk := otel.Meter("test")
otel.SetMeterProvider(fix)
sdk := global.Meter("test")
global.SetMeterProvider(fix)

labs := []label.KeyValue{label.String("A", "B")}
cnt := Must(sdk).NewInt64Counter("int64.sum")
Expand Down

0 comments on commit 420e00a

Please sign in to comment.