I'm trying to use otelzap as middleware for fiber, but I'm not seeing log messages provided with a span context showing up in the span output. Trace IDs also do not appear in the log message, despite the logger option being present. Also perhaps relevant, Fiber is being run with Immutable set to true.
Here is the middleware function with some additional code comments:
func (a *ManagementAPI) RequestLogging(c *fiber.Ctx) error {
start := time.Now()
c.Next()
// c.UserContext() is set by an earlier middleware function that creates a "request" span
ctx, span := a.config.Tracer(ServiceName).Start(c.UserContext(), "request_log")
defer span.End()
// a.logger has type *otelzap.Logger
a.logger.Ctx(ctx).Info(
fmt.Sprintf("%s", c.Path()),
zap.Int("status", c.Response().StatusCode()),
zap.String("method", c.Method()),
zap.String("path", c.Path()),
zap.String("ip", c.IP()),
zap.ByteString("user_agent", c.Request().Header.UserAgent()),
zap.Int64("latency_ns", time.Now().Sub(start).Nanoseconds()),
// zap.String("trace_id", span.SpanContext().TraceID().String()),
)
return nil
}
The span context coming from c.UserContext() also appears to be valid, as it does link parent and child spans correctly.
The logger being used in the middleware function is created by this function:
func L(name string) *otelzap.Logger {
return otelzap.New(zap.L().Named(name), otelzap.WithTraceIDField(true))
}
Before then, the global loggers are set up like so:
func InitializeGlobalLogger(c LoggingConfig) error {
zapConfig, err := zapConfig(c)
if err != nil {
return fmt.Errorf("problem creating zap configuration: %w\n", err)
}
zapLogger := zap.Must(zapConfig.Build())
zapLogger = zapLogger.With(zap.String("id", c.AppID()))
defer zapLogger.Sync()
otelLogger := otelzap.New(zapLogger, otelzap.WithTraceIDField(true))
defer otelLogger.Sync()
zap.ReplaceGlobals(zapLogger)
otelzap.ReplaceGlobals(otelLogger)
return nil
}
The project is using go 1.19, and these versions may be relevant:
go.uber.org/zap v1.23.0
github.com/uptrace/opentelemetry-go-extra/otelutil v0.1.17 // indirect
github.com/uptrace/opentelemetry-go-extra/otelzap v0.1.17 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.11.2 // indirect
go.opentelemetry.io/otel/sdk v1.11.2 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
Any help would be greatly appreciated.