Skip to content

Commit

Permalink
feat(log): Handle Errors in Event Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 committed Jan 13, 2025
1 parent e2354b8 commit f968eac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
30 changes: 21 additions & 9 deletions agent/app/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,10 +811,8 @@ func (u *ContainerService) StreamLogs(ctx *gin.Context, params dto.StreamLog) {
}
return true
case err := <-errorChan:
_, err = fmt.Fprintf(w, "data: {\"event\": \"error\", \"data\": \"%s\"}\n\n", err.Error())
if err != nil {
return false
}
errorMsg := fmt.Sprintf("event: error\ndata: %v\n\n", err.Error())
_, err = fmt.Fprintf(w, errorMsg)
return false
case <-ctx.Request.Context().Done():
return false
Expand Down Expand Up @@ -843,14 +841,19 @@ func collectLogs(params dto.StreamLog, messageChan chan<- string, errorChan chan
if params.Container != "" {
cmdArgs = append(cmdArgs, params.Container)
}
cmd := exec.Command("docker", cmdArgs...)
dockerCmd := exec.Command("docker", cmdArgs...)

stdout, err := cmd.StdoutPipe()
stdout, err := dockerCmd.StdoutPipe()
if err != nil {
errorChan <- fmt.Errorf("failed to get stdout pipe: %v", err)
return
}
if err := cmd.Start(); err != nil {
stderr, err := dockerCmd.StderrPipe()
if err != nil {
errorChan <- fmt.Errorf("failed to get stderr pipe: %v", err)
return
}
if err = dockerCmd.Start(); err != nil {
errorChan <- fmt.Errorf("failed to start command: %v", err)
return
}
Expand All @@ -869,11 +872,20 @@ func collectLogs(params dto.StreamLog, messageChan chan<- string, errorChan chan
}
}

if err := scanner.Err(); err != nil {
if err = scanner.Err(); err != nil {
errorChan <- fmt.Errorf("scanner error: %v", err)
return
}
cmd.Wait()

errScanner := bufio.NewScanner(stderr)
for errScanner.Scan() {
line := errScanner.Text()
errorChan <- fmt.Errorf("%v", line)
}
if err = dockerCmd.Wait(); err != nil {
errorChan <- fmt.Errorf("%v", err)
return
}
}

func (u *ContainerService) DownloadContainerLogs(containerType, container, since, tail string, c *gin.Context) error {
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/container-log/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ const searchLogs = async () => {
}
});
};
eventSource.onerror = (event: MessageEvent) => {
stopListening();
MsgError(event.data);
};
};
const onDownload = async () => {
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/components/hightlight/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ const nginxRules: TokenRule[] = [
},
{
type: 'path',
pattern:
/(?:(?<=GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)\s+|(?<=open\(\s*")|(?<="\s*))(\/[^"\s]+(?:\.\w+)?(?:\?\w+=\w+)?)/g,
pattern: /(?<=[\s"])\/[^"\s]+(?:\.\w+)?(?:\?\w+=\w+)?/g,
color: '#B87A2B',
},
{
type: 'http-method',
pattern: /(?<=")(?:GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)(?=\s)/g,
pattern: /(?<=)(?:GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)(?=\s)/g,
color: '#27AE60',
},
{
Expand Down

0 comments on commit f968eac

Please sign in to comment.