Proposal Details
const XRquestID = "X-Request-Id"
var xRquestID = any(XRquestID)
func GetTraceID(c context.Context) string {
traceID, _ := c.Value(xRquestID).(string)
return traceID
}
type TraceHandler struct {
slog.Handler
}
func (h TraceHandler) Handle(ctx context.Context, r slog.Record) error {
r.AddAttrs(slog.String(XRquestID, GetTraceID(ctx)))
return h.Handler.Handle(ctx, r)
}
In slog.Handler, dynamically added fields like trace_id always appear at the end of the log line.
This becomes a problem when logs are long — some platforms truncate long lines, causing trace_id to be silently dropped.
Suggestion: Allow control, Trace ID in Before the slog.MessageKey or other control program operations