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

Add diagnostics collect command to elastic-agent. #28461

Merged
merged 6 commits into from Oct 19, 2021

Conversation

michel-laterman
Copy link
Contributor

@michel-laterman michel-laterman commented Oct 15, 2021

What does this PR do?

Add diagnostics collect command to elastic-agent. This option will
create an archive with information that can help with debugging the
elastic-agent. The information includes process metadata (same as the
output of elastic-agent diagnostics), config information, the rendered
policy, and any local log files.

Why is it important?

This command can be used by support to help when debugging issues with the elastic-agent.

Note that it currently DOES NOT redact any credentials, so if it used by a customer, they will need to manually edit the files in the archive to redact credentials.

Contents (config, policies, and metadata) may be written in json or yaml (specified by a flag, default yaml). The archive will have the following structure:

meta/
    elastic-agent-version.yaml
    <process-name>-<routing_key>.yaml
config/
    elastic-agent-local.yaml
    elastic-agent-policy.yaml
logs/
    <all local log files>

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation PR here
  • [] I have made corresponding change to the default configuration files
  • [] I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

How to test this PR locally

Run the agent normally, then run elastic-agent diagnostics collect.

Related issues

Add diagnostics collect command to elastic-agent. This option will
create an archieve with information that can help with debugging the
elastic-agent. The information includes process metadata (same as the
output of elastic-agent diagnostics), config information, the rendered
policy, and any local log files.
@michel-laterman michel-laterman added enhancement backport-v7.16.0 Automated backport with mergify Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team labels Oct 15, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane)

@botelastic botelastic bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels Oct 15, 2021
return err
}

// using Data() + "/logs", for some reason default paths/Logs() is the home dir...
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm confused as to why path.Logs() returned <path> instead of <path>/data/logs (as I expected), anyone have insight into this?

Copy link
Contributor

Choose a reason for hiding this comment

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

logPath defaults to topPath which is what you see if no override is provided x-pack/elastic-agent/pkg/agent/application/paths/common.go:L40

if fErr != nil {
return fmt.Errorf("unable to walk log dir: %w", fErr)
}
name := strings.TrimPrefix(path, logPath)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

name in this case gives the name of the directory/file within log/ to write into the archive. For exampledefault/filebeat-json.log

}
zw := zip.NewWriter(f)

zf, err := zw.Create("meta/")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the docs for zip indicate that ending a name with a slash creates a directory.

@elasticmachine
Copy link
Collaborator

elasticmachine commented Oct 15, 2021

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Duration: 98 min 49 sec

❕ Flaky test report

No test was executed to be analysed.

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

@@ -147,4 +147,5 @@
- Support ephemeral containers in Kubernetes dynamic provider. {issue}27020[#27020] {pull}27707[27707]
- Add complete k8s metadata through composable provider. {pull}27691[27691]
- Add diagnostics command to gather beat metadata. {pull}28265[28265]
- Add diagnostics collect command to gather beat metadata, config, policy, and logs and bundle it into an archieve. {pull}28461[28461]
Copy link
Contributor

Choose a reason for hiding this comment

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

s/archieve/archive

Copy link
Contributor

@michalpristas michalpristas 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, please take a look at these small issues i raised and test on windows to check processing of path separator

return closeHandlers(err, zw, f)
}

zf, err = zw.Create("meta/elastic-agent-version." + outputFormat)
Copy link
Contributor

Choose a reason for hiding this comment

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

tested on windows as well? i'm wondering because of / vs \ as a path separator.
same thing all over this file

// using Data() + "/logs", for some reason default paths/Logs() is the home dir...
logPath := filepath.Join(paths.Data(), "logs") + string(filepath.Separator)
return filepath.WalkDir(logPath, func(path string, d fs.DirEntry, fErr error) error {
if fErr != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

let's hande IsNotExist(err) and continue without processing if so. i've had issues with Walk before and this is not serious but can break the process

return nil
}

lf, err := os.Open(path)
Copy link
Contributor

Choose a reason for hiding this comment

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

not closed, better to extract to a func and use defer to close the file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, just added closeHandlers to my exit points


// writeFile writes json or yaml data from the interface to the writer.
func writeFile(w io.Writer, outputFormat string, v interface{}) error {
if outputFormat == "yaml" {
Copy link
Contributor

Choose a reason for hiding this comment

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

not serious at all but we default to yaml when using a command, we could also use json here if specified and default to yaml as well

fix typos, handle file not exist errors, close open file handles for
logs after copying, writeFile will default to yaml instead of json
behaviour.
@michel-laterman
Copy link
Contributor Author

I've tested on windows now and found and fixed an issue with the archive file name (Windows does not allow : in file names).

Currently the command runs and creates an archive, however it's not able to collect logs (on Windows).
The log path I create from paths.Data() + "logs" (default c:\program files\elastic\agent\data\logs) does not exist on windows. The logs are in a sub-directory of data: c:\program files\elastic\agent\data\elastic-agent-<hash>\logs.
I can't find a way to find this path with the local config settings.logging.files.path or path.Logs().
@michalpristas, @blakerouse any suggestions?

@michalpristas
Copy link
Contributor

Home should point to versioned home directory

@michel-laterman
Copy link
Contributor Author

/test

@michel-laterman
Copy link
Contributor Author

/package

@michel-laterman michel-laterman merged commit 99ebf3e into elastic:master Oct 19, 2021
@michel-laterman michel-laterman deleted the diagnostics-collect branch October 19, 2021 15:31
mergify bot pushed a commit that referenced this pull request Oct 19, 2021
Add diagnostics collect command to elastic-agent.

Add diagnostics collect command to elastic-agent. This option will
create an archieve with information that can help with debugging the
elastic-agent. The information includes process metadata (same as the
output of elastic-agent diagnostics), config information, the rendered
policy, and any local log files.

(cherry picked from commit 99ebf3e)
michel-laterman added a commit that referenced this pull request Oct 19, 2021
Add diagnostics collect command to elastic-agent.

Add diagnostics collect command to elastic-agent. This option will
create an archieve with information that can help with debugging the
elastic-agent. The information includes process metadata (same as the
output of elastic-agent diagnostics), config information, the rendered
policy, and any local log files.

(cherry picked from commit 99ebf3e)

Co-authored-by: Michel Laterman <82832767+michel-laterman@users.noreply.github.com>
Icedroid pushed a commit to Icedroid/beats that referenced this pull request Nov 1, 2021
Add diagnostics collect command to elastic-agent.

Add diagnostics collect command to elastic-agent. This option will
create an archieve with information that can help with debugging the
elastic-agent. The information includes process metadata (same as the
output of elastic-agent diagnostics), config information, the rendered
policy, and any local log files.
mergify bot pushed a commit that referenced this pull request Nov 10, 2021
Add diagnostics collect command to elastic-agent.

Add diagnostics collect command to elastic-agent. This option will
create an archieve with information that can help with debugging the
elastic-agent. The information includes process metadata (same as the
output of elastic-agent diagnostics), config information, the rendered
policy, and any local log files.

(cherry picked from commit 99ebf3e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-v7.16.0 Automated backport with mergify enhancement Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants