update: trace

This commit is contained in:
loveuer
2024-07-17 21:31:51 +08:00
parent ec869aec2b
commit 116d5b13f8
5 changed files with 15 additions and 19 deletions

View File

@ -8,7 +8,6 @@ import (
"github.com/loveuer/nf/nft/resp"
"net/http"
"strconv"
"strings"
"time"
"ultone/internal/opt"
"ultone/internal/tool"
@ -22,29 +21,25 @@ func New() nf.HandlerFunc {
return func(c *nf.Ctx) error {
var (
now = time.Now()
trace = c.Get(Header)
logFn func(msg string, data ...any)
ip = c.IP()
now = time.Now()
traceId = c.Get(Header)
logFn func(msg string, data ...any)
ip = c.IP()
)
if trace == "" {
trace = uuid.Must(uuid.NewV7()).String()
if traceId == "" {
traceId = uuid.Must(uuid.NewV7()).String()
}
c.SetHeader(Header, trace)
c.SetHeader(Header, traceId)
traces := strings.Split(trace, "-")
shortTrace := traces[len(traces)-1]
c.Locals(opt.LocalTraceKey, trace)
c.Locals(opt.LocalShortTraceKey, shortTrace)
c.Locals(opt.LocalTraceKey, traceId)
err := c.Next()
status, _ := strconv.Atoi(c.Writer.Header().Get(resp.RealStatusHeader))
duration := time.Since(now)
msg := fmt.Sprintf("%s | %15s | %d[%3d] | %s | %6s | %s", shortTrace, ip, c.StatusCode, status, tool.HumanDuration(duration.Nanoseconds()), c.Method(), c.Path())
msg := fmt.Sprintf("%s | %15s | %d[%3d] | %s | %6s | %s", traceId, ip, c.StatusCode, status, tool.HumanDuration(duration.Nanoseconds()), c.Method(), c.Path())
switch {
case status >= 500: