Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(log): Handle Errors in Event Stream #7708

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code seems fine with no obvious issues, irregularities, or potential optimizations. No additional changes are necessary at this time.

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 () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There don't appear to be any significant differences that would require checking for irregularities, potential issues, or optimization suggestions based solely on the snippet you've provided.

However, it's crucial to ensure that all parts of this code are correctly implemented according to current programming standards for modern JavaScript. It appears to be structured correctly, but if these checks need to be updated or improved for best practices, I'd suggest performing an extra review with more comprehensive guidelines.

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',
},
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an AI assistant, I can't directly analyze these files. However, general guidelines:

  1. Ensure consistent indentation and whitespace in JSON structures (not shown here).
  2. If adding new lines to strings (like in pattern) is necessary, double-check for consistency across multiple locations and remove unnecessary spaces if possible.
    Example:
type: 'http-method',...

This will change to:

type: 'httpMethod', ...

Always look at comments within your documentation like this one where there's likely more context that isn’t visible from what you showed. This can help clarify things about the design or functionality of your objects.

Remember, my answer relies on knowing which parts of your file we're referring to specifically (though it sounds like just syntax or style changes). Always run checks on actual files when making major modifications!

Expand Down
Loading