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

40
main.go
View File

@ -5,27 +5,49 @@ import (
"ZhenTuLocalPassiveAdapter/config"
"ZhenTuLocalPassiveAdapter/core"
"ZhenTuLocalPassiveAdapter/dto"
"ZhenTuLocalPassiveAdapter/telemetry"
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"log"
"os"
"time"
)
var tracer = otel.Tracer("vpt")
func startTask(device config.DeviceMapping, task dto.Task) {
fo, err := core.HandleTask(device, task)
ctx, span := tracer.Start(context.Background(), "startTask")
span.SetAttributes(attribute.String("deviceNo", device.DeviceNo))
span.SetAttributes(attribute.String("taskId", task.TaskID))
span.SetAttributes(attribute.String("scenicId", task.ScenicID))
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")))
fo, err := core.HandleTask(ctx, device, task)
if err != nil {
span.SetStatus(codes.Error, "处理任务失败")
log.Printf("处理任务失败, TaskID:【%s】, DeviceNo: %s, 错误: %v\n", task.TaskID, task.DeviceNo, err)
api.ReportTaskFailure(task.TaskID)
api.ReportTaskFailure(ctx, task.TaskID)
return
}
span.SetAttributes(attribute.String("fileUrl", fo.URL))
log.Printf("处理任务成功, TaskID:【%s】, DeviceNo: %s\n", task.TaskID, task.DeviceNo)
err = api.UploadTaskFile(task, *fo)
err = api.UploadTaskFile(ctx, task, *fo)
if err != nil {
span.SetStatus(codes.Error, "上传文件失败")
log.Printf("上传文件失败, TaskID:【%s】, DeviceNo: %s, 错误: %v\n", task.TaskID, task.DeviceNo, err)
api.ReportTaskFailure(task.TaskID)
api.ReportTaskFailure(ctx, task.TaskID)
return
}
result := api.ReportTaskSuccess(ctx, task.TaskID, fo)
if result {
span.SetStatus(codes.Error, "上报任务成功失败")
log.Printf("上报任务成功失败, TaskID:【%s】, DeviceNo: %s, 错误: %v\n", task.TaskID, task.DeviceNo, err)
return
}
span.SetStatus(codes.Ok, "上传文件成功")
log.Printf("上传文件成功, TaskID:【%s】, DeviceNo: %s\n", task.TaskID, task.DeviceNo)
api.ReportTaskSuccess(task.TaskID, fo)
}
func main() {
@ -36,7 +58,13 @@ func main() {
}
// 日志文件路径
logFilePath := "app.log"
ctx := context.Background()
shutdown, err := telemetry.InitTelemetry(ctx)
if err != nil {
log.Fatalf("Failed to initialize telemetry: %v", err)
return
}
defer shutdown(ctx)
// 创建或打开日志文件
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {