github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/observability/exporter.go (about) 1 package observability 2 3 import ( 4 "context" 5 "time" 6 7 "go.opentelemetry.io/otel" 8 "go.opentelemetry.io/otel/exporters/prometheus" 9 "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" 10 "go.opentelemetry.io/otel/sdk/metric" 11 ) 12 13 // References: 14 // https://docs.datadoghq.com/opentelemetry/runtime_metrics/go/ 15 // https://opentelemetry.io/docs/languages/go/exporters/ 16 17 // Serves for test/dev environment. 18 func newConsoleMetricsExporter(interval, timeout time.Duration, opts ...stdoutmetric.Option) (func(ctx context.Context) error, error) { 19 exporter, err := stdoutmetric.New(opts...) 20 if err != nil { 21 return nil, err 22 } 23 mp := metric.NewMeterProvider(metric.WithReader(metric.NewPeriodicReader( 24 exporter, 25 metric.WithInterval(interval), 26 metric.WithTimeout(timeout), 27 ))) 28 callback := mp.Shutdown 29 otel.SetMeterProvider(mp) 30 return callback, nil 31 } 32 33 // Serves for the product environment and fetch stats metrics by HTTP. 34 func newPrometheusMetricsExporter() (func(ctx context.Context) error, error) { 35 exporter, err := prometheus.New() 36 if err != nil { 37 return nil, err 38 } 39 mp := metric.NewMeterProvider(metric.WithReader(exporter)) 40 callback := mp.Shutdown 41 otel.SetMeterProvider(mp) 42 return callback, nil 43 }