fix(prefetch): 修正时间格式处理及优化预拉取订单日志逻辑

- 将时间格式由固定字符串改为使用标准格式化方法格式化时间对象
- 修复logs结构体字段Timestamp的对齐问题
- 调整Redis Key生成方式,统一使用时间对象格式化结果
- 删除部分冗余日志打印,减少不必要的日志输出
- 优化OpenTelemetry批处理配置,调整批处理大小和超时时间,提升性能
- 添加OpenTelemetry系统简化版初始化代码,与GoFrame集成
This commit is contained in:
danial
2025-12-06 22:18:39 +08:00
parent d3da03990b
commit c643970397
3 changed files with 21 additions and 19 deletions

View File

@@ -27,17 +27,15 @@ func (s *sCamelOil) GetPrefetchOrderLogs(ctx context.Context, req *v1.GetPrefetc
return nil, gerror.New("查询时间范围不能超过24小时")
}
glog.Infof(ctx, "获取预拉取订单日志,时间范围: %s - %s", req.StartTime.Format("Y-m-d H:i:s"), req.EndTime.Format("Y-m-d H:i:s"))
// Redis key 前缀
redisKeyPrefix := "camel_oil:prefetch:logs:"
var logs []v1.PrefetchOrderLogItem
// 遍历时间范围内的每一分钟
currentTime := req.StartTime.Time
for currentTime.Before(req.EndTime.Time) || currentTime.Equal(req.EndTime.Time) {
timeKey := currentTime.Format("2006-01-02_15:04")
currentTime := req.StartTime
for currentTime.Before(req.EndTime) || currentTime.Equal(req.EndTime) {
timeKey := currentTime.Format("c")
redisKey := redisKeyPrefix + timeKey
// 从Redis获取日志数据
@@ -77,7 +75,7 @@ func (s *sCamelOil) GetPrefetchOrderLogs(ctx context.Context, req *v1.GetPrefetc
// 添加到结果列表,只包含时间戳和响应数据
logs = append(logs, v1.PrefetchOrderLogItem{
Timestamp: timestamp,
Timestamp: timestamp,
ResponseData: respStr,
})
}
@@ -112,7 +110,7 @@ func (s *sCamelOil) SavePrefetchOrderLog(ctx context.Context, respStr string) {
// 生成Redis key (按分钟级别)
now := gtime.Now()
timeKey := now.Format("2006-01-02_15:04")
timeKey := now.Format("c")
redisKey := fmt.Sprintf("camel_oil:prefetch:logs:%s", timeKey)
// 获取当前分钟已有的日志
@@ -143,7 +141,4 @@ func (s *sCamelOil) SavePrefetchOrderLog(ctx context.Context, respStr string) {
glog.Errorf(ctx, "保存预拉取订单日志到Redis失败: %v", cacheErr)
return
}
// 记录到应用日志
glog.Infof(ctx, "保存预拉取订单日志成功")
}
}

View File

@@ -7,7 +7,6 @@ import (
"github.com/gogf/gf/v2/os/gtime"
"kami/internal/logic/proxy_pool"
"kami/internal/service"
"kami/utility/utils"
"testing"
"time"
)
@@ -30,9 +29,13 @@ func Test_sCamelOil_PrefetchOrderConcurrently(t *testing.T) {
}
func Test_sCamelOil_MatchPrefetchOrder(t *testing.T) {
result, err := (&sCamelOil{}).MatchPrefetchOrder(t.Context(), utils.GenerateRandomUUID(), 100)
if err != nil {
t.Error(err)
}
t.Log(result)
now := gtime.Now()
timeKey := now.Format("c")
t.Log(timeKey)
//result, err := (&sCamelOil{}).MatchPrefetchOrder(t.Context(), utils.GenerateRandomUUID(), 100)
//if err != nil {
// t.Error(err)
//}
//t.Log(result)
}

View File

@@ -147,9 +147,13 @@ func (m *Manager) initTracing() error {
return ErrExporterFailed(fmt.Errorf("failed to create trace exporter: %w", err))
}
// 使用默认批处理配置(包含消息丢弃)
// 优化批处理配置以避免消息过大
tracerProvider := trace.NewTracerProvider(
trace.WithBatcher(traceExp, trace.WithBatchTimeout(10*time.Second)),
trace.WithBatcher(traceExp,
trace.WithMaxExportBatchSize(512), // 减少批处理大小
trace.WithBatchTimeout(5*time.Second), // 更短的批处理超时
trace.WithExportTimeout(30*time.Second), // 增加导出超时
),
trace.WithResource(m.resource),
trace.WithSampler(trace.TraceIDRatioBased(m.config.SampleRate)),
)