tracing: setup log provider

This commit is contained in:
Ask Bjørn Hansen 2024-11-09 10:19:16 +00:00
parent ddb56b3566
commit ab94adb925

View File

@ -9,6 +9,7 @@ import (
"crypto/x509" "crypto/x509"
"errors" "errors"
"os" "os"
"slices"
"time" "time"
"go.ntppool.org/common/logger" "go.ntppool.org/common/logger"
@ -21,9 +22,11 @@ import (
"go.opentelemetry.io/otel/exporters/otlp/otlptrace" "go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
logglobal "go.opentelemetry.io/otel/log/global"
"go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/propagation"
sdklog "go.opentelemetry.io/otel/sdk/log"
"go.opentelemetry.io/otel/sdk/resource" "go.opentelemetry.io/otel/sdk/resource"
otelsdktrace "go.opentelemetry.io/otel/sdk/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0" semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
@ -64,15 +67,17 @@ type TracerConfig struct {
} }
func InitTracer(ctx context.Context, cfg *TracerConfig) (TpShutdownFunc, error) { func InitTracer(ctx context.Context, cfg *TracerConfig) (TpShutdownFunc, error) {
// todo: setup environment from cfg
return SetupSDK(ctx, cfg) return SetupSDK(ctx, cfg)
} }
func SetupSDK(ctx context.Context, cfg *TracerConfig) (shutdown TpShutdownFunc, err error) { func SetupSDK(ctx context.Context, cfg *TracerConfig) (shutdown TpShutdownFunc, err error) {
if cfg == nil {
cfg = &TracerConfig{}
}
log := logger.Setup() log := logger.Setup()
log.Warn("SetupSDK")
if serviceName := os.Getenv(svcNameKey); len(serviceName) == 0 { if serviceName := os.Getenv(svcNameKey); len(serviceName) == 0 {
if len(cfg.ServiceName) > 0 { if len(cfg.ServiceName) > 0 {
os.Setenv(svcNameKey, cfg.ServiceName) os.Setenv(svcNameKey, cfg.ServiceName)
@ -113,10 +118,18 @@ func SetupSDK(ctx context.Context, cfg *TracerConfig) (shutdown TpShutdownFunc,
var shutdownFuncs []func(context.Context) error var shutdownFuncs []func(context.Context) error
shutdown = func(ctx context.Context) error { shutdown = func(ctx context.Context) error {
var err error var err error
// need to shutdown the providers first,
// exporters after which is the opposite
// order they are setup.
slices.Reverse(shutdownFuncs)
for _, fn := range shutdownFuncs { for _, fn := range shutdownFuncs {
// log.Warn("shutting down", "fn", fn)
err = errors.Join(err, fn(ctx)) err = errors.Join(err, fn(ctx))
} }
shutdownFuncs = nil shutdownFuncs = nil
if err != nil {
log.Warn("shutdown returned errors", "err", err)
}
return err return err
} }
@ -128,29 +141,32 @@ func SetupSDK(ctx context.Context, cfg *TracerConfig) (shutdown TpShutdownFunc,
prop := newPropagator() prop := newPropagator()
otel.SetTextMapPropagator(prop) otel.SetTextMapPropagator(prop)
var exporter otelsdktrace.SpanExporter var spanExporter sdktrace.SpanExporter
switch os.Getenv("OTEL_TRACES_EXPORTER") { switch os.Getenv("OTEL_TRACES_EXPORTER") {
case "": case "":
exporter, err = newOLTPExporter(ctx, cfg) spanExporter, err = newOLTPExporter(ctx, cfg)
case "otlp": case "otlp":
exporter, err = newOLTPExporter(ctx, cfg) spanExporter, err = newOLTPExporter(ctx, cfg)
default: default:
exporter, err = autoexport.NewSpanExporter(ctx) // log.Debug("OTEL_TRACES_EXPORTER", "fallback", os.Getenv("OTEL_TRACES_EXPORTER"))
spanExporter, err = autoexport.NewSpanExporter(ctx)
} }
if err != nil { if err != nil {
handleErr(err) handleErr(err)
return return
} }
shutdownFuncs = append(shutdownFuncs, exporter.Shutdown) shutdownFuncs = append(shutdownFuncs, spanExporter.Shutdown)
if exporter == nil { logExporter, err := autoexport.NewLogExporter(ctx)
log.Warn("tracing not configured") if err != nil {
handleErr(err)
return return
} }
shutdownFuncs = append(shutdownFuncs, logExporter.Shutdown)
// Set up trace provider. // Set up trace provider.
tracerProvider, err := newTraceProvider(exporter, res) tracerProvider, err := newTraceProvider(spanExporter, res)
if err != nil { if err != nil {
handleErr(err) handleErr(err)
return return
@ -158,6 +174,19 @@ func SetupSDK(ctx context.Context, cfg *TracerConfig) (shutdown TpShutdownFunc,
shutdownFuncs = append(shutdownFuncs, tracerProvider.Shutdown) shutdownFuncs = append(shutdownFuncs, tracerProvider.Shutdown)
otel.SetTracerProvider(tracerProvider) otel.SetTracerProvider(tracerProvider)
logProvider := sdklog.NewLoggerProvider(sdklog.WithResource(res),
sdklog.WithProcessor(
sdklog.NewBatchProcessor(logExporter, sdklog.WithExportBufferSize(10)),
),
)
logglobal.SetLoggerProvider(logProvider)
shutdownFuncs = append(shutdownFuncs, func(ctx context.Context) error {
logProvider.ForceFlush(ctx)
return logProvider.Shutdown(ctx)
},
)
if err != nil { if err != nil {
handleErr(err) handleErr(err)
return return
@ -166,7 +195,7 @@ func SetupSDK(ctx context.Context, cfg *TracerConfig) (shutdown TpShutdownFunc,
return return
} }
func newOLTPExporter(ctx context.Context, cfg *TracerConfig) (otelsdktrace.SpanExporter, error) { func newOLTPExporter(ctx context.Context, cfg *TracerConfig) (sdktrace.SpanExporter, error) {
log := logger.Setup() log := logger.Setup()
@ -235,11 +264,11 @@ func newOLTPExporter(ctx context.Context, cfg *TracerConfig) (otelsdktrace.SpanE
return exporter, err return exporter, err
} }
func newTraceProvider(traceExporter otelsdktrace.SpanExporter, res *resource.Resource) (*otelsdktrace.TracerProvider, error) { func newTraceProvider(traceExporter sdktrace.SpanExporter, res *resource.Resource) (*sdktrace.TracerProvider, error) {
traceProvider := otelsdktrace.NewTracerProvider( traceProvider := sdktrace.NewTracerProvider(
otelsdktrace.WithResource(res), sdktrace.WithResource(res),
otelsdktrace.WithBatcher(traceExporter, sdktrace.WithBatcher(traceExporter,
otelsdktrace.WithBatchTimeout(time.Second*3), sdktrace.WithBatchTimeout(time.Second*3),
), ),
) )
return traceProvider, nil return traceProvider, nil