Skip to content

Commit

Permalink
perf: 记录会话生命周期日志
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeEirc committed Feb 8, 2024
1 parent f8fe7c4 commit 6ac1349
Show file tree
Hide file tree
Showing 11 changed files with 875 additions and 277 deletions.
24 changes: 22 additions & 2 deletions cmd/common/uploader_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (u *UploaderService) UploadReplay(sid, replayPath string) error {
logger.Errorf("Retrieve session %s detail failed: %s", sid, err)
return err
}
u.recordingSessionLifecycleReplay(sid, model.ReplayUploadStart, "")
today := sess.DateStart.UTC().Format(dateTimeFormat)
absGzFile := replayPath
if !isGzipFile(absGzFile) {
Expand All @@ -169,20 +170,29 @@ func (u *UploaderService) UploadReplay(sid, replayPath string) error {
replayBackend := u.getReplayBackend()
gzFilename := filepath.Base(absGzFile)
target := strings.Join([]string{today, gzFilename}, "/")
err = replayBackend.Upload(absGzFile, target)
replayBackendName := replayBackend.TypeName()
if replayBackendName == "null" {
reason := string(model.ReasonErrNullStorage)
u.recordingSessionLifecycleReplay(sid, model.ReplayUploadFailure, reason)
_ = os.Remove(absGzFile)
return nil
}

err = replayBackend.Upload(absGzFile, target)
if err != nil && replayBackendName != "server" {
u.recordingSessionLifecycleReplay(sid, model.ReplayUploadFailure, err.Error())
logger.Errorf("Uploader service replay backend %s error %s", replayBackendName, err)
logger.Error("Switch default server to upload replay %s.", absGzFile)
replayBackendName = "server"
u.recordingSessionLifecycleReplay(sid, model.ReplayUploadStart, "")
err = u.apiClient.Upload(sid, absGzFile)
}
if err != nil {
u.recordingSessionLifecycleReplay(sid, model.ReplayUploadFailure, err.Error())
logger.Errorf("Uploader service replay %s uploaded error: %s", absGzFile, err)
return err
}
logger.Infof("Uploader service replay file %s upload to %s", absGzFile, replayBackendName)

if _, err = u.apiClient.FinishReply(sid); err != nil {
logger.Errorf("Finish %s replay api failed: %s", sid, err)
return err
Expand All @@ -208,13 +218,16 @@ func (u *UploaderService) UploadRemainReplays(replayDir string) (ret RemainRepla
logger.Debugf("Upload Remain %d replay files", len(allRemainReplays))
for replayPath := range allRemainReplays {
remainReplay := allRemainReplays[replayPath]
u.recordingSessionLifecycleReplay(remainReplay.Id, model.ReplayUploadStart, "")
if err := u.uploadRemainReplay(&remainReplay); err != nil {
logger.Errorf("Uploader service clean remain replay %s failed: %s",
replayPath, err)
failureFiles = append(failureFiles, replayPath)
failureErrs = append(failureErrs, err.Error())
u.recordingSessionLifecycleReplay(remainReplay.Id, model.ReplayUploadFailure, err.Error())
continue
}
u.recordingSessionLifecycleReplay(remainReplay.Id, model.ReplayUploadSuccess, "")
successFiles = append(successFiles, replayPath)
// 上传完成 删除原录像文件
if err := os.Remove(replayPath); err != nil {
Expand All @@ -232,6 +245,13 @@ func (u *UploaderService) UploadRemainReplays(replayDir string) (ret RemainRepla
return
}

func (u *UploaderService) recordingSessionLifecycleReplay(sid string, event model.LifecycleEvent, msgErr string) {
logObj := model.SessionLifecycleLog{Reason: msgErr}
if err := u.apiClient.RecordSessionLifecycleLog(sid, event, logObj); err != nil {
logger.Errorf("Record session %s activity %s failed: %s", sid, event, err)
}
}

func (u *UploaderService) uploadRemainReplay(replay *RemainReplay) error {
replayAbsGzPath := replay.AbsFilePath
if !isGzipFile(replayAbsGzPath) {
Expand Down
16 changes: 16 additions & 0 deletions cmd/impl/convert_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,19 @@ func ConvertToReqInfo(req *pb.ReqInfo) model.ReqInfo {
URL: req.GetUrl(),
}
}

var LifecycleEventMap = map[pb.SessionLifecycleLogRequest_EventType]model.LifecycleEvent{
pb.SessionLifecycleLogRequest_AssetConnectSuccess: model.AssetConnectSuccess,
pb.SessionLifecycleLogRequest_AssetConnectFinished: model.AssetConnectFinished,
pb.SessionLifecycleLogRequest_CreateShareLink: model.CreateShareLink,
pb.SessionLifecycleLogRequest_UserJoinSession: model.UserJoinSession,
pb.SessionLifecycleLogRequest_UserLeaveSession: model.UserLeaveSession,
pb.SessionLifecycleLogRequest_AdminJoinMonitor: model.AdminJoinMonitor,
pb.SessionLifecycleLogRequest_AdminExitMonitor: model.AdminExitMonitor,
pb.SessionLifecycleLogRequest_ReplayConvertStart: model.ReplayConvertStart,
pb.SessionLifecycleLogRequest_ReplayConvertSuccess: model.ReplayUploadStart,
pb.SessionLifecycleLogRequest_ReplayConvertFailure: model.ReplayConvertFailure,
pb.SessionLifecycleLogRequest_ReplayUploadStart: model.ReplayConvertStart,
pb.SessionLifecycleLogRequest_ReplayUploadSuccess: model.ReplayUploadSuccess,
pb.SessionLifecycleLogRequest_ReplayUploadFailure: model.ReplayUploadFailure,
}
29 changes: 29 additions & 0 deletions cmd/impl/jms_record_session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package impl

import (
"context"

"github.com/jumpserver/wisp/pkg/jms-sdk-go/model"
"github.com/jumpserver/wisp/pkg/logger"
pb "github.com/jumpserver/wisp/protobuf-go/protobuf"
)

func (j *JMServer) RecordSessionLifecycleLog(ctx context.Context, req *pb.SessionLifecycleLogRequest) (*pb.StatusResponse, error) {
var (
status pb.Status
)
sessionId := req.GetSessionId()
event := req.GetEvent()
reason := req.GetReason()
user := req.GetUser()
logObj := model.SessionLifecycleLog{Reason: reason, User: user}
lifecycleEvent := LifecycleEventMap[event]
logger.Infof("Request record session %s lifecyle %s : %s", sessionId, event, logObj)
if err := j.apiClient.RecordSessionLifecycleLog(sessionId, lifecycleEvent, logObj); err != nil {
logger.Errorf("Record session %s lifecyle failed: %s", sessionId, err)
status.Err = err.Error()
return &pb.StatusResponse{Status: &status}, nil
}
status.Ok = true
return &pb.StatusResponse{Status: &status}, nil
}
38 changes: 38 additions & 0 deletions pkg/jms-sdk-go/model/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,41 @@ const (
LoginFromRT LabelFiled = "RT"
LoginFromDT LabelFiled = "DT"
)

type LifecycleEvent string

const (
AssetConnectSuccess LifecycleEvent = "asset_connect_success"
AssetConnectFinished LifecycleEvent = "asset_connect_finished"
CreateShareLink LifecycleEvent = "create_share_link"
UserJoinSession LifecycleEvent = "user_join_session"
UserLeaveSession LifecycleEvent = "user_leave_session"
AdminJoinMonitor LifecycleEvent = "admin_join_monitor"
AdminExitMonitor LifecycleEvent = "admin_exit_monitor"
ReplayConvertStart LifecycleEvent = "replay_convert_start"
ReplayConvertSuccess LifecycleEvent = "replay_convert_success"
ReplayConvertFailure LifecycleEvent = "replay_convert_failure"
ReplayUploadStart LifecycleEvent = "replay_upload_start"
ReplayUploadSuccess LifecycleEvent = "replay_upload_success"
ReplayUploadFailure LifecycleEvent = "replay_upload_failure"
)

type SessionLifecycleLog struct {
Reason string `json:"reason"`
User string `json:"user"`
}

var EmptyLifecycleLog = SessionLifecycleLog{}

type SessionLifecycleReasonErr string

const (
ReasonErrConnectFailed SessionLifecycleReasonErr = "connect_failed"
ReasonErrConnectDisconnect SessionLifecycleReasonErr = "connect_disconnect"
ReasonErrUserClose SessionLifecycleReasonErr = "user_close"
ReasonErrIdleDisconnect SessionLifecycleReasonErr = "idle_disconnect"
ReasonErrAdminTerminate SessionLifecycleReasonErr = "admin_terminate"
ReasonErrMaxSessionTimeout SessionLifecycleReasonErr = "max_session_timeout"
ReasonErrPermissionExpired SessionLifecycleReasonErr = "permission_expired"
ReasonErrNullStorage SessionLifecycleReasonErr = "null_storage"
)
24 changes: 24 additions & 0 deletions pkg/jms-sdk-go/service/jms_session_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package service

import (
"fmt"

"github.com/jumpserver/wisp/pkg/jms-sdk-go/model"
)

func (s *JMService) RecordSessionLifecycleLog(sid string, event model.LifecycleEvent, logObj model.SessionLifecycleLog) (err error) {
data := map[string]interface{}{
"event": event,
}
if logObj.Reason != "" {
data["reason"] = logObj.Reason
}
if logObj.User != "" {
data["user"] = logObj.User
}

reqURL := fmt.Sprintf(SessionLifecycleLogURL, sid)
var resp map[string]interface{}
_, err = s.authClient.Post(reqURL, data, &resp)
return
}
2 changes: 2 additions & 0 deletions pkg/jms-sdk-go/service/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
FinishTaskURL = "/api/v1/terminal/tasks/%s/"
JoinRoomValidateURL = "/api/v1/terminal/sessions/join/validate/"
FTPLogListURL = "/api/v1/audits/ftp-logs/" // 上传 ftp日志

SessionLifecycleLogURL = "/api/v1/terminal/sessions/%s/lifecycle_log/"
)

// 授权相关API
Expand Down
Loading

0 comments on commit 6ac1349

Please sign in to comment.