telemetry

This commit is contained in:
2025-04-13 11:24:20 +08:00
parent a6c5ba5957
commit 3d989c2f47
14 changed files with 443 additions and 63 deletions

View File

@ -4,7 +4,10 @@ import (
"ZhenTuLocalPassiveAdapter/config"
"ZhenTuLocalPassiveAdapter/dto"
"ZhenTuLocalPassiveAdapter/util"
"context"
"fmt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"os"
"path"
"sort"
@ -15,15 +18,22 @@ type LocalAdapter struct {
StorageConfig config.StorageConfig
}
func (l *LocalAdapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, error) {
func (l *LocalAdapter) GetFileList(ctx context.Context, dirPath string, relDt time.Time) ([]dto.File, error) {
_, span := tracer.Start(ctx, "GetFileList_local")
defer span.End()
if l.StorageConfig.Path == "" {
span.SetAttributes(attribute.String("error", "未配置存储路径"))
span.SetStatus(codes.Error, "未配置存储路径")
return nil, fmt.Errorf("未配置存储路径")
}
// 读取文件夹下目录
files, err := os.ReadDir(path.Join(l.StorageConfig.Path, dirPath))
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "文件夹读取失败")
return nil, err
}
span.SetAttributes(attribute.Int("file.count", len(files)))
var fileList []dto.File
for _, file := range files {
@ -44,7 +54,7 @@ func (l *LocalAdapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File,
if startTime.Equal(stopTime) || stopTime.IsZero() {
// 如果文件名没有时间戳,则认为该文件是未录制完成的
// 尝试读取一下视频信息
duration, err := util.GetVideoDuration(path.Join(l.StorageConfig.Path, dirPath, file.Name()))
duration, err := util.GetVideoDuration(ctx, path.Join(l.StorageConfig.Path, dirPath, file.Name()))
if err != nil {
// 如果还是没有,就按照配置文件里的加起来
stopTime = stopTime.Add(time.Second * time.Duration(config.Config.Record.Duration))
@ -64,5 +74,6 @@ func (l *LocalAdapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File,
return fileList[i].StartTime.Before(fileList[j].StartTime)
})
}
span.SetStatus(codes.Ok, "文件读取成功")
return fileList, nil
}