update: X-Trace-Id

This commit is contained in:
loveuer
2024-07-17 23:05:23 +08:00
parent 116d5b13f8
commit 459e3d9c09
10 changed files with 60 additions and 68 deletions

View File

@ -2,38 +2,25 @@ package logger
import (
"fmt"
"github.com/google/uuid"
"github.com/loveuer/esgo2dump/log"
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/resp"
"net/http"
"strconv"
"time"
"ultone/internal/opt"
"ultone/internal/tool"
)
var (
Header = http.CanonicalHeaderKey("X-Trace-Id")
)
func New() nf.HandlerFunc {
return func(c *nf.Ctx) error {
var (
now = time.Now()
traceId = c.Get(Header)
logFn func(msg string, data ...any)
ip = c.IP()
now = time.Now()
logFn func(msg string, data ...any)
ip = c.IP()
)
if traceId == "" {
traceId = uuid.Must(uuid.NewV7()).String()
}
c.SetHeader(Header, traceId)
c.Locals(opt.LocalTraceKey, traceId)
traceId := c.Context().Value(nf.TraceKey)
c.Locals(nf.TraceKey, traceId)
err := c.Next()
status, _ := strconv.Atoi(c.Writer.Header().Get(resp.RealStatusHeader))