Skip to content

Commit

Permalink
Bring back the metric global package (#2764)
Browse files Browse the repository at this point in the history
* This PR brings back the metric global package
Closes: 2752

Signed-off-by: Brad Topol <btopol@us.ibm.com>

* updated CHANGELOG.md

Signed-off-by: Brad Topol <btopol@us.ibm.com>

* reformatted CHANGELOG.md

Signed-off-by: Brad Topol <btopol@us.ibm.com>

* Updated change log and removed unnecessary variable declaration

Signed-off-by: Brad Topol <btopol@us.ibm.com>
  • Loading branch information
bradtopol committed Apr 5, 2022
1 parent b7a5c1a commit c91da41
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Added

- The metrics global package was added back into several test files. (#2764)
- The `Meter` function is added back to the `go.opentelemetry.io/otel/metric/global` package.
This function is a convenience function equivalent to calling `global.MeterProvider().Meter(...)`. (#2750)

Expand Down
15 changes: 5 additions & 10 deletions example/prometheus/main.go
Expand Up @@ -24,7 +24,7 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
Expand All @@ -35,9 +35,6 @@ import (

var (
lemonsKey = attribute.Key("ex.com/lemons")

// TODO Bring back Global package
meterProvider metric.MeterProvider
)

func initMeter() {
Expand All @@ -57,9 +54,8 @@ func initMeter() {
if err != nil {
log.Panicf("failed to initialize prometheus exporter %v", err)
}
// TODO Bring back Global package
// global.SetMeterProvider(exporter.MeterProvider())
meterProvider = exporter.MeterProvider()

global.SetMeterProvider(exporter.MeterProvider())

http.HandleFunc("/", exporter.ServeHTTP)
go func() {
Expand All @@ -72,9 +68,8 @@ func initMeter() {
func main() {
initMeter()

// TODO Bring back Global package
// meter := global.Meter("ex.com/basic")
meter := meterProvider.Meter("ex.com/basic")
meter := global.Meter("ex.com/basic")

observerLock := new(sync.RWMutex)
observerValueToReport := new(float64)
observerLabelsToReport := new([]attribute.KeyValue)
Expand Down
31 changes: 13 additions & 18 deletions exporters/otlp/otlpmetric/otlpmetricgrpc/example_test.go
Expand Up @@ -24,6 +24,7 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
Expand Down Expand Up @@ -53,11 +54,11 @@ func Example_insecure() {
controller.WithExporter(exp),
controller.WithCollectPeriod(2*time.Second),
)
// TODO Bring back Global package
// global.SetMeterProvider(pusher)

global.SetMeterProvider(pusher)

if err := pusher.Start(ctx); err != nil {
log.Fatalf("could not start metric controoler: %v", err)
log.Fatalf("could not start metric controller: %v", err)
}
defer func() {
ctx, cancel := context.WithTimeout(ctx, time.Second)
Expand All @@ -68,9 +69,7 @@ func Example_insecure() {
}
}()

// TODO Bring Back Global package
// meter := global.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
meter := pusher.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
meter := global.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")

// Recorder metric example

Expand Down Expand Up @@ -115,11 +114,11 @@ func Example_withTLS() {
controller.WithExporter(exp),
controller.WithCollectPeriod(2*time.Second),
)
// TODO Bring back Global package
// global.SetMeterProvider(pusher)

global.SetMeterProvider(pusher)

if err := pusher.Start(ctx); err != nil {
log.Fatalf("could not start metric controoler: %v", err)
log.Fatalf("could not start metric controller: %v", err)
}

defer func() {
Expand All @@ -131,9 +130,7 @@ func Example_withTLS() {
}
}()

// TODO Bring back Global package
// meter := global.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
meter := pusher.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
meter := global.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")

// Recorder metric example
counter, err := meter.SyncFloat64().Counter("an_important_metric", instrument.WithDescription("Measures the cumulative epicness of the app"))
Expand Down Expand Up @@ -174,11 +171,11 @@ func Example_withDifferentSignalCollectors() {
controller.WithExporter(exp),
controller.WithCollectPeriod(2*time.Second),
)
// TODO Bring back Global package
// global.SetMeterProvider(pusher)

global.SetMeterProvider(pusher)

if err := pusher.Start(ctx); err != nil {
log.Fatalf("could not start metric controoler: %v", err)
log.Fatalf("could not start metric controller: %v", err)
}
defer func() {
ctx, cancel := context.WithTimeout(ctx, time.Second)
Expand All @@ -189,9 +186,7 @@ func Example_withDifferentSignalCollectors() {
}
}()

// TODO Bring back Global package
// meter := global.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
meter := pusher.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
meter := global.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")

// Recorder metric example
counter, err := meter.SyncFloat64().Counter("an_important_metric", instrument.WithDescription("Measures the cumulative epicness of the app"))
Expand Down
14 changes: 4 additions & 10 deletions exporters/stdout/stdoutmetric/example_test.go
Expand Up @@ -21,6 +21,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
Expand All @@ -33,13 +34,6 @@ const (
)

var (
// TODO Bring back Global package
// meter = global.GetMeterProvider().Meter(
// instrumentationName,
// metric.WithInstrumentationVersion(instrumentationVersion),
// )
meter metric.Meter

loopCounter syncint64.Counter
paramValue syncint64.Histogram

Expand Down Expand Up @@ -82,9 +76,9 @@ func InstallExportPipeline(ctx context.Context) func() {
if err = pusher.Start(ctx); err != nil {
log.Fatalf("starting push controller: %v", err)
}
// TODO Bring back Global package
// global.SetMeterProvider(pusher)
meter = pusher.Meter(instrumentationName, metric.WithInstrumentationVersion(instrumentationVersion))

global.SetMeterProvider(pusher)
meter := global.Meter(instrumentationName, metric.WithInstrumentationVersion(instrumentationVersion))

loopCounter, err = meter.SyncInt64().Counter("function.loops")
if err != nil {
Expand Down
31 changes: 15 additions & 16 deletions sdk/metric/benchmark_test.go
Expand Up @@ -22,6 +22,7 @@ import (

"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/metric/instrument/syncfloat64"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
Expand Down Expand Up @@ -181,27 +182,25 @@ func BenchmarkIterator_16(b *testing.B) {

// Counters

// TODO readd global
func BenchmarkGlobalInt64CounterAddWithSDK(b *testing.B) {
// Compare with BenchmarkInt64CounterAdd() to see overhead of global
// package. This is in the SDK to avoid the API from depending on the
// SDK.
ctx := context.Background()
fix := newFixture(b)

// func BenchmarkGlobalInt64CounterAddWithSDK(b *testing.B) {
// // Compare with BenchmarkInt64CounterAdd() to see overhead of global
// // package. This is in the SDK to avoid the API from depending on the
// // SDK.
// ctx := context.Background()
// fix := newFixture(b)
global.SetMeterProvider(fix)

// sdk := global.Meter("test")
// global.SetMeterProvider(fix)
labs := []attribute.KeyValue{attribute.String("A", "B")}

// labs := []attribute.KeyValue{attribute.String("A", "B")}
// cnt := Must(sdk).NewInt64Counter("int64.sum")
cnt := fix.iCounter("int64.sum")

// b.ResetTimer()
b.ResetTimer()

// for i := 0; i < b.N; i++ {
// cnt.Add(ctx, 1, labs...)
// }
// }
for i := 0; i < b.N; i++ {
cnt.Add(ctx, 1, labs...)
}
}

func BenchmarkInt64CounterAdd(b *testing.B) {
ctx := context.Background()
Expand Down

0 comments on commit c91da41

Please sign in to comment.