更新字符串清理和token报错

This commit is contained in:
Cold
2025-12-03 16:39:55 +08:00
committed by 张斌
parent 0738f6f957
commit 17cc8a371a
3 changed files with 32 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"strconv"
"strings"
"github.com/gogf/gf/contrib/trace/otlphttp/v2"
"github.com/gogf/gf/v2/frame/g"
@@ -28,7 +29,17 @@ func NewTracer(r *ghttp.Request) {
defer span.End()
span.SetAttributes(attribute.String("request", getParams(r)))
r.Middleware.Next()
span.SetAttributes(attribute.String("response", r.Response.BufferString()))
// 清理响应字符串,确保 UTF-8 有效(处理二进制数据如 ZIP 文件)
response := r.Response.BufferString()
cleanResponse := strings.ToValidUTF8(response, "")
// 如果响应太大(如文件下载),只记录前 1000 字符
if len(cleanResponse) > 1000 {
cleanResponse = cleanResponse[:1000] + "... (truncated)"
}
span.SetAttributes(attribute.String("response", cleanResponse))
}
func getParams(r *ghttp.Request) string {
params := map[string]interface{}{}