feat: 采集记录成功失败分表并保存网关原始数据,到期归档 csv.gz,新增记录分页查询接口

- 网关客户端新增原始数据收集器,记录最后一次尝试的明文请求、HTTP 状态、响应体与 code/msg/trace_id
- tb_card_traffic_collection 只写成功读数,新增 carrier_id 与原始数据列;新增 tb_card_traffic_collection_failure,失败不判断有无套餐并带错误详情
- 采集记录在线保留当月加最近 2 个整月,到期分区摘出、导出 csv.gz、校验行数与 SHA-256 后删除,本地保留 3 个月
- 新增采集成功/失败、卡日流量、套餐日流量详单、冲正流水、退款后处理尝试列表接口:可查单个也可查全部,分页与条件筛选
- 停复机记录支持资产标识、场景、原因、指令号筛选;单卡历史支持动作、结果、来源筛选
- 单卡采集记录支持筛选与合并失败记录;套餐日流量详单支持分页;退款列表支持后处理状态筛选
- 测试环境 api、worker 日志分目录,保留 40 个压缩备份;worker 挂载 archive 目录
- 迁移 000272、000273 需维护者在测试库执行后再部署

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
邹进
2026-11-05 18:47:00 +09:00
parent 8eb7d403c4
commit 94cbf2adab
39 changed files with 1794 additions and 140 deletions

View File

@@ -114,6 +114,10 @@ func (c *Client) doRequest(ctx context.Context, path string, params interface{})
return nil, errors.Wrap(errors.CodeInternalError, err, "序列化业务数据失败")
}
if capture := rawCaptureFrom(ctx); capture != nil {
capture.Request = append(json.RawMessage(nil), dataBytes...)
}
// 加密业务数据(加密结果不变,可在重试间复用)
encryptedData, err := aesEncrypt(dataBytes, c.appSecret)
if err != nil {
@@ -184,8 +188,13 @@ func (c *Client) executeWithRetry(ctx context.Context, path, encryptedData strin
return nil, err
}
}
capture := rawCaptureFrom(ctx)
if capture != nil {
capture.resetResponse()
capture.Attempts = attemptNumber
}
attemptStartedAt := time.Now()
result, retryable, err := c.executeHTTPRequest(ctx, path, encryptedData)
result, retryable, err := c.executeHTTPRequest(ctx, path, encryptedData, capture != nil && logPayload)
attemptDuration := time.Since(attemptStartedAt)
if gwErr, ok := AsGatewayError(err); ok {
gwErr.Duration = attemptDuration
@@ -237,7 +246,12 @@ func (c *Client) executeWithRetry(ctx context.Context, path, encryptedData strin
// executeHTTPRequest 执行单次 HTTP 请求(无重试逻辑)
// 返回值:响应数据、是否可重试、错误
func (c *Client) executeHTTPRequest(ctx context.Context, path string, encryptedData string) (json.RawMessage, bool, error) {
// captureRaw 为 true 时把状态码、响应体与外层字段写入上下文上的原始数据收集器。
func (c *Client) executeHTTPRequest(ctx context.Context, path string, encryptedData string, captureRaw bool) (json.RawMessage, bool, error) {
var capture *RawCapture
if captureRaw {
capture = rawCaptureFrom(ctx)
}
// 每次重试使用新的时间戳和签名
timestamp := time.Now().Unix()
sign := generateSign(c.appID, encryptedData, timestamp, c.appSecret)
@@ -277,6 +291,9 @@ func (c *Client) executeHTTPRequest(ctx context.Context, path string, encryptedD
return nil, true, newGatewayError(constants.GatewayErrorNetwork, errors.Wrap(errors.CodeGatewayError, err, "发送 HTTP 请求失败"))
}
defer resp.Body.Close()
if capture != nil {
capture.HTTPStatus = resp.StatusCode
}
// HTTP 状态码错误 — 不可重试
if resp.StatusCode != http.StatusOK {
@@ -290,10 +307,18 @@ func (c *Client) executeHTTPRequest(ctx context.Context, path string, encryptedD
return nil, false, newGatewayError(constants.GatewayErrorInvalidResponse, errors.Wrap(errors.CodeGatewayInvalidResp, err, "读取响应体失败"))
}
if capture != nil {
capture.Response = body
}
var gatewayResp GatewayResponse
if err := sonic.Unmarshal(body, &gatewayResp); err != nil {
return nil, false, newGatewayError(constants.GatewayErrorInvalidResponse, errors.Wrap(errors.CodeGatewayInvalidResp, err, "解析 Gateway 响应失败"))
}
if capture != nil {
code := gatewayResp.Code
capture.GatewayCode, capture.GatewayMsg, capture.TraceID = &code, gatewayResp.Msg, gatewayResp.TraceID
}
// Gateway 业务错误 — 不可重试
if gatewayResp.Code != gatewaySuccessCode {

View File

@@ -0,0 +1,44 @@
package gateway
import (
"context"
"encoding/json"
)
// RawCapture 收集一次网关调用的原始数据(最后一次 HTTP 尝试),供业务记录留存。
// 只保存加密前的明文业务请求与网关响应;不含加密报文、签名、应用标识、密钥与请求头。
type RawCapture struct {
// Request 是加密前的明文业务请求({"params": ...});不记录载荷的调用为空。
Request json.RawMessage
// HTTPStatus 是 HTTP 状态码;没拿到响应(超时、连接失败)时为 0。
HTTPStatus int
// Response 是网关原始响应体;没拿到响应体时为空。
Response []byte
// GatewayCode、GatewayMsg、TraceID 是网关响应外层字段;响应无法解析时为空。
GatewayCode *int
GatewayMsg string
TraceID string
// Attempts 是网关客户端内部 HTTP 尝试次数。
Attempts int
}
type rawCaptureKey struct{}
// WithRawCapture 在请求上下文上挂载原始数据收集器;未挂载的调用不收集。
func WithRawCapture(ctx context.Context) (context.Context, *RawCapture) {
capture := &RawCapture{}
return context.WithValue(ctx, rawCaptureKey{}, capture), capture
}
func rawCaptureFrom(ctx context.Context) *RawCapture {
capture, _ := ctx.Value(rawCaptureKey{}).(*RawCapture)
return capture
}
// resetResponse 在每次 HTTP 尝试前清空上一次的响应,保证留存的是最后一次尝试。
func (c *RawCapture) resetResponse() {
if c == nil {
return
}
c.HTTPStatus, c.Response, c.GatewayCode, c.GatewayMsg, c.TraceID = 0, nil, nil, "", ""
}