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

@ -3,11 +3,12 @@ package fs
import (
"ZhenTuLocalPassiveAdapter/config"
"ZhenTuLocalPassiveAdapter/dto"
"context"
"time"
)
type Adapter interface {
GetFileList(path string, relDt time.Time) ([]dto.File, error)
GetFileList(ctx context.Context, path string, relDt time.Time) ([]dto.File, error)
}
func GetAdapter() Adapter {

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
}

View File

@ -14,6 +14,8 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
type S3Adapter struct {
@ -43,8 +45,13 @@ func (s *S3Adapter) getClient() (*s3.Client, error) {
return s.s3Client, nil
}
func (s *S3Adapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, error) {
func (s *S3Adapter) GetFileList(ctx context.Context, dirPath string, relDt time.Time) ([]dto.File, error) {
_, span := tracer.Start(ctx, "GetFileList_s3")
defer span.End()
if s.StorageConfig.S3.Bucket == "" {
span.SetAttributes(attribute.String("error", "未配置S3存储桶"))
span.SetStatus(codes.Error, "未配置S3存储桶")
return nil, fmt.Errorf("未配置S3存储桶")
}
@ -55,6 +62,8 @@ func (s *S3Adapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, er
client, err := s.getClient()
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "创建S3客户端失败")
return nil, err
}
@ -68,6 +77,8 @@ func (s *S3Adapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, er
result, err := client.ListObjectsV2(context.TODO(), listObjectsInput)
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "文件列表读取失败")
return nil, err
}
@ -91,6 +102,8 @@ func (s *S3Adapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, er
presignOptions.Expires = 10 * time.Minute
})
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "生成预签名URL失败")
log.Println("Error presigning GetObject request:", err)
continue
}
@ -110,8 +123,10 @@ func (s *S3Adapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, er
continuationToken = result.NextContinuationToken
}
span.SetAttributes(attribute.Int("file.count", len(fileList)))
sort.Slice(fileList, func(i, j int) bool {
return fileList[i].StartTime.Before(fileList[j].StartTime)
})
span.SetStatus(codes.Ok, "文件读取成功")
return fileList, nil
}

5
fs/tracer.go Normal file
View File

@ -0,0 +1,5 @@
package fs
import "go.opentelemetry.io/otel"
var tracer = otel.Tracer("fs")