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

State export log ignores some log files #3382

Merged
merged 17 commits into from
Jul 29, 2024
Merged

State export log ignores some log files #3382

merged 17 commits into from
Jul 29, 2024

Conversation

MDrakos
Copy link
Member

@MDrakos MDrakos commented Jun 28, 2024

StoryDX-2892 `state export log -i` should not consider logs for `state export log`

One thing to note about this PR is that moving the logging of arguments to the captain package means that they won't be at the top of the log file anymore. I still think it makes sense to log the arguments in that package. The added requirement of only logging known commands and flags means that we have to log after command registration so that we can inspect the arguments and omit things we don't recognize.

@github-actions github-actions bot changed the base branch from master to version/0-46-0-RC1 June 28, 2024 22:31
@MDrakos MDrakos marked this pull request as ready for review July 9, 2024 21:50
@MDrakos MDrakos requested a review from Naatan July 9, 2024 21:50
@@ -880,3 +918,30 @@ func childCommands(cmd *Command) string {

return fmt.Sprintf("\n\nAvailable Commands:\n%s", table.Render())
}

func (c *Command) LogArgs() {
children, err := c.FindChildren(os.Args[1:])
Copy link
Member

Choose a reason for hiding this comment

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

We should use c.Args. I think for those it already dropped the first entry of os.Args, but worth verifying.

Copy link
Member Author

Choose a reason for hiding this comment

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

All of the arg values in the command appear to be the known command arguments and not the os.Args. We set the args as part of the cobra command but I'm not finding a way to get them back out.

@@ -880,3 +918,30 @@ func childCommands(cmd *Command) string {

return fmt.Sprintf("\n\nAvailable Commands:\n%s", table.Render())
}

func (c *Command) LogArgs() {
children, err := c.FindChildren(os.Args[1:])
Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty sure FindChild() already achieves the desired effect you're looking for. And it passes through cobra, guaranteeing that we get the right command. From there I think you could use command.cobra.CommandPath(), which should give you the full path.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right and it also turns out we have other ways to accomplish this already so I've deleted the FindChildren function.

Comment on lines 73 to 74
logging.Error("failed to validate log file: %s", err.Error())
continue
Copy link
Member

Choose a reason for hiding this comment

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

Bubble up errors please. This shouldn't ever cause an error, so if it does something is broken and we need to fix it. Logging just obfuscates the breakage.

}
defer file.Close()

regex := regexp.MustCompile(`.*\] Args: \[(.*?)\], Flags: \[.*?\]`)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
regex := regexp.MustCompile(`.*\] Args: \[(.*?)\], Flags: \[.*?\]`)
regex := regexp.MustCompile(`Args: \[(.*?)\], Flags: \[.*?\]`)

Doesn't seem necessary for this function to have an opinion on what the prefix looks like.

}
}

if strings.Contains(args, "export") && strings.Contains(args, "log") {
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer we be a but more explicit with the matches. However unlikely, it's easy enough to guard against something like state exec -- myapp -i file.log --export or something along those lines.

Copy link
Member Author

Choose a reason for hiding this comment

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

I made the check sequential. Is there more that you're looking for?

@MDrakos MDrakos requested a review from Naatan July 17, 2024 18:13
@@ -920,18 +885,15 @@ func childCommands(cmd *Command) string {
}

func (c *Command) LogArgs() {
children, err := c.FindChildren(os.Args[1:])
child, err := c.FindChild(os.Args[1:])
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

For some reason all of my comments were Pending 🤦

internal/runners/export/log.go Outdated Show resolved Hide resolved
Comment on lines 939 to 947
func flags() []string {
flags := []string{}
for _, arg := range os.Args {
if strings.HasPrefix(arg, "-") {
flags = append(flags, arg)
}
}
return flags
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This function is largely duplicated from the analytics package. I tried moving things around but ended up with import cycles so opted to put this here.

}
}

if strings.Contains(args, "export") && strings.Contains(args, "log") {
Copy link
Member Author

Choose a reason for hiding this comment

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

I made the check sequential. Is there more that you're looking for?

@@ -880,3 +918,30 @@ func childCommands(cmd *Command) string {

return fmt.Sprintf("\n\nAvailable Commands:\n%s", table.Render())
}

func (c *Command) LogArgs() {
children, err := c.FindChildren(os.Args[1:])
Copy link
Member Author

Choose a reason for hiding this comment

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

All of the arg values in the command appear to be the known command arguments and not the os.Args. We set the args as part of the cobra command but I'm not finding a way to get them back out.

@@ -880,3 +918,30 @@ func childCommands(cmd *Command) string {

return fmt.Sprintf("\n\nAvailable Commands:\n%s", table.Render())
}

func (c *Command) LogArgs() {
children, err := c.FindChildren(os.Args[1:])
Copy link
Member Author

Choose a reason for hiding this comment

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

You're right and it also turns out we have other ways to accomplish this already so I've deleted the FindChildren function.

MDrakos and others added 2 commits July 17, 2024 15:00
@Naatan
Copy link
Member

Naatan commented Jul 17, 2024

@MDrakos can't respond to actual comment cause it's "old" and github UX is awful.

Suggest instead passing os.Args to the LogArgs function, that way captain isn't violating its own responsibilities by reaching out to os.Args.

That said, perhaps it would be better to just log args internally when the command is executed and the args are passed, ie. here:

func (c *Command) Execute(args []string) error {

@MDrakos MDrakos requested a review from Naatan July 17, 2024 22:41
Copy link
Member

@Naatan Naatan left a comment

Choose a reason for hiding this comment

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

Looks good! I think we just need a simple integration test covering this mechanic though, come to think of it. As this feels a little fragile considering how the logic is somewhat implicitly shared between different packages.

@MDrakos MDrakos requested a review from Naatan July 29, 2024 17:47
Copy link
Member

@Naatan Naatan left a comment

Choose a reason for hiding this comment

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

Nice work!

@MDrakos MDrakos merged commit dd0c713 into version/0-46-0-RC1 Jul 29, 2024
7 checks passed
@MDrakos MDrakos deleted the DX-2892 branch July 29, 2024 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants