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

[flare] Add JMXFetch-specific info #1726

Merged
merged 10 commits into from
Jul 2, 2015
Prev Previous commit
Next Next commit
[flare] Redirect root logger's output to tar
The user should not see any output from the called commands.

In `check_status.py`, stop assuming that all streams have a `name` attribute
(for instance `StringIO.StringIO` instances do not have one).
olivielpeau committed Jul 1, 2015
commit 7d79a5671d65af12d31ff1f70df14a8bee807c4f
5 changes: 4 additions & 1 deletion checks/check_status.py
Original file line number Diff line number Diff line change
@@ -88,7 +88,10 @@ def logger_info():
if len(root_logger.handlers) > 0:
for handler in root_logger.handlers:
if isinstance(handler, logging.StreamHandler):
loggers.append(handler.stream.name)
try:
loggers.append(handler.stream.name)
except AttributeError:
loggers.append("unnamed stream")
if isinstance(handler, logging.handlers.SysLogHandler):
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why we need to do this 😖

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 documented it in the message of 378fdae:

In `check_status.py`, stop assuming that all streams have a `name` attribute
(for instance `StringIO.StringIO` instances do not have one).

Do you think it deserves a comment directly in the source code?

if isinstance(handler.address, basestring):
loggers.append('syslog:%s' % handler.address)
21 changes: 5 additions & 16 deletions utils/flare.py
Original file line number Diff line number Diff line change
@@ -228,8 +228,7 @@ def _add_jmxinfo_tar(self):
log.info(" * datadog-agent jmx {0} output".format(command))
self._add_command_output_tar(
os.path.join('jmxinfo', '{0}.log'.format(command)),
partial(self._jmx_command_call, command),
'jmxfetch'
partial(self._jmx_command_call, command)
)

# java version
@@ -238,8 +237,7 @@ def _add_jmxinfo_tar(self):
os.path.join('jmxinfo', 'java_version.log'),
# We use lambda so that JMXFetch.get_configuration is evaluated in _add_command_output_tar,
# which captures the logging output from JMXFetch
lambda: self._java_version(JMXFetch.get_configuration(get_confd_path())[2]),
'jmxfetch'
lambda: self._java_version(JMXFetch.get_configuration(get_confd_path())[2])
)

# Check if the file is readable (and log it)
@@ -295,23 +293,14 @@ def _strip_password(self, file_path):
return temp_path, password_found

# Add output of the command to the tarfile
def _add_command_output_tar(self, name, command, logger_name=None):
def _add_command_output_tar(self, name, command):
backup_out, backup_err = sys.stdout, sys.stderr
backup_handlers = logging.root.handlers[:]
out, err = StringIO.StringIO(), StringIO.StringIO()
if logger_name:
# If specified, redirect logger output to `out`
logger = logging.getLogger(logger_name)
backup_logger_handlers = logger.handlers[:]
logger.handlers = [logging.StreamHandler(out)]
backup_logger_propagate = logger.propagate
logger.propagate = False
backup_handlers = logging.root.handlers[:]
logging.root.handlers = [logging.StreamHandler(out)]
sys.stdout, sys.stderr = out, err
command()
sys.stdout, sys.stderr = backup_out, backup_err
if logger_name:
logger.handlers = backup_logger_handlers
logger.propagate = backup_logger_propagate
logging.root.handlers = backup_handlers
_, temp_path = tempfile.mkstemp(prefix='dd')
with open(temp_path, 'w') as temp_file: