You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-11-25 22:41:46 +02:00
Bring back the metric global package (#2764)
* 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>
This commit is contained in:
@@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|||||||
|
|
||||||
### Added
|
### 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.
|
- 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)
|
This function is a convenience function equivalent to calling `global.MeterProvider().Meter(...)`. (#2750)
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import (
|
|||||||
|
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/exporters/prometheus"
|
"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/metric/instrument"
|
||||||
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
|
||||||
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
||||||
@@ -35,9 +35,6 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
lemonsKey = attribute.Key("ex.com/lemons")
|
lemonsKey = attribute.Key("ex.com/lemons")
|
||||||
|
|
||||||
// TODO Bring back Global package
|
|
||||||
meterProvider metric.MeterProvider
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func initMeter() {
|
func initMeter() {
|
||||||
@@ -57,9 +54,8 @@ func initMeter() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("failed to initialize prometheus exporter %v", err)
|
log.Panicf("failed to initialize prometheus exporter %v", err)
|
||||||
}
|
}
|
||||||
// TODO Bring back Global package
|
|
||||||
// global.SetMeterProvider(exporter.MeterProvider())
|
global.SetMeterProvider(exporter.MeterProvider())
|
||||||
meterProvider = exporter.MeterProvider()
|
|
||||||
|
|
||||||
http.HandleFunc("/", exporter.ServeHTTP)
|
http.HandleFunc("/", exporter.ServeHTTP)
|
||||||
go func() {
|
go func() {
|
||||||
@@ -72,9 +68,8 @@ func initMeter() {
|
|||||||
func main() {
|
func main() {
|
||||||
initMeter()
|
initMeter()
|
||||||
|
|
||||||
// TODO Bring back Global package
|
meter := global.Meter("ex.com/basic")
|
||||||
// meter := global.Meter("ex.com/basic")
|
|
||||||
meter := meterProvider.Meter("ex.com/basic")
|
|
||||||
observerLock := new(sync.RWMutex)
|
observerLock := new(sync.RWMutex)
|
||||||
observerValueToReport := new(float64)
|
observerValueToReport := new(float64)
|
||||||
observerLabelsToReport := new([]attribute.KeyValue)
|
observerLabelsToReport := new([]attribute.KeyValue)
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
"go.opentelemetry.io/otel"
|
"go.opentelemetry.io/otel"
|
||||||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric"
|
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric"
|
||||||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
|
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
|
||||||
|
"go.opentelemetry.io/otel/metric/global"
|
||||||
"go.opentelemetry.io/otel/metric/instrument"
|
"go.opentelemetry.io/otel/metric/instrument"
|
||||||
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
||||||
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
|
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
|
||||||
@@ -53,11 +54,11 @@ func Example_insecure() {
|
|||||||
controller.WithExporter(exp),
|
controller.WithExporter(exp),
|
||||||
controller.WithCollectPeriod(2*time.Second),
|
controller.WithCollectPeriod(2*time.Second),
|
||||||
)
|
)
|
||||||
// TODO Bring back Global package
|
|
||||||
// global.SetMeterProvider(pusher)
|
global.SetMeterProvider(pusher)
|
||||||
|
|
||||||
if err := pusher.Start(ctx); err != nil {
|
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() {
|
defer func() {
|
||||||
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
||||||
@@ -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 := global.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
|
|
||||||
meter := pusher.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
|
|
||||||
|
|
||||||
// Recorder metric example
|
// Recorder metric example
|
||||||
|
|
||||||
@@ -115,11 +114,11 @@ func Example_withTLS() {
|
|||||||
controller.WithExporter(exp),
|
controller.WithExporter(exp),
|
||||||
controller.WithCollectPeriod(2*time.Second),
|
controller.WithCollectPeriod(2*time.Second),
|
||||||
)
|
)
|
||||||
// TODO Bring back Global package
|
|
||||||
// global.SetMeterProvider(pusher)
|
global.SetMeterProvider(pusher)
|
||||||
|
|
||||||
if err := pusher.Start(ctx); err != nil {
|
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() {
|
defer func() {
|
||||||
@@ -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 := global.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
|
|
||||||
meter := pusher.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
|
|
||||||
|
|
||||||
// Recorder metric example
|
// Recorder metric example
|
||||||
counter, err := meter.SyncFloat64().Counter("an_important_metric", instrument.WithDescription("Measures the cumulative epicness of the app"))
|
counter, err := meter.SyncFloat64().Counter("an_important_metric", instrument.WithDescription("Measures the cumulative epicness of the app"))
|
||||||
@@ -174,11 +171,11 @@ func Example_withDifferentSignalCollectors() {
|
|||||||
controller.WithExporter(exp),
|
controller.WithExporter(exp),
|
||||||
controller.WithCollectPeriod(2*time.Second),
|
controller.WithCollectPeriod(2*time.Second),
|
||||||
)
|
)
|
||||||
// TODO Bring back Global package
|
|
||||||
// global.SetMeterProvider(pusher)
|
global.SetMeterProvider(pusher)
|
||||||
|
|
||||||
if err := pusher.Start(ctx); err != nil {
|
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() {
|
defer func() {
|
||||||
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
||||||
@@ -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 := global.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
|
|
||||||
meter := pusher.Meter("go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc_test")
|
|
||||||
|
|
||||||
// Recorder metric example
|
// Recorder metric example
|
||||||
counter, err := meter.SyncFloat64().Counter("an_important_metric", instrument.WithDescription("Measures the cumulative epicness of the app"))
|
counter, err := meter.SyncFloat64().Counter("an_important_metric", instrument.WithDescription("Measures the cumulative epicness of the app"))
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import (
|
|||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
|
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
|
||||||
"go.opentelemetry.io/otel/metric"
|
"go.opentelemetry.io/otel/metric"
|
||||||
|
"go.opentelemetry.io/otel/metric/global"
|
||||||
"go.opentelemetry.io/otel/metric/instrument/syncint64"
|
"go.opentelemetry.io/otel/metric/instrument/syncint64"
|
||||||
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
||||||
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
|
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
|
||||||
@@ -33,13 +34,6 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// TODO Bring back Global package
|
|
||||||
// meter = global.GetMeterProvider().Meter(
|
|
||||||
// instrumentationName,
|
|
||||||
// metric.WithInstrumentationVersion(instrumentationVersion),
|
|
||||||
// )
|
|
||||||
meter metric.Meter
|
|
||||||
|
|
||||||
loopCounter syncint64.Counter
|
loopCounter syncint64.Counter
|
||||||
paramValue syncint64.Histogram
|
paramValue syncint64.Histogram
|
||||||
|
|
||||||
@@ -82,9 +76,9 @@ func InstallExportPipeline(ctx context.Context) func() {
|
|||||||
if err = pusher.Start(ctx); err != nil {
|
if err = pusher.Start(ctx); err != nil {
|
||||||
log.Fatalf("starting push controller: %v", err)
|
log.Fatalf("starting push controller: %v", err)
|
||||||
}
|
}
|
||||||
// TODO Bring back Global package
|
|
||||||
// global.SetMeterProvider(pusher)
|
global.SetMeterProvider(pusher)
|
||||||
meter = pusher.Meter(instrumentationName, metric.WithInstrumentationVersion(instrumentationVersion))
|
meter := global.Meter(instrumentationName, metric.WithInstrumentationVersion(instrumentationVersion))
|
||||||
|
|
||||||
loopCounter, err = meter.SyncInt64().Counter("function.loops")
|
loopCounter, err = meter.SyncInt64().Counter("function.loops")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
|
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/metric"
|
"go.opentelemetry.io/otel/metric"
|
||||||
|
"go.opentelemetry.io/otel/metric/global"
|
||||||
"go.opentelemetry.io/otel/metric/instrument"
|
"go.opentelemetry.io/otel/metric/instrument"
|
||||||
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
|
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
|
||||||
"go.opentelemetry.io/otel/metric/instrument/syncint64"
|
"go.opentelemetry.io/otel/metric/instrument/syncint64"
|
||||||
@@ -181,27 +182,25 @@ func BenchmarkIterator_16(b *testing.B) {
|
|||||||
|
|
||||||
// Counters
|
// 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) {
|
global.SetMeterProvider(fix)
|
||||||
// // 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)
|
|
||||||
|
|
||||||
// sdk := global.Meter("test")
|
labs := []attribute.KeyValue{attribute.String("A", "B")}
|
||||||
// global.SetMeterProvider(fix)
|
|
||||||
|
|
||||||
// labs := []attribute.KeyValue{attribute.String("A", "B")}
|
cnt := fix.iCounter("int64.sum")
|
||||||
// cnt := Must(sdk).NewInt64Counter("int64.sum")
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
// for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
// cnt.Add(ctx, 1, labs...)
|
cnt.Add(ctx, 1, labs...)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
func BenchmarkInt64CounterAdd(b *testing.B) {
|
func BenchmarkInt64CounterAdd(b *testing.B) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|||||||
Reference in New Issue
Block a user