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

Save CLI log output to file included in support bundle #4289

Merged
merged 1 commit into from
Dec 8, 2022

Conversation

cxbrowne1207
Copy link
Member

@cxbrowne1207 cxbrowne1207 commented Dec 5, 2022

Issue #, if available:

Description of changes:
This PR customizes the zap logger to log verbose logs to a file, and then include a copy of it in the support bundles when one is generated. This offers an easier way for the customer recover CLI run logs for debugging, when there are errors and interruptions of some kind.

Testing (if applicable):

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@eks-distro-bot
Copy link
Collaborator

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@eks-distro-bot eks-distro-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. do-not-merge/hold labels Dec 5, 2022
@cxbrowne1207 cxbrowne1207 marked this pull request as ready for review December 5, 2022 21:44
@codecov
Copy link

codecov bot commented Dec 5, 2022

Codecov Report

Merging #4289 (6bef564) into main (29a9135) will increase coverage by 0.09%.
The diff coverage is 90.24%.

@@            Coverage Diff             @@
##             main    #4289      +/-   ##
==========================================
+ Coverage   68.61%   68.70%   +0.09%     
==========================================
  Files         406      408       +2     
  Lines       33086    33310     +224     
==========================================
+ Hits        22701    22886     +185     
- Misses       8933     8969      +36     
- Partials     1452     1455       +3     
Impacted Files Coverage Δ
pkg/logger/zap.go 85.48% <85.18%> (ø)
pkg/diagnostics/collectors.go 81.51% <100.00%> (+0.59%) ⬆️
pkg/diagnostics/diagnostic_bundle.go 77.85% <100.00%> (+0.54%) ⬆️
pkg/logger/logger.go 30.00% <100.00%> (ø)
pkg/providers/nutanix/env.go 80.00% <0.00%> (-1.25%) ⬇️
pkg/networkutils/networkutils.go 67.56% <0.00%> (-0.86%) ⬇️
pkg/executables/govc.go 63.94% <0.00%> (-0.19%) ⬇️
internal/pkg/api/vsphere.go 0.00% <0.00%> (ø)
internal/pkg/api/vspheremachines.go 0.00% <0.00%> (ø)
pkg/api/v1alpha1/vspheremachineconfig_types.go 57.14% <0.00%> (ø)
... and 15 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@cxbrowne1207 cxbrowne1207 changed the title [WIP] Save CLI log output to file included in support bundle Save CLI log output to file included in support bundle Dec 6, 2022
@cxbrowne1207
Copy link
Member Author

\hold

@cxbrowne1207
Copy link
Member Author

\unhold

Copy link
Member

@g-gaston g-gaston left a comment

Choose a reason for hiding this comment

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

  • Is there a requirement to store the logs in memory? Or is that just an implementation detail.
  • Do we need this log dump always or should be just do it selectively where we need it (CLI commands for example)?
  • How does this solve the interrupted SSH connection? It seems like we are only reading the in memory logs when the support bundle report is triggered. And that only happens if the CLI execution is maintained uninterrupted and only after a CLI error happens. If the SSH connection is dropped and the process is interrupted, the support bundle won't be created and there won't be any logs. TBH, the solution to this problem should be to use tmux or screen, SSH connections are out of the CLI's domain of responsibility, so any SSH associated issue should be solved with SSH tools. But you described that situation in your problem statement and I don't think this code solves that usecase.

Wouldn't this be simpler if we use config.OutputPaths?

pkg/diagnostics/collectors.go Outdated Show resolved Hide resolved
pkg/logger/logcacher.go Outdated Show resolved Hide resolved
pkg/logger/logcacher.go Outdated Show resolved Hide resolved
pkg/logger/logcacher.go Outdated Show resolved Hide resolved
pkg/logger/logcacher.go Outdated Show resolved Hide resolved
pkg/logger/logcacher.go Outdated Show resolved Hide resolved
pkg/logger/logcacher.go Outdated Show resolved Hide resolved
pkg/logger/logcacher.go Outdated Show resolved Hide resolved
pkg/logger/logcacher.go Outdated Show resolved Hide resolved
pkg/logger/logger.go Outdated Show resolved Hide resolved
@danbudris
Copy link
Member

danbudris commented Dec 6, 2022

@g-gaston

  • How does this solve the interrupted SSH connection? It seems like we are only reading the in memory logs when the support bundle report is triggered. And that only happens if the CLI execution is maintained uninterrupted and only after a CLI error happens. If the SSH connection is dropped and the process is interrupted, the support bundle won't be created and there won't be any logs. TBH, the solution to this problem should be to use tmux or screen, SSH connections are out of the CLI's domain of responsibility, so any SSH associated issue should be solved with SSH tools. But you described that situation in your problem statement and I don't think this code solves that usecase.

Yeah from the conversation between @cxbrowne1207 and myself, the original impetus for this work was #1703; but the ticket over-states the importance of perserving the connection when SSH is used, and the real value is 'how do we get the CLI logs into the support bundle' rather than 'how do we maintain the session when closed'; I think that maybe we need to revisit the description, but the value of doing this as a way to get the CLI output into the support bundle stands IMO. Agree tho re: customer should be using tmux or w/e to maintain their sessions.

@cxbrowne1207
Copy link
Member Author

cxbrowne1207 commented Dec 6, 2022

  • Is there a requirement to store the logs in memory? Or is that just an implementation detail.
  • Do we need this log dump always or should be just do it selectively where we need it (CLI commands for example)?
  • How does this solve the interrupted SSH connection? It seems like we are only reading the in memory logs when the support bundle report is triggered. And that only happens if the CLI execution is maintained uninterrupted and only after a CLI error happens. If the SSH connection is dropped and the process is interrupted, the support bundle won't be created and there won't be any logs. TBH, the solution to this problem should be to use tmux or screen, SSH connections are out of the CLI's domain of responsibility, so any SSH associated issue should be solved with SSH tools. But you described that situation in your problem statement and I don't think this code solves that usecase.

Wouldn't this be simpler if we use config.OutputPaths?

It's only an implementation detail, the goal is to have the logs to be ultimately stored in the support bundle it seems. As mentioned @danbudris mentioned, the original ticket was used for as a basis for the description, but there's a mismatch with the goal of saving the logs to the support bundle as it doesn't solve the SSH connection issue. So, the PR description is a mistake. Will update.

I guess the question is now whether or not the solution is too complex and if the same thing can be done with config.OutputPaths. It sounds like it may also be better than saving it in memory to address the cases where there is some kind of interruption before the support bundle phase. I'll look into that :)

@cxbrowne1207
Copy link
Member Author

cxbrowne1207 commented Dec 6, 2022

@g-gaston @eks-distro-bot

Using OutputPath works well, but if we want to always log verbosely to the file like suggested on the ticket there are some caveats. We would have to customize the creation of the logger instead of using Zap.Config.Build in the InitZap method. So, our options are

  1. Settle for logging to the file with whatever level is specified by the customer.
  2. Customize the step to build a logger where zapcore.NewTee() is used to duplicate the logs, to the file and to the console.
  3. We may be able to instantiate, a secondary logger that only logs to a file (may require some customization beyond Zap.Config.Build as well but would have to log twice every-time (its also separate from the original.

@danbudris
Copy link
Member

  1. Customize the step to build a logger where zapcore.NewTee() is used to duplicate the logs, to the file and to the console.

This seems reasonable to me; from looking at the docs and some quick googling this seems like the recommended approach.

@g-gaston
Copy link
Member

g-gaston commented Dec 6, 2022

For the record, there was quorum since I voted option 2 offline as well :)

pkg/logger/config.go Outdated Show resolved Hide resolved
}

// Build constructs a logger and returns a logger.
func (cfg Config) Build() (*zap.Logger, error) {
Copy link
Member

Choose a reason for hiding this comment

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

exported?

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 was exported mainly so that it could be accessed it from the tests.

pkg/logger/config.go Outdated Show resolved Hide resolved
pkg/logger/config.go Outdated Show resolved Hide resolved
pkg/logger/config.go Outdated Show resolved Hide resolved
pkg/logger/zap.go Show resolved Hide resolved
pkg/logger/logger.go Outdated Show resolved Hide resolved
Comment on lines 19 to 21
l logr.Logger = logr.Discard()
once sync.Once
outputFilename string
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 think about grouping these 3 (or at least the logger and output file) in a struct
Can be a follow up

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed

pkg/diagnostics/collectors.go Outdated Show resolved Hide resolved
pkg/diagnostics/diagnostic_bundle.go Outdated Show resolved Hide resolved
pkg/logger/logger.go Show resolved Hide resolved
pkg/logger/zap.go Outdated Show resolved Hide resolved
pkg/logger/zap.go Outdated Show resolved Hide resolved
pkg/logger/zap.go Outdated Show resolved Hide resolved
pkg/logger/zap.go Outdated Show resolved Hide resolved
pkg/logger/zap.go Outdated Show resolved Hide resolved
pkg/logger/zap.go Outdated Show resolved Hide resolved
pkg/diagnostics/collectors.go Outdated Show resolved Hide resolved
@@ -32,7 +33,11 @@ func rootPersistentPreRun(cmd *cobra.Command, args []string) {
}

func initLogger() error {
if err := logger.InitZap(viper.GetInt("verbosity")); err != nil {
outputFilePath := fmt.Sprintf("./eksa-cli-%s.log", time.Now().Format("2006-01-02T15_04_05"))
Copy link
Member

Choose a reason for hiding this comment

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

should we look into deleting this file after a successful cli operation?

opt(&logr)

for _, name := range args.WithNames {
WithName(name)(&logr)
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
WithName(name)(&logr)
logr = logr.WithName(name)

I think it's a bit more readable? Unless I'm missing something

@cxbrowne1207
Copy link
Member Author

/approve

@eks-distro-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cxbrowne1207

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cxbrowne1207
Copy link
Member Author

/unhold

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants