tracing: support for shutting down the trace provider
This commit is contained in:
parent
9d136b2502
commit
a4447c97f6
@ -19,6 +19,8 @@ import (
|
||||
|
||||
// https://github.com/open-telemetry/opentelemetry-go/blob/main/exporters/otlp/otlptrace/otlptracehttp/example_test.go
|
||||
|
||||
type TpShutdownFunc func(ctx context.Context) error
|
||||
|
||||
func Tracer() trace.Tracer {
|
||||
traceProvider := otel.GetTracerProvider()
|
||||
return traceProvider.Tracer("ntppool-tracer")
|
||||
@ -29,7 +31,11 @@ type TracerConfig struct {
|
||||
Environment string
|
||||
}
|
||||
|
||||
func InitTracer(ctx context.Context, cfg *TracerConfig) error {
|
||||
var emptyTpShutdownFunc = func(_ context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitTracer(ctx context.Context, cfg *TracerConfig) (TpShutdownFunc, error) {
|
||||
|
||||
log := logger.Setup()
|
||||
|
||||
@ -43,23 +49,24 @@ func InitTracer(ctx context.Context, cfg *TracerConfig) error {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return emptyTpShutdownFunc, err
|
||||
}
|
||||
|
||||
if exporter != nil {
|
||||
tp := otelsdktrace.NewTracerProvider(
|
||||
otelsdktrace.WithSampler(otelsdktrace.AlwaysSample()),
|
||||
otelsdktrace.WithBatcher(exporter),
|
||||
otelsdktrace.WithResource(newResource(cfg)),
|
||||
)
|
||||
|
||||
otel.SetTracerProvider(tp)
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
||||
} else {
|
||||
if exporter == nil {
|
||||
log.Warn("tracing not configured")
|
||||
return emptyTpShutdownFunc, nil
|
||||
}
|
||||
|
||||
return nil
|
||||
tp := otelsdktrace.NewTracerProvider(
|
||||
otelsdktrace.WithSampler(otelsdktrace.AlwaysSample()),
|
||||
otelsdktrace.WithBatcher(exporter),
|
||||
otelsdktrace.WithResource(newResource(cfg)),
|
||||
)
|
||||
|
||||
otel.SetTracerProvider(tp)
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
||||
|
||||
return tp.Shutdown, nil
|
||||
}
|
||||
|
||||
func newOLTPExporter(ctx context.Context) (otelsdktrace.SpanExporter, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user