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

[APP-6612] Support downloading logs #4660

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

JosephBorodach
Copy link

@JosephBorodach JosephBorodach commented Dec 31, 2024

This PR implements enhancements to the machines logs CLI command, including support for writing logs to a file in text or json format, while maintaining the ability to print logs to stdout. Below are the details and test cases for the command:

See ticket here

Print text format to console (stdout)

Command

go run cli/viam/main.go machines logs -machine="1" --organization="viam-dev" --location="office temp" --format="text"

Output

Screenshot 2025-01-06 at 10 38 04 AM

Print json format to console (stdout)

Command

go run cli/viam/main.go machines logs -machine="1" --organization="viam-dev" --location="office temp" --format="json"

Output

Screenshot 2025-01-06 at 10 42 56 AM

Write logs to a file in text format

Command

go run cli/viam/main.go machines logs -machine="1" --organization="viam-dev" --location="office temp" --output="logs.txt" --format="text"

Output

Screenshot 2025-01-06 at 10 41 03 AM

Write logs to a file in json format

Command

go run cli/viam/main.go machines logs -machine="1" --organization="viam-dev" --location="office temp" --output="logs.json" --format="json"

Output

Screenshot 2025-01-06 at 10 43 46 AM

Invalid: Without any arguments

Command

go run cli/viam/main.go machines logs

Output

Screenshot 2025-01-02 at 12 50 24 PM

Missing format args, will default to text format and print logs to console

Command

go run cli/viam/main.go machines logs --machine="1" --organization="viam-dev" --location="office temp"

Output

Screenshot 2025-01-06 at 11 58 00 AM

Invalid format - will print error to console

Command

go run cli/viam/main.go machines logs --machine="1" --organization="viam-dev" --location="office temp" --format="foo"

Output

Screenshot 2025-01-06 at 12 01 47 PM

@JosephBorodach JosephBorodach requested a review from a team as a code owner December 31, 2024 17:13
@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label Dec 31, 2024
@JosephBorodach JosephBorodach force-pushed the APP-6612-Support-Downloading-Logs branch from 9e3765b to 2274d7a Compare December 31, 2024 19:56
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@JosephBorodach JosephBorodach force-pushed the APP-6612-Support-Downloading-Logs branch from 2274d7a to 788f538 Compare December 31, 2024 20:09
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@JosephBorodach JosephBorodach force-pushed the APP-6612-Support-Downloading-Logs branch from 788f538 to c7849be Compare December 31, 2024 20:24
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@JosephBorodach JosephBorodach force-pushed the APP-6612-Support-Downloading-Logs branch from c7849be to 0d596a6 Compare December 31, 2024 20:34
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@JosephBorodach JosephBorodach force-pushed the APP-6612-Support-Downloading-Logs branch from 0d596a6 to 386db8c Compare December 31, 2024 20:41
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@JosephBorodach JosephBorodach requested a review from jr22 December 31, 2024 20:49
@dgottlieb
Copy link
Member

Dumb question -- were you aware we already had a way to download logs?

$ ./viam-cli robot log --help
NAME:
   viam machines logs - display machine logs

USAGE:
   viam machines logs --machine=<machine> [other options]

OPTIONS:
   --count value                   number of logs to fetch (max 10000) (default: 100)
   --errors                        show only errors (default: false)
   --location value                (default: first location alphabetically)
   --machine value, --robot value  
   --organization value            (default: first organization alphabetically)

It's admittedly a bit unwieldy to discover that.

@JosephBorodach
Copy link
Author

Dumb question -- were you aware we already had a way to download logs?

$ ./viam-cli robot log --help
NAME:
   viam machines logs - display machine logs

USAGE:
   viam machines logs --machine=<machine> [other options]

OPTIONS:
   --count value                   number of logs to fetch (max 10000) (default: 100)
   --errors                        show only errors (default: false)
   --location value                (default: first location alphabetically)
   --machine value, --robot value  
   --organization value            (default: first organization alphabetically)

It's admittedly a bit unwieldy to discover that.

@dgottlieb - I’m aware that we already have a way to download logs, but the current method prints them directly to the console. The new feature addresses interest in being able to save logs as a file (e.g., in text or JSON formats) for easier consumption, sharing, or further processing. I think this enhances the usability of the tool.

@dgottlieb
Copy link
Member

Ah got it! The ticket wasn't clear on exactly what functionality was missing. I'm going to (strongly) recommend not adding another command altogether. And instead add this functionality to the existing logs command. But I suppose SDK is ultimately in charge of that.

From what you said, this adds to bits of functionality:

  • Downloading instead of printing
  • Being able to choose between text vs json outputs

If you really* want to add an option for downloading, that's fine. But it is (very) typical to turn text output into a file with shell redirection, e.g:

$ ./viam-cli print-logs > myfile.txt

But certainly for some commands (like curl) they default to stdout, but can additionally write to a file (using -o is traditional. Short for --output_file).

The other bit you mentioned -- giving the user a choice between text and json is very cool! I think you already went with --format=<text/json> which is exactly what I would recommend!

@JosephBorodach
Copy link
Author

Ah got it! The ticket wasn't clear on exactly what functionality was missing. I'm going to (strongly) recommend not adding another command altogether. And instead add this functionality to the existing logs command. But I suppose SDK is ultimately in charge of that.

From what you said, this adds to bits of functionality:

  • Downloading instead of printing
  • Being able to choose between text vs json outputs

If you really* want to add an option for downloading, that's fine. But it is (very) typical to turn text output into a file with shell redirection, e.g:

$ ./viam-cli print-logs > myfile.txt

But certainly for some commands (like curl) they default to stdout, but can additionally write to a file (using -o is traditional. Short for --output_file).

The other bit you mentioned -- giving the user a choice between text and json is very cool! I think you already went with --format=<text/json> which is exactly what I would recommend!

@dgottlieb - Great point! I agree with your suggestion to extend the current functionality of the logs command rather than introducing a new command - it ensures consistency and avoids redundancy in the CLI. I'll proceed to modify the logs command to support saving logs to a file.

cli/utils.go Outdated
switch format {
case "json":
var err error
output, err = json.MarshalIndent(logs, "", " ")
Copy link
Member

Choose a reason for hiding this comment

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

When outputting data as json, I think it's customary to output:

{"ts": 123, "message": "log line 1"}
{"ts": 124, "message": "log line 2"}

IIUC, this will instead output an array with 2 items:

[
  {"ts": 123, "message": "log line 1"},
  {"ts": 124, "message": "log line 2"}
]

Related to the above, the reason is because it is often difficult for json parsers/utilities to work with large lists efficiently/ergonomically.

This feels like a recipe to OOM. I've downloaded multi-GB log files

cli/utils.go Outdated Show resolved Hide resolved
cli/utils.go Outdated Show resolved Hide resolved
@jr22
Copy link
Member

jr22 commented Jan 2, 2025

Haven't looked at the code yet but adding my own context on the work-- ideally this would bring the export logs experience closer to the export data and logs UI experience which supports time ranges, filters, etc. I'm not super familiar with the existing command's code but would imagine theres also some room for improvement for downloading large amounts of logs. Basically reducing friction when person 1 is looking at these specific logs in the UI and wants to send these exact logs to person 2 that doesnt have access to the machine without copying and pasting from the UI and getting gross formatting. part 2 of this work will be to surface this command (prepopulated with values when possible) in the UI (like we do for data) so that its more discoverable

@viambot viambot removed the safe to test This pull request is marked safe to test from a trusted zone label Jan 2, 2025
@JosephBorodach JosephBorodach force-pushed the APP-6612-Support-Downloading-Logs branch from 72c0738 to 921a908 Compare January 2, 2025 22:56
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 2, 2025
@jr22
Copy link
Member

jr22 commented Jan 3, 2025

removing myself since dan is already reviewing. if there is anything core specific that you want my opinion on, please retag me in a comment. my one request is that we do a follow up pr to include things that i mentioned in my comment above such as filters and time span. thanks joseph!

@jr22 jr22 removed their request for review January 3, 2025 14:56
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
@JosephBorodach
Copy link
Author

removing myself since dan is already reviewing. if there is anything core specific that you want my opinion on, please retag me in a comment. my one request is that we do a follow up pr to include things that i mentioned in my comment above such as filters and time span. thanks joseph!

I am going to stack another pr onto this one.

…t arguments to be paired - either both are provided, or an error will be displated
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
cli/client.go Outdated
@@ -619,6 +624,14 @@ func RobotsLogsAction(c *cli.Context, args robotsLogsArgs) error {
return err
}

// Validate required arguments
if args.Output != "" && args.Format == "" {
Copy link
Author

Choose a reason for hiding this comment

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

[todo] remove

cli/client.go Outdated
}

// fetchAndSaveLogs fetches logs for all parts incrementally and saves them to a file.
func (c *viamClient) fetchAndSaveLogs(parts []*apppb.RobotPart, args robotsLogsArgs) error {
Copy link
Author

Choose a reason for hiding this comment

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

[todo] have fetchAndSaveLogs io.writer, determine the formatting accordingly, and then write the formatted log to io.writer using fmt.fPrintln. Combine fetchAndSaveLogs and printLogsToConsole (essentially removing printLogsToConsole).

cli/client.go Outdated
if args.Format == formatJSON {
// Each log as a standalone JSON object
if _, err := file.WriteString(formattedLog + "\n"); err != nil {
return errors.Wrap(err, "failed to write log to file")
Copy link
Author

Choose a reason for hiding this comment

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

[todo] Change to fmt.fPrintln and accept a io.writter. [In order to do this we will need to change the signature of the function]

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 6, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 6, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 6, 2025
Copy link
Member

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

Nice!

logsFlagOutputFile = "output"
logsFlagErrors = "errors"
logsFlagTail = "tail"
logsFlagCount = "count"
Copy link
Member

Choose a reason for hiding this comment

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

Are we still limited to 10k log fetch at once? /Is a future ticket adding date ranges for log-grabbing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test This pull request is marked safe to test from a trusted zone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants