mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-06-22 01:02:29 +00:00
fix(otel)!: do not use a global client
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
64
otel/otel.go
64
otel/otel.go
@ -4,17 +4,14 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"go.linka.cloud/grpc-toolkit/logger"
|
||||
)
|
||||
|
||||
var log = logger.StandardLogger().WithField("name", "otel")
|
||||
var dummy = newClient(&DSN{})
|
||||
|
||||
// Configure configures OpenTelemetry.
|
||||
// By default, it:
|
||||
@ -23,27 +20,28 @@ var log = logger.StandardLogger().WithField("name", "otel")
|
||||
// - sets tracecontext + baggage composite context propagator.
|
||||
//
|
||||
// You can use OTEL_DISABLED env var to completely skip otel configuration.
|
||||
func Configure(opts ...Option) {
|
||||
func Configure(ctx context.Context, opts ...Option) Provider {
|
||||
if _, ok := os.LookupEnv("OTEL_DISABLED"); ok {
|
||||
return
|
||||
return dummy
|
||||
}
|
||||
|
||||
ctx := context.TODO()
|
||||
log := logger.C(ctx)
|
||||
|
||||
conf := newConfig(opts)
|
||||
|
||||
if !conf.tracingEnabled && !conf.metricsEnabled && !conf.loggingEnabled {
|
||||
return
|
||||
return dummy
|
||||
}
|
||||
|
||||
if len(conf.dsn) == 0 {
|
||||
log.Warn("no DSN provided (otel-go is disabled)")
|
||||
return
|
||||
return dummy
|
||||
}
|
||||
|
||||
dsn, err := ParseDSN(conf.dsn[0])
|
||||
if err != nil {
|
||||
log.Warnf("invalid DSN: %s (otel is disabled)", err)
|
||||
return
|
||||
return dummy
|
||||
}
|
||||
|
||||
if strings.HasSuffix(dsn.Host, ":4318") {
|
||||
@ -63,7 +61,7 @@ func Configure(opts ...Option) {
|
||||
client.lp = configureLogging(ctx, conf)
|
||||
}
|
||||
|
||||
atomicClient.Store(client)
|
||||
return client
|
||||
}
|
||||
|
||||
func configurePropagator(conf *config) {
|
||||
@ -76,47 +74,3 @@ func configurePropagator(conf *config) {
|
||||
}
|
||||
otel.SetTextMapPropagator(textMapPropagator)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
var (
|
||||
fallbackClient = newClient(&DSN{})
|
||||
atomicClient atomic.Value
|
||||
)
|
||||
|
||||
func activeClient() *client {
|
||||
v := atomicClient.Load()
|
||||
if v == nil {
|
||||
return fallbackClient
|
||||
}
|
||||
return v.(*client)
|
||||
}
|
||||
|
||||
func TraceURL(span trace.Span) string {
|
||||
return activeClient().TraceURL(span)
|
||||
}
|
||||
|
||||
func ReportError(ctx context.Context, err error, opts ...trace.EventOption) {
|
||||
activeClient().ReportError(ctx, err, opts...)
|
||||
}
|
||||
|
||||
func ReportPanic(ctx context.Context, val any) {
|
||||
activeClient().ReportPanic(ctx, val)
|
||||
}
|
||||
|
||||
func Shutdown(ctx context.Context) error {
|
||||
return activeClient().Shutdown(ctx)
|
||||
}
|
||||
|
||||
func ForceFlush(ctx context.Context) error {
|
||||
return activeClient().ForceFlush(ctx)
|
||||
}
|
||||
|
||||
func TracerProvider() *sdktrace.TracerProvider {
|
||||
return activeClient().tp
|
||||
}
|
||||
|
||||
// SetLogger sets the logger to the given one.
|
||||
func SetLogger(logger logger.Logger) {
|
||||
log = logger
|
||||
}
|
||||
|
Reference in New Issue
Block a user