From 94e1f662882fa24b79057ed49f8963fd2b8f2455 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sun, 13 Apr 2025 12:31:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=92=E5=BA=8F=EF=BC=8C?= =?UTF-8?q?=E6=9B=B4=E5=A4=9A=E5=9F=8B=E7=82=B9=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/task.go | 5 ++--- telemetry/init.go | 1 - util/ffmpeg.go | 11 ++++++++--- util/file_filter.go | 4 ++-- util/json.go | 11 +++++++++++ 5 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 util/json.go diff --git a/core/task.go b/core/task.go index 87ffd81..1d997fc 100644 --- a/core/task.go +++ b/core/task.go @@ -21,9 +21,8 @@ func HandleTask(ctx context.Context, device config.DeviceMapping, task dto.Task) subCtx, span := tracer.Start(ctx, "HandleTask") defer span.End() adapter := fs.GetAdapter() - span.SetAttributes(attribute.String("taskId", task.TaskID)) - span.SetAttributes(attribute.String("startTime", task.StartTime.Format("2006-01-02 15:04:05"))) - span.SetAttributes(attribute.String("endTime", task.EndTime.Format("2006-01-02 15:04:05"))) + span.SetAttributes(attribute.String("task.id", task.TaskID)) + span.SetAttributes(attribute.String("task", util.ToJson(task))) span.SetAttributes(attribute.String("device.no", device.DeviceNo)) span.SetAttributes(attribute.String("device.name", device.Name)) fileList, err := adapter.GetFileList( diff --git a/telemetry/init.go b/telemetry/init.go index 3adead3..72f665e 100644 --- a/telemetry/init.go +++ b/telemetry/init.go @@ -59,7 +59,6 @@ func newJaegerTraceProvider(ctx context.Context) (*trace.TracerProvider, error) // 创建一个使用 HTTP 协议连接本机Jaeger的 Exporter res, err := resource.New(ctx, resource.WithFromEnv(), - resource.WithProcess(), resource.WithTelemetrySDK(), resource.WithHost(), resource.WithAttributes( diff --git a/util/ffmpeg.go b/util/ffmpeg.go index cb61f75..7e6733e 100644 --- a/util/ffmpeg.go +++ b/util/ffmpeg.go @@ -12,6 +12,7 @@ import ( "os" "os/exec" "path" + "sort" "strconv" "strings" "sync" @@ -147,6 +148,10 @@ func CheckFileCoverageAndConstructTask(ctx context.Context, fileList []dto.File, log.Printf("无法根据要求找到对应录制片段!ID:【%s】,开始时间:【%s】,结束时间:【%s】", task.TaskID, beginDt, endDt) return nil, fmt.Errorf("无法根据要求找到对应录制片段") } + // 按照 Create 的值升序排序 + sort.Slice(fileList, func(i, j int) bool { + return fileList[i].StartTime.Unix() <= fileList[j].StartTime.Unix() + }) // 如果片段在中间断开时间过长 if len(fileList) > 1 { @@ -180,7 +185,7 @@ func CheckFileCoverageAndConstructTask(ctx context.Context, fileList []dto.File, Offset: int(beginDt.Sub(fileList[0].StartTime).Seconds()), OutputFile: path.Join(os.TempDir(), task.TaskID+".mp4"), } - span.SetAttributes(attribute.Int("task.files", len(ffmpegTask.Files))) + span.SetAttributes(attribute.String("task.files", ToJson(ffmpegTask.Files))) span.SetAttributes(attribute.Int("task.offset", ffmpegTask.Offset)) span.SetAttributes(attribute.Int("task.length", ffmpegTask.Length)) span.SetStatus(codes.Ok, "FFMPEG任务构造成功") @@ -317,7 +322,7 @@ func SlowVideoCut(ctx context.Context, inputFiles []dto.File, offset, length int func handleFfmpegProcess(ctx context.Context, ffmpegCmd []string) (bool, error) { _, span := tracer.Start(ctx, "handleFfmpegProcess") defer span.End() - span.SetAttributes(attribute.String("ffmpeg.cmd", strings.Join(ffmpegCmd, " "))) + span.SetAttributes(attribute.String("ffmpeg.cmd", ToJson(ffmpegCmd))) startTime := time.Now() defer func() { span.SetAttributes(attribute.Int64("ffmpeg.duration", int64(time.Since(startTime).Seconds()))) @@ -371,7 +376,7 @@ func GetVideoDuration(ctx context.Context, filePath string) (float64, error) { "-of", "default=noprint_wrappers=1:nokey=1", filePath, } - span.SetAttributes(attribute.String("ffprobe.cmd", strings.Join(ffprobeCmd, " "))) + span.SetAttributes(attribute.String("ffprobe.cmd", ToJson(ffprobeCmd))) cmd := exec.Command(ffprobeCmd[0], ffprobeCmd[1:]...) var out bytes.Buffer cmd.Stdout = &out diff --git a/util/file_filter.go b/util/file_filter.go index f4e0629..9c8b76d 100644 --- a/util/file_filter.go +++ b/util/file_filter.go @@ -33,9 +33,9 @@ func FilterAndSortFiles(fileList []dto.File, beginDt, endDt time.Time) []dto.Fil } } - // 按照 DiffMs 的值降序排序 + // 按照 Create 的值升序排序 sort.Slice(filteredFiles, func(i, j int) bool { - return filteredFiles[i].DiffMs > filteredFiles[j].DiffMs + return filteredFiles[i].StartTime.Unix() <= filteredFiles[j].StartTime.Unix() }) return filteredFiles diff --git a/util/json.go b/util/json.go new file mode 100644 index 0000000..b6f47d0 --- /dev/null +++ b/util/json.go @@ -0,0 +1,11 @@ +package util + +import "encoding/json" + +func ToJson(v interface{}) string { + b, err := json.Marshal(v) + if err != nil { + return "" + } + return string(b) +}