Skip to content

Commit

Permalink
[core] add a switch to disable profiling, but still use developer mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cberry777 committed Oct 7, 2016
1 parent a33aa3f commit cca3dd0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
4 changes: 3 additions & 1 deletion agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,15 @@ def run(self, config=None):
self.restart_interval = int(self._agentConfig.get('restart_interval', RESTART_INTERVAL))
self.agent_start = time.time()

self.allow_profiling = self._agentConfig.get('allow_profiling', True)

profiled = False
collector_profiled_runs = 0

# Run the main loop.
while self.run_forever:
# Setup profiling if necessary
if self.in_developer_mode and not profiled:
if self.allow_profiling and self.in_developer_mode and not profiled:
try:
profiler = AgentProfiler()
profiler.enable_profiling()
Expand Down
17 changes: 12 additions & 5 deletions checks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,11 @@ def __init__(self, name, init_config, agentConfig, instances=None):
self.name = name
self.init_config = init_config or {}
self.agentConfig = agentConfig

self.in_developer_mode = agentConfig.get('developer_mode') and psutil
self._internal_profiling_stats = None
self.allow_profiling = self.agentConfig.get('allow_profiling', True)

self.default_integration_http_timeout = float(agentConfig.get('default_integration_http_timeout', 9))

self.hostname = agentConfig.get('checksd_hostname') or get_hostname(agentConfig)
Expand Down Expand Up @@ -732,13 +735,16 @@ def _collect_internal_stats(methods=None):
return stats

def _set_internal_profiling_stats(self, before, after):
self._internal_profiling_stats = {'before': before, 'after': after}
if self.allow_profiling:
self._internal_profiling_stats = {'before': before, 'after': after}

def _get_internal_profiling_stats(self):
"""
If in developer mode, return a dictionary of statistics about the check run
"""
stats = self._internal_profiling_stats
stats = None
if self.allow_profiling:
stats = self._internal_profiling_stats
self._internal_profiling_stats = None
return stats

Expand Down Expand Up @@ -798,10 +804,11 @@ def run(self):
if self.in_developer_mode and self.name != AGENT_METRICS_CHECK_NAME:
try:
after = AgentCheck._collect_internal_stats()
self._set_internal_profiling_stats(before, after)
log.info("\n \t %s %s" % (self.name, pretty_statistics(self._internal_profiling_stats)))
if self.allow_profiling:
self._set_internal_profiling_stats(before, after)
log.info("\n \t %s %s" % (self.name, pretty_statistics(self._internal_profiling_stats)))
except Exception: # It's fine if we can't collect stats for the run, just log and proceed
self.log.debug("Failed to collect Agent Stats after check {0}".format(self.name))
self.log.exception("Failed to collect Agent Stats after check {0}".format(self.name))

return instance_statuses

Expand Down
35 changes: 35 additions & 0 deletions tests/checks/mock/test_no_profiling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import mock

from tests.checks.common import AgentCheckTest, load_check

class TestNoProfiling(AgentCheckTest):

CHECK_NAME = 'redisdb'

def test_no_profiling(self):
agentConfig = {
'api_key': 'XXXtest_apikey',
'developer_mode': True,
'allow_profiling': False
}
# this must be SystemExit, because otherwise the Exception is eaten
mocks = {
'_set_internal_profiling_stats': mock.MagicMock(side_effect=SystemExit),
}
redis_config = {
"init_config": {},
"instances": [{"host": "localhost", "port": 6379}]
}
check = load_check('redisdb', redis_config, agentConfig)

self.assertFalse(check.allow_profiling)
self.assertTrue(check.in_developer_mode)

for func_name, mock1 in mocks.iteritems():
if not hasattr(check, func_name):
continue
else:
setattr(check, func_name, mock1)

check.run()
# If we get here, no Exception was thrown
Binary file removed tests/core/fixtures/flare/datadog-agent-1.tar.bz2
Binary file not shown.

0 comments on commit cca3dd0

Please sign in to comment.