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 log collector utility #1847

Merged
merged 23 commits into from
May 29, 2020
Merged

Add log collector utility #1847

merged 23 commits into from
May 29, 2020

Conversation

pgombar
Copy link
Contributor

@pgombar pgombar commented Apr 7, 2020

Description

First step of the feature to enable WALinuxAgent to periodically upload relevant log files to the host.

This PR adds the module that contains the functionality to collect logs based on a given manifest file. The format and contents of the manifest files is taken from Azure Inspect Disk Service.

The log collector module supports prioritization of important files in case there is not enough space for all files to be collected. Singe large files are not dropped, but truncated, where the freshest entries are kept.

During collection, the files are kept in a tarball that is incrementally updated on each collection. This means that in subsequent calls, only new files are added to the tarball, deleted files are removed, and modified files are updated, the rest stays the same. Finally, when all the files are collected, the tarball is compressed into a zip before it is sent to the host.

Future work entails adding a thread that will periodically call this utiilty (with limits on resource consumption) and send the zip to the host.


PR information

  • The title of the PR is clear and informative.
  • There are a small number of commits, each of which has an informative message. This means that previously merged commits do not appear in the history of the PR. For information on cleaning up the commits in your pull request, see this page.
  • Except for special cases involving multiple contributors, the PR is started from a fork of the main repository, not a branch.
  • If applicable, the PR references the bug/issue that it fixes in the description.
  • New Unit tests were added for the changes made and Travis.CI is passing.

Quality of Code and Contribution Guidelines


This change is Reviewable

@codecov
Copy link

codecov bot commented Apr 7, 2020

Codecov Report

Merging #1847 into develop will increase coverage by 0.34%.
The diff coverage is 87.61%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1847      +/-   ##
===========================================
+ Coverage    69.24%   69.58%   +0.34%     
===========================================
  Files           83       84       +1     
  Lines        11483    11700     +217     
  Branches      1597     1626      +29     
===========================================
+ Hits          7951     8142     +191     
- Misses        3193     3213      +20     
- Partials       339      345       +6     
Impacted Files Coverage Δ
azurelinuxagent/agent.py 44.65% <ø> (ø)
azurelinuxagent/common/logcollector.py 87.44% <87.44%> (ø)
azurelinuxagent/common/conf.py 77.77% <100.00%> (+0.73%) ⬆️
azurelinuxagent/pa/deprovision/default.py 67.85% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5c2d67d...48cd548. Read the comment docs.

azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
Copy link
Member

@narrieta narrieta left a comment

Choose a reason for hiding this comment

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

a few comments now while i continue my review

azurelinuxagent/common/utils/fileutil.py Outdated Show resolved Hide resolved
azurelinuxagent/common/utils/shellutil.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/utils/shellutil.py Outdated Show resolved Hide resolved
tests/tools.py Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
Copy link
Contributor

@larohra larohra left a comment

Choose a reason for hiding this comment

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

A few comments on code, taking a look at the tests now

azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
config/logcollector_manifest_normal Show resolved Hide resolved
Copy link
Member

@narrieta narrieta left a comment

Choose a reason for hiding this comment

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

few more comments

azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
:return: Returns True if the log collection succeeded
"""
try:
# Clear previous run's output and create base directories if they don't exist already
Copy link
Member

Choose a reason for hiding this comment

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

you should clean up as soon as you're done creating the zip instead of cleaning on the next run

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 idea was to leave the previous run's output for debugging purposes. For example, in a situation where the tool is invoked and the archive couldn't be created, not even the results file will be added to the archive. In that case, there is value in leaving that file on disk instead of deleting it.

azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/utils/shellutil.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
tests/tools.py Show resolved Hide resolved
tests/common/test_logcollector.py Show resolved Hide resolved
Copy link
Contributor

@larohra larohra left a comment

Choose a reason for hiding this comment

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

Some minor comments

azurelinuxagent/common/logcollector.py Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
def format_command(cmd):
return " ".join(cmd) if isinstance(cmd, list) else command

def _encode_command_output(output):
Copy link
Member

Choose a reason for hiding this comment

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

why is this encoding needed?

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 maintain utf-8 encoding throughout the module (when reading files and when writing to them), so I wanted to preserve this behaviour for outputs out of my control, e.g. outputs from shell commands or stack traces. Seems safe to keep it?

Copy link
Member

Choose a reason for hiding this comment

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

But you are creating a utf8-encoded log file already. Why would this extra encoding would be needed?

Copy link
Contributor

@larohra larohra left a comment

Choose a reason for hiding this comment

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

Left some minor comments

azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
Copy link
Contributor

@larohra larohra left a comment

Choose a reason for hiding this comment

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

Left some comments

azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
azurelinuxagent/common/logcollector.py Outdated Show resolved Hide resolved
Copy link
Member

@narrieta narrieta left a comment

Choose a reason for hiding this comment

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

Thanks for the updates!

@@ -243,6 +243,10 @@ def get_ext_log_dir(conf=__conf__):
return conf.get("Extension.LogDir", "/var/log/azure")


def get_agent_log_file():
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a bit concerned about putting this here since its not part of the conf (Maybe osutil would be a better place since the other PR eventually plans to move it there).

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 still have to revisit the logic in the other PR, and if the ultimate goal is to expose the agent log parameter in the config file, this would be the correct place. Of course, this can change, so for now this I settled for this solution.

Copy link
Contributor

@larohra larohra left a comment

Choose a reason for hiding this comment

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

A bit concerned about the placement of the function get_agent_log_file() but its not a pressing concern for me as such, I'll approve it for now.

Thanks for the changes :)

@pgombar pgombar merged commit c143bef into Azure:develop May 29, 2020
@pgombar pgombar mentioned this pull request Aug 20, 2020
6 tasks
@pgombar pgombar deleted the log branch October 6, 2020 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants