Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back the metric global package #2764

Merged
merged 4 commits into from Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,8 @@ 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. (#2752)

MrAlias marked this conversation as resolved.
Show resolved Hide resolved
- 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
17 changes: 8 additions & 9 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,12 +34,10 @@ const (
)

var (
// TODO Bring back Global package
// meter = global.GetMeterProvider().Meter(
// instrumentationName,
// metric.WithInstrumentationVersion(instrumentationVersion),
// )
meter metric.Meter
meter = global.Meter(
instrumentationName,
metric.WithInstrumentationVersion(instrumentationVersion),
)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

loopCounter syncint64.Counter
paramValue syncint64.Histogram
Expand Down Expand Up @@ -82,9 +81,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