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] Add Filters for Downloading Logs #4673

Merged
merged 31 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
386db8c
Support downloading logs
JosephBorodach Dec 31, 2024
232f143
Restructure RobotsLogsAction to extend functionality for outputing lo…
JosephBorodach Jan 2, 2025
6a02466
Refactor saveLogsToDisk to handle large log files efficiently
JosephBorodach Jan 2, 2025
e14e9be
Restructure fetchAndSaveLogs to write logs to the file while retrievi…
JosephBorodach Jan 2, 2025
761d6fe
Add nolint for not checking the error returned when closing the file
JosephBorodach Jan 2, 2025
05dc2c2
add headers to log output for file downloads
JosephBorodach Jan 2, 2025
921a908
Add constant variable for FormatJSON to appease linter. Add error che…
JosephBorodach Jan 2, 2025
690b887
Make the json format consecutive json object instead of one large jso…
JosephBorodach Jan 3, 2025
e2786a5
Make error handling more comprehensive for requiring Output and Forma…
JosephBorodach Jan 3, 2025
225b5cb
Make fetchAndSaveLogs, streamLogsForPart and printLogsToConsole metho…
JosephBorodach Jan 3, 2025
b0698af
Add filters including keyword, levels, and start and end times to the…
JosephBorodach Jan 3, 2025
b3e00d3
Merge remote-tracking branch 'upstream/main' into APP-6612-Add-Filter…
JosephBorodach Jan 8, 2025
bf0df5f
Replace dataFlagStart, dataFlagEnd, logsFlagStartTime and logsFlagEnd…
JosephBorodach Jan 8, 2025
7abd091
Add logic to streamLogsForPart for the filters
JosephBorodach Jan 8, 2025
0c6c6f9
Remove validation checks
JosephBorodach Jan 8, 2025
b292477
Remove constant variables for formatJSON and formatText
JosephBorodach Jan 8, 2025
0d65512
Correct cli argument types to strings from timestamps
JosephBorodach Jan 8, 2025
791cda4
Fix start issue
JosephBorodach Jan 13, 2025
774614b
Add parseTimeString helper method and refactor streamLogsForPart and …
JosephBorodach Jan 13, 2025
6a8138e
Remove logsFlagErrors flag from logs because it is redundant since th…
JosephBorodach Jan 13, 2025
410bad0
Permit the combination of start + count + end in RobotsLogsAction
JosephBorodach Jan 13, 2025
f281e27
Make error for 'args.Start != && args.Count > 0 && args.End == ' in …
JosephBorodach Jan 13, 2025
f35b373
Merge branch 'main' into APP-6612-Add-Filters-For-Downloading-Logs
JosephBorodach Jan 13, 2025
575f98e
Remove logsFlagErrors flag from logs because it is redundant since th…
JosephBorodach Jan 13, 2025
c164165
Fix bug to allow for more than 100 logs to be retrieved from streamLo…
JosephBorodach Jan 13, 2025
2f273e2
Fix useless print with f usage found Errorf
JosephBorodach Jan 14, 2025
ef5db1a
Fix parseTimeString - timeLayout always receives same timelayout
JosephBorodach Jan 14, 2025
b8a5d77
Cleanup streamLogsForPart logic
JosephBorodach Jan 14, 2025
21d27c6
Remove redundant comments from streamLogsForPart and add clarifying c…
JosephBorodach Jan 15, 2025
8a30121
Add examples for using the start and end time options for the logs co…
JosephBorodach Jan 15, 2025
0c77e33
Add todo comments to RobotsLogsAction
JosephBorodach Jan 15, 2025
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
33 changes: 24 additions & 9 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ const (
disableProfilesFlag = "disable-profiles"
profileFlagName = "profile-name"

generalFlagStart = "start"
generalFlagEnd = "end"

// TODO: RSDK-6683.
quietFlag = "quiet"

logsFlagFormat = "format"
logsFlagOutputFile = "output"
logsFlagKeyword = "keyword"
logsFlagLevels = "levels"
logsFlagErrors = "errors"
logsFlagTail = "tail"
logsFlagCount = "count"
Expand Down Expand Up @@ -114,8 +119,6 @@ const (
dataFlagResourceName = "resource-name"
dataFlagMethod = "method"
dataFlagMimeTypes = "mime-types"
dataFlagStart = "start"
dataFlagEnd = "end"
dataFlagParallelDownloads = "parallel"
dataFlagTags = "tags"
dataFlagBboxLabels = "bbox-labels"
Expand Down Expand Up @@ -211,11 +214,11 @@ var commonFilterFlags = []cli.Flag{
Usage: "mime types filter",
},
&cli.StringFlag{
Name: dataFlagStart,
Name: generalFlagStart,
Usage: "ISO-8601 timestamp in RFC3339 format indicating the start of the interval filter",
},
&cli.StringFlag{
Name: dataFlagEnd,
Name: generalFlagEnd,
Usage: "ISO-8601 timestamp in RFC3339 format indicating the end of the interval filter",
},
&cli.StringSliceFlag{
Expand Down Expand Up @@ -1060,12 +1063,12 @@ var app = &cli.App{
Usage: "orgs filter",
},
&cli.StringFlag{
Name: dataFlagStart,
Name: generalFlagStart,
Required: true,
Usage: "ISO-8601 timestamp in RFC3339 format indicating the start of the interval filter",
},
&cli.StringFlag{
Name: dataFlagEnd,
Name: generalFlagEnd,
Required: true,
Usage: "ISO-8601 timestamp in RFC3339 format indicating the end of the interval filter",
},
Expand Down Expand Up @@ -1792,9 +1795,21 @@ var app = &cli.App{
Name: logsFlagFormat,
Usage: "file format (text or json)",
},
&cli.BoolFlag{
Name: logsFlagErrors,
Usage: "show only errors",
&cli.StringFlag{
Name: logsFlagKeyword,
Usage: "filter logs by keyword",
},
&cli.StringSliceFlag{
Name: logsFlagLevels,
Usage: "filter logs by levels (e.g., info, warn, error)",
},
&cli.StringFlag{
Name: generalFlagStart,
Usage: "ISO-8601 timestamp in RFC3339 format indicating the start of the interval filter",
Copy link
Member

Choose a reason for hiding this comment

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

nit: may be nice to give an example of what that is for both this and the one below

},
&cli.StringFlag{
Name: generalFlagEnd,
Usage: "ISO-8601 timestamp in RFC3339 format indicating the end of the interval filter",
},
&cli.IntFlag{
Name: logsFlagCount,
Expand Down
56 changes: 41 additions & 15 deletions cli/client.go
Copy link
Contributor Author

@JosephBorodach JosephBorodach Jan 14, 2025

Choose a reason for hiding this comment

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

@jr22 - Is there a reason we don't have unit tests for this function's helpers? Should I add unit tests?

Copy link
Member

Choose a reason for hiding this comment

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

not a reason that im aware of, feel free to add test coverage if you want!

Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,10 @@ type robotsLogsArgs struct {
Machine string
Output string
Format string
Errors bool
Keyword string
Levels []string
Start string
End string
Count int
}

Expand All @@ -758,6 +761,16 @@ func RobotsLogsAction(c *cli.Context, args robotsLogsArgs) error {
return err
}

// Check if both start time and count are provided
if args.Start != "" && args.Count > 0 && args.End == "" {
return errors.New("unsupported functionality: specifying both a start time and a count without an end time is not supported. " +
Copy link
Member

Choose a reason for hiding this comment

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

do we need this? i feel like this is explaining weirdness in our code to our users which we shouldn't do.

May make more sense to make this a comment rather than something user facing and just say this is unsupported to the user

Copy link
Member

Choose a reason for hiding this comment

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

(I know this is out of scope, but this behaviour is also counter-intuitive to me). Other logs api's support this: https://docs.aws.amazon.com/ja_jp/AmazonCloudWatchLogs/latest/APIReference/API_StartQuery.html

so why don't we?

Copy link
Contributor Author

@JosephBorodach JosephBorodach Jan 15, 2025

Choose a reason for hiding this comment

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

I agree that this functionality is something we really need to support but it will require changing the app's api to support sorting the order of the logs (currently we default to descending order for time)

even though I would love to implement the sorting functionality now, it's out of the scope of the pr and I am not sure we want to prioritize more resources to this now. I think we wanted to leave this comment so that it screams out to us that it needs to be addressed

Copy link
Member

Choose a reason for hiding this comment

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

can you make a ticket to add this in as something in the backloig? and link this code?

I agree that theres nothing to change here as part of the ticket, but wondered if there was something we could do without having to change the API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call! I have a ticket cut, I will add it to the code

"This behavior can be counterintuitive because logs are currently only sorted in descending order. " +
"For example, if there are 200 logs after the specified start time and you request 10 logs, it will return the 10 most recent logs, " +
"rather than the 10 logs closest to the start time. " +
"Please provide either a start time and an end time to define a clear range, or a count without a start time for recent logs",
)
}

orgStr := args.Organization
locStr := args.Location
robotStr := args.Machine
Expand Down Expand Up @@ -823,32 +836,39 @@ func (c *viamClient) streamLogsForPart(part *apppb.RobotPart, args robotsLogsArg
return err
}

startTime, err := parseTimeString(args.Start)
if err != nil {
return errors.Wrap(err, "invalid start time format")
}
endTime, err := parseTimeString(args.End)
if err != nil {
return errors.Wrap(err, "invalid end time format")
}

// Write logs for this part
var pageToken string
for logsFetched := 0; logsFetched < numLogs; {
remainingLogs := int64(numLogs - logsFetched)
if remainingLogs > 100 {
remainingLogs = int64(100)
}
resp, err := c.client.GetRobotPartLogs(c.c.Context, &apppb.GetRobotPartLogsRequest{
Id: part.Id,
ErrorsOnly: args.Errors,
PageToken: &pageToken,
Id: part.Id,
Filter: &args.Keyword,
PageToken: &pageToken,
Levels: args.Levels,
Start: startTime,
End: endTime,
Limit: &remainingLogs,
})
if err != nil {
return errors.Wrap(err, "failed to fetch logs")
}

pageToken = resp.NextPageToken
// Break in the event of no logs in GetRobotPartLogsResponse or when
// page token is empty (no more pages).
if resp.Logs == nil || pageToken == "" {
if len(resp.Logs) == 0 {
break
}

// Truncate this intermediate slice of resp.Logs based on how many logs
// are still required by numLogs.
remainingLogsNeeded := numLogs - logsFetched
if remainingLogsNeeded < len(resp.Logs) {
resp.Logs = resp.Logs[:remainingLogsNeeded]
}

for _, log := range resp.Logs {
formattedLog, err := formatLog(log, part.Name, args.Format)
if err != nil {
Expand All @@ -861,6 +881,12 @@ func (c *viamClient) streamLogsForPart(part *apppb.RobotPart, args robotsLogsArg
}

logsFetched += len(resp.Logs)
pageToken = resp.NextPageToken

if pageToken == "" {
// End of pages
break
}
}

return nil
Expand Down
24 changes: 7 additions & 17 deletions cli/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/timestamppb"

"go.viam.com/rdk/data"
)
Expand Down Expand Up @@ -308,23 +307,14 @@ func createExportTabularRequest(c *cli.Context) (*datapb.ExportTabularDataReques
}

func createCaptureInterval(startStr, endStr string) (*datapb.CaptureInterval, error) {
var start *timestamppb.Timestamp
var end *timestamppb.Timestamp
timeLayout := time.RFC3339

if startStr != "" {
t, err := time.Parse(timeLayout, startStr)
if err != nil {
return nil, errors.Wrap(err, "could not parse start flag")
}
start = timestamppb.New(t)
start, err := parseTimeString(startStr)
if err != nil {
return nil, err
}
if endStr != "" {
t, err := time.Parse(timeLayout, endStr)
if err != nil {
return nil, errors.Wrap(err, "could not parse end flag")
}
end = timestamppb.New(t)

end, err := parseTimeString(endStr)
if err != nil {
return nil, err
}

return &datapb.CaptureInterval{
Expand Down
15 changes: 15 additions & 0 deletions cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"github.com/pkg/errors"
apppb "go.viam.com/api/app/v1"
"google.golang.org/protobuf/types/known/timestamppb"
)

// samePath returns true if abs(path1) and abs(path2) are the same.
Expand Down Expand Up @@ -93,6 +95,19 @@ func parseBillingAddress(address string) (*apppb.BillingAddress, error) {
}, nil
}

func parseTimeString(timeStr string) (*timestamppb.Timestamp, error) {
if timeStr == "" {
return nil, nil
}

t, err := time.Parse(time.RFC3339, timeStr)
if err != nil {
return nil, errors.Wrapf(err, "could not parse time string: %s", timeStr)
}

return timestamppb.New(t), nil
}

func formatStringForOutput(protoString, prefixToTrim string) string {
return strings.ToLower(strings.TrimPrefix(protoString, prefixToTrim))
}
Loading