Skip to content

Commit

Permalink
Merge pull request #336 from DataDog/info_traceback_2
Browse files Browse the repository at this point in the history
Tracebacks on agent.py info and changed how get_config() receives options and args, fixes #329
  • Loading branch information
conorbranagan committed Jan 10, 2013
2 parents bed21c5 + 7dfbcc7 commit 94406d0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
14 changes: 8 additions & 6 deletions agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _handle_sigterm(self, signum, frame):
if self.collector:
self.collector.stop()

def run(self):
def run(self, config=None):
"""Main loop of the collector"""

# Gracefully exit on sigterm.
Expand All @@ -73,7 +73,10 @@ def run(self):
CollectorStatus().persist()

# Intialize the collector.
agentConfig = self._set_agent_config_hostname(get_config())
if not config:
config = get_config(parse_args=True)

agentConfig = self._set_agent_config_hostname(config)
systemStats = get_system_stats()
emitters = self._get_emitters(agentConfig)
self.collector = Collector(agentConfig, emitters, systemStats)
Expand Down Expand Up @@ -168,7 +171,7 @@ def setup_logging(agentConfig):

def main():
options, args = get_parsed_args()
agentConfig = get_config()
agentConfig = get_config(options=options)

# Logging
setup_logging(agentConfig)
Expand All @@ -188,7 +191,6 @@ def main():
return 2

command = args[0]

if command not in COMMANDS:
sys.stderr.write("Unknown command: %s\n" % command)
return 3
Expand Down Expand Up @@ -217,7 +219,7 @@ def main():

elif 'foreground' == command:
logging.info('Running in foreground')
agent.run()
agent.run(config=agentConfig)

# Commands that don't need the agent to be initialized.
else:
Expand All @@ -229,7 +231,7 @@ def main():
sys.stdout.write('dd-agent is not running.\n')

elif 'info' == command:
return CollectorStatus.print_latest_status()
return CollectorStatus.print_latest_status(verbose=options.verbose)

return 0

Expand Down
4 changes: 3 additions & 1 deletion checks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import time
import types
import os
import sys

from util import LaconicFilter
from checks import check_status
Expand Down Expand Up @@ -417,7 +418,8 @@ def run(self):
instance_status = check_status.InstanceStatus(i, check_status.STATUS_OK)
except Exception, e:
self.log.exception("Check '%s' instance #%s failed" % (self.name, i))
instance_status = check_status.InstanceStatus(i, check_status.STATUS_ERROR, e)
# Send the traceback (located at sys.exc_info()[2]) into the InstanceStatus otherwise a traceback won't be able to be printed
instance_status = check_status.InstanceStatus(i, check_status.STATUS_ERROR, e, sys.exc_info()[2])
instance_statuses.append(instance_status)
return instance_statuses

Expand Down
24 changes: 22 additions & 2 deletions checks/check_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import platform
import sys
import tempfile
import traceback

# project
import config
Expand Down Expand Up @@ -162,7 +163,8 @@ def load_latest_status(cls):
return None

@classmethod
def print_latest_status(cls):
def print_latest_status(cls, verbose=False):
cls.verbose = verbose
Stylizer.ENABLED = False
try:
if sys.stdout.isatty():
Expand Down Expand Up @@ -190,11 +192,16 @@ def _get_pickle_path(cls):

class InstanceStatus(object):

def __init__(self, instance_id, status, error=None):
def __init__(self, instance_id, status, error=None, tb=None):
self.instance_id = instance_id
self.status = status
self.error = repr(error)

if (type(tb).__name__ == 'traceback'):
self.traceback = traceback.format_tb(tb)
else:
self.traceback = None

def has_error(self):
return self.status != STATUS_OK

Expand Down Expand Up @@ -289,6 +296,19 @@ def body_lines(self):
" - Collected %s metrics & %s events" % (cs.metric_count, cs.event_count),
""
]

if self.verbose and s.traceback is not None:
# Formatting the traceback to look like a python traceback
check_lines.append(" Traceback (most recent call last):")

# Format the traceback lines to look good in the output
for tb_line in s.traceback:
lines = tb_line.split('\n')
for line in lines:
if line.strip() == '':
continue
check_lines.append(' ' + line)

lines += check_lines

# Emitter status
Expand Down
6 changes: 3 additions & 3 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def get_parsed_args():
default=False,dest='use_forwarder')
parser.add_option('-n', '--disable-dd', action='store_true', default=False,
dest="disable_dd")
parser.add_option('-v', '--verbose', action='store_true', default=False,
dest='verbose', help='Print out available tracebacks for errors in checks')
try:
options, args = parser.parse_args()
except SystemExit:
Expand Down Expand Up @@ -141,11 +143,9 @@ def get_config_path(cfg_path=None, os_name=None):
sys.stderr.write("Please supply a configuration file at %s or in the directory where the agent is currently deployed.\n" % str(exc))
sys.exit(3)

def get_config(parse_args = True, cfg_path=None, init_logging=False, options=None):
def get_config(parse_args=False, cfg_path=None, init_logging=False, options=None):
if parse_args:
options, args = get_parsed_args()
elif not options:
args = None

# General config
agentConfig = {
Expand Down

0 comments on commit 94406d0

Please sign in to comment.