fix(tracing): use global provider by default, but allow custom providers (#2803)

pull/2750/head
Haibo 2 years ago committed by GitHub
parent c6a4604839
commit 1f10166028
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      middleware/tracing/tracer.go
  2. 2
      middleware/tracing/tracing.go

@ -30,15 +30,15 @@ func NewTracer(kind trace.SpanKind, opts ...Option) *Tracer {
for _, o := range opts { for _, o := range opts {
o(&op) o(&op)
} }
if op.tracerProvider != nil { if op.tracerProvider == nil {
otel.SetTracerProvider(op.tracerProvider) op.tracerProvider = otel.GetTracerProvider()
} }
switch kind { switch kind {
case trace.SpanKindClient: case trace.SpanKindClient:
return &Tracer{tracer: otel.Tracer(op.tracerName), kind: kind, opt: &op} return &Tracer{tracer: op.tracerProvider.Tracer(op.tracerName), kind: kind, opt: &op}
case trace.SpanKindServer: case trace.SpanKindServer:
return &Tracer{tracer: otel.Tracer(op.tracerName), kind: kind, opt: &op} return &Tracer{tracer: op.tracerProvider.Tracer(op.tracerName), kind: kind, opt: &op}
default: default:
panic(fmt.Sprintf("unsupported span kind: %v", kind)) panic(fmt.Sprintf("unsupported span kind: %v", kind))
} }

@ -29,7 +29,7 @@ func WithPropagator(propagator propagation.TextMapPropagator) Option {
} }
// WithTracerProvider with tracer provider. // WithTracerProvider with tracer provider.
// Deprecated: use otel.SetTracerProvider(provider) instead. // By default, it uses the global provider that is set by otel.SetTracerProvider(provider).
func WithTracerProvider(provider trace.TracerProvider) Option { func WithTracerProvider(provider trace.TracerProvider) Option {
return func(opts *options) { return func(opts *options) {
opts.tracerProvider = provider opts.tracerProvider = provider

Loading…
Cancel
Save