优化抢购

This commit is contained in:
yml2213
2026-06-20 05:54:25 +08:00
parent f04ba4ed64
commit 86374fdd4d
5 changed files with 172 additions and 38 deletions
+72 -4
View File
@@ -42,9 +42,9 @@ func initTrace(logDir string, cfg TraceConfig) (*traceRecorder, func()) {
filePath: path,
}
if cfg.MaxBodyBytes > 0 {
logEvent("Trace", "warn", "trace 已启用: %s,单段正文最多保留 %d 字节,不做脱敏", path, cfg.MaxBodyBytes)
logEvent("Trace", "warn", "trace 已启用: %s,单段正文最多保留 %d 字节;请求头不脱敏,响应 Set-Cookie 隐藏值", path, cfg.MaxBodyBytes)
} else {
logEvent("Trace", "warn", "trace 已启用: %s,全量保留请求和响应,不做脱敏", path)
logEvent("Trace", "warn", "trace 已启用: %s,全量保留请求和响应;请求头不脱敏,响应 Set-Cookie 隐藏值", path)
}
return recorder, func() {
_ = recorder.Close()
@@ -60,6 +60,10 @@ func (t *traceRecorder) Close() error {
}
func (t *traceRecorder) record(label string, req *http.Request, status int, cost time.Duration, reqBody, respBody []byte, errText string, apiError int, apiMsg string) {
t.recordResponse(label, req, nil, status, cost, reqBody, respBody, errText, apiError, apiMsg)
}
func (t *traceRecorder) recordResponse(label string, req *http.Request, resp *http.Response, status int, cost time.Duration, reqBody, respBody []byte, errText string, apiError int, apiMsg string) {
if t == nil || !t.enabled || req == nil || req.URL == nil {
return
}
@@ -76,6 +80,7 @@ func (t *traceRecorder) record(label string, req *http.Request, status int, cost
Status: status,
Cost: cost,
ReqHeader: sanitizeHeader(req.Header),
RespHeader: sanitizeResponseHeader(resp),
ReqBody: sanitizeBodyForTrace(reqBody, req.Header.Get("Content-Type"), t.maxBodyBytes),
RespBody: sanitizeResponseBody(respBody, t.maxBodyBytes),
APIError: apiError,
@@ -114,6 +119,66 @@ func sanitizeHeader(header http.Header) map[string][]string {
return out
}
func sanitizeResponseHeader(resp *http.Response) map[string][]string {
if resp == nil {
return nil
}
out := sanitizeHeader(resp.Header)
if len(out) == 0 {
return out
}
if _, ok := out["Set-Cookie"]; ok {
out["Set-Cookie"] = summarizeSetCookies(resp)
}
return out
}
func summarizeSetCookies(resp *http.Response) []string {
if resp == nil {
return nil
}
cookies := resp.Cookies()
if len(cookies) == 0 {
values := resp.Header.Values("Set-Cookie")
summary := make([]string, 0, len(values))
for range values {
summary = append(summary, "<hidden>")
}
return summary
}
summary := make([]string, 0, len(cookies))
for _, ck := range cookies {
if ck == nil || ck.Name == "" {
continue
}
parts := []string{ck.Name + "=<hidden>"}
if ck.Path != "" {
parts = append(parts, "Path="+ck.Path)
}
if ck.Domain != "" {
parts = append(parts, "Domain="+ck.Domain)
}
if ck.MaxAge != 0 {
parts = append(parts, fmt.Sprintf("Max-Age=%d", ck.MaxAge))
}
if !ck.Expires.IsZero() {
parts = append(parts, "Expires="+ck.Expires.In(cst).Format(time.RFC3339))
}
if ck.HttpOnly {
parts = append(parts, "HttpOnly")
}
if ck.Secure {
parts = append(parts, "Secure")
}
summary = append(summary, strings.Join(parts, "; "))
}
if len(summary) == 0 {
return []string{"<hidden>"}
}
sort.Strings(summary)
return summary
}
func sanitizeBodyForTrace(body []byte, contentType string, max int) string {
if len(body) == 0 {
return ""
@@ -192,6 +257,7 @@ type traceEntry struct {
Status int
Cost time.Duration
ReqHeader map[string][]string
RespHeader map[string][]string
ReqBody string
RespBody string
APIError int
@@ -220,6 +286,8 @@ func formatTraceEntry(e traceEntry) string {
if e.ErrorText != "" {
fmt.Fprintf(&b, "错误: %s\n", e.ErrorText)
}
b.WriteString("\n[响应头]\n")
b.WriteString(formatTraceHeaders(e.RespHeader))
b.WriteString("\n[请求头]\n")
b.WriteString(formatTraceHeaders(e.ReqHeader))
b.WriteString("\n[请求体]\n")
@@ -227,9 +295,9 @@ func formatTraceEntry(e traceEntry) string {
b.WriteString("\n[响应体]\n")
b.WriteString(formatTraceBody(e.RespBody))
if e.MaxBodyBytes > 0 {
fmt.Fprintf(&b, "\n提示: 单段正文最多保留 %d 字节;trace 不做脱敏。\n", e.MaxBodyBytes)
fmt.Fprintf(&b, "\n提示: 单段正文最多保留 %d 字节;请求头不脱敏,响应 Set-Cookie 隐藏值。\n", e.MaxBodyBytes)
} else {
b.WriteString("\n提示: trace 全量保留请求和响应,不做脱敏。\n")
b.WriteString("\n提示: trace 全量保留请求和响应;请求头不脱敏,响应 Set-Cookie 隐藏值。\n")
}
b.WriteByte('\n')
return b.String()