tracer层级问题

This commit is contained in:
Jerry Yan 2025-04-13 12:09:49 +08:00
parent b11a315b0d
commit dc10092f7a
3 changed files with 27 additions and 25 deletions

View File

@ -14,12 +14,14 @@ import (
)
func UploadTaskFile(ctx context.Context, task dto.Task, file dto.FileObject) error {
url, err := QueryUploadUrlForTask(ctx, task.TaskID)
subCtx, span := tracer.Start(ctx, "UploadTaskFile")
defer span.End()
url, err := QueryUploadUrlForTask(subCtx, task.TaskID)
if err != nil {
return err
}
log.Printf("开始上传文件, URL【%s】\n", url)
if err := OssUpload(ctx, url, file.URL); err != nil {
if err := OssUpload(subCtx, url, file.URL); err != nil {
return err
}
return nil

View File

@ -19,7 +19,7 @@ type LocalAdapter struct {
}
func (l *LocalAdapter) GetFileList(ctx context.Context, dirPath string, relDt time.Time) ([]dto.File, error) {
_, span := tracer.Start(ctx, "GetFileList_local")
subCtx, span := tracer.Start(ctx, "GetFileList_local")
defer span.End()
if l.StorageConfig.Path == "" {
span.SetAttributes(attribute.String("error", "未配置存储路径"))
@ -54,7 +54,7 @@ func (l *LocalAdapter) GetFileList(ctx context.Context, dirPath string, relDt ti
if startTime.Equal(stopTime) || stopTime.IsZero() {
// 如果文件名没有时间戳,则认为该文件是未录制完成的
// 尝试读取一下视频信息
duration, err := util.GetVideoDuration(ctx, path.Join(l.StorageConfig.Path, dirPath, file.Name()))
duration, err := util.GetVideoDuration(subCtx, path.Join(l.StorageConfig.Path, dirPath, file.Name()))
if err != nil {
// 如果还是没有,就按照配置文件里的加起来
stopTime = stopTime.Add(time.Second * time.Duration(config.Config.Record.Duration))

View File

@ -21,15 +21,15 @@ import (
const FfmpegExec = "ffmpeg"
func RunFfmpegTask(ctx context.Context, task *dto.FfmpegTask) bool {
_, span := tracer.Start(ctx, "RunFfmpegTask")
subCtx, span := tracer.Start(ctx, "RunFfmpegTask")
defer span.End()
var result bool
if len(task.Files) == 1 {
// 单个文件切割,用简单方法
result = runFfmpegForSingleFile(ctx, task)
result = runFfmpegForSingleFile(subCtx, task)
} else {
// 多个文件切割,用速度快的
result = runFfmpegForMultipleFile1(ctx, task)
result = runFfmpegForMultipleFile1(subCtx, task)
}
// 先尝试方法1
if result {
@ -38,7 +38,7 @@ func RunFfmpegTask(ctx context.Context, task *dto.FfmpegTask) bool {
}
log.Printf("FFMPEG简易方法失败尝试复杂方法转码")
// 不行再尝试方法二
result = runFfmpegForMultipleFile2(ctx, task)
result = runFfmpegForMultipleFile2(subCtx, task)
if result {
span.SetStatus(codes.Ok, "FFMPEG复杂方法成功")
return true
@ -48,7 +48,7 @@ func RunFfmpegTask(ctx context.Context, task *dto.FfmpegTask) bool {
}
func runFfmpegForMultipleFile1(ctx context.Context, task *dto.FfmpegTask) bool {
_, span := tracer.Start(ctx, "runFfmpegForMultipleFile1")
subCtx, span := tracer.Start(ctx, "runFfmpegForMultipleFile1")
defer span.End()
// 多文件方法一先转换成ts然后合并切割
// 步骤一先转换成ts并行转换
@ -61,7 +61,7 @@ func runFfmpegForMultipleFile1(ctx context.Context, task *dto.FfmpegTask) bool {
go func(file *dto.File) {
defer wg.Done()
tmpFile := path.Join(os.TempDir(), file.Name+".ts")
result, err := convertMp4ToTs(ctx, *file, tmpFile)
result, err := convertMp4ToTs(subCtx, *file, tmpFile)
if err != nil {
log.Printf("转码出错: %v", err)
mu.Lock()
@ -88,7 +88,7 @@ func runFfmpegForMultipleFile1(ctx context.Context, task *dto.FfmpegTask) bool {
}
// 步骤二使用concat协议拼接裁切
result, err := QuickConcatVideoCut(ctx, task.Files, int64(task.Offset), int64(task.Length), task.OutputFile)
result, err := QuickConcatVideoCut(subCtx, task.Files, int64(task.Offset), int64(task.Length), task.OutputFile)
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "FFMPEG多文件concat协议转码失败")
@ -110,10 +110,10 @@ func runFfmpegForMultipleFile1(ctx context.Context, task *dto.FfmpegTask) bool {
}
func runFfmpegForMultipleFile2(ctx context.Context, task *dto.FfmpegTask) bool {
_, span := tracer.Start(ctx, "runFfmpegForMultipleFile2")
subCtx, span := tracer.Start(ctx, "runFfmpegForMultipleFile2")
defer span.End()
// 多文件,方法二:使用计算资源编码
result, err := SlowVideoCut(ctx, task.Files, int64(task.Offset), int64(task.Length), task.OutputFile)
result, err := SlowVideoCut(subCtx, task.Files, int64(task.Offset), int64(task.Length), task.OutputFile)
if err != nil {
return false
}
@ -121,9 +121,9 @@ func runFfmpegForMultipleFile2(ctx context.Context, task *dto.FfmpegTask) bool {
}
func runFfmpegForSingleFile(ctx context.Context, task *dto.FfmpegTask) bool {
_, span := tracer.Start(ctx, "runFfmpegForSingleFile")
subCtx, span := tracer.Start(ctx, "runFfmpegForSingleFile")
defer span.End()
result, err := QuickVideoCut(ctx, task.Files[0].Url, int64(task.Offset), int64(task.Length), task.OutputFile)
result, err := QuickVideoCut(subCtx, task.Files[0].Url, int64(task.Offset), int64(task.Length), task.OutputFile)
if err != nil {
span.SetStatus(codes.Error, "FFMPEG单个文件裁切失败")
return false
@ -188,7 +188,7 @@ func CheckFileCoverageAndConstructTask(ctx context.Context, fileList []dto.File,
}
func convertMp4ToTs(ctx context.Context, file dto.File, outFileName string) (bool, error) {
_, span := tracer.Start(ctx, "convertMp4ToTs")
subCtx, span := tracer.Start(ctx, "convertMp4ToTs")
defer span.End()
ffmpegCmd := []string{
FfmpegExec,
@ -200,11 +200,11 @@ func convertMp4ToTs(ctx context.Context, file dto.File, outFileName string) (boo
"-f", "mpegts",
outFileName,
}
return handleFfmpegProcess(ctx, ffmpegCmd)
return handleFfmpegProcess(subCtx, ffmpegCmd)
}
func convertHevcToTs(ctx context.Context, file dto.File, outFileName string) (bool, error) {
_, span := tracer.Start(ctx, "convertHevcToTs")
subCtx, span := tracer.Start(ctx, "convertHevcToTs")
defer span.End()
ffmpegCmd := []string{
FfmpegExec,
@ -216,11 +216,11 @@ func convertHevcToTs(ctx context.Context, file dto.File, outFileName string) (bo
"-f", "mpegts",
outFileName,
}
return handleFfmpegProcess(ctx, ffmpegCmd)
return handleFfmpegProcess(subCtx, ffmpegCmd)
}
func QuickVideoCut(ctx context.Context, inputFile string, offset, length int64, outputFile string) (bool, error) {
_, span := tracer.Start(ctx, "QuickVideoCut")
subCtx, span := tracer.Start(ctx, "QuickVideoCut")
defer span.End()
ffmpegCmd := []string{
FfmpegExec,
@ -236,11 +236,11 @@ func QuickVideoCut(ctx context.Context, inputFile string, offset, length int64,
"-f", "mp4",
outputFile,
}
return handleFfmpegProcess(ctx, ffmpegCmd)
return handleFfmpegProcess(subCtx, ffmpegCmd)
}
func QuickConcatVideoCut(ctx context.Context, inputFiles []dto.File, offset, length int64, outputFile string) (bool, error) {
_, span := tracer.Start(ctx, "QuickConcatVideoCut")
subCtx, span := tracer.Start(ctx, "QuickConcatVideoCut")
defer span.End()
tmpFile := fmt.Sprintf("tmp%.10f.txt", rand.Float64())
tmpFileObj, err := os.Create(tmpFile)
@ -277,11 +277,11 @@ func QuickConcatVideoCut(ctx context.Context, inputFiles []dto.File, offset, len
"-f", "mp4",
outputFile,
}
return handleFfmpegProcess(ctx, ffmpegCmd)
return handleFfmpegProcess(subCtx, ffmpegCmd)
}
func SlowVideoCut(ctx context.Context, inputFiles []dto.File, offset, length int64, outputFile string) (bool, error) {
_, span := tracer.Start(ctx, "SlowVideoCut")
subCtx, span := tracer.Start(ctx, "SlowVideoCut")
defer span.End()
ffmpegCmd := []string{
FfmpegExec,
@ -311,7 +311,7 @@ func SlowVideoCut(ctx context.Context, inputFiles []dto.File, offset, length int
outputFile,
)
return handleFfmpegProcess(ctx, ffmpegCmd)
return handleFfmpegProcess(subCtx, ffmpegCmd)
}
func handleFfmpegProcess(ctx context.Context, ffmpegCmd []string) (bool, error) {