Skip to content

Commit 6757c35

Browse files
committed
[core] only start collector/dogstatsd when needed
Before any call to `agent.py` or `dogstatsd.py` would actually create a `Agent` or `Dogstatsd` instance, without doing anaything with it (and diplaying a message about its pidfile). This commit changes this behaviour and only create them when needed.
1 parent 6cb37ec commit 6757c35

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

agent.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def _handle_sigusr1(self, signum, frame):
7777
self._handle_sigterm(signum, frame)
7878
self._do_restart()
7979

80-
def info(self, verbose=None):
80+
@classmethod
81+
def info(cls, verbose=None):
8182
logging.getLogger().setLevel(logging.ERROR)
8283
return CollectorStatus.print_latest_status(verbose=verbose)
8384

@@ -212,19 +213,24 @@ def main():
212213
autorestart = agentConfig.get('autorestart', False)
213214
hostname = get_hostname(agentConfig)
214215

215-
COMMANDS = [
216+
COMMANDS_AGENT = [
216217
'start',
217218
'stop',
218219
'restart',
219-
'foreground',
220220
'status',
221+
'foreground',
222+
]
223+
224+
COMMANDS_NO_AGENT = [
221225
'info',
222226
'check',
223227
'configcheck',
224228
'jmx',
225229
'flare',
226230
]
227231

232+
COMMANDS = COMMANDS_AGENT + COMMANDS_NO_AGENT
233+
228234
if len(args) < 1:
229235
sys.stderr.write("Usage: %s %s\n" % (sys.argv[0], "|".join(COMMANDS)))
230236
return 2
@@ -240,12 +246,13 @@ def main():
240246
from utils.deprecations import deprecate_old_command_line_tools
241247
deprecate_old_command_line_tools()
242248

243-
pid_file = PidFile('dd-agent')
249+
if command in COMMANDS_AGENT:
250+
pid_file = PidFile('dd-agent')
244251

245-
if options.clean:
246-
pid_file.clean()
252+
if options.clean:
253+
pid_file.clean()
247254

248-
agent = Agent(pid_file.get_path(), autorestart)
255+
agent = Agent(pid_file.get_path(), autorestart)
249256

250257
if command in START_COMMANDS:
251258
log.info('Agent version %s' % get_version())
@@ -266,7 +273,7 @@ def main():
266273
agent.status()
267274

268275
elif 'info' == command:
269-
return agent.info(verbose=options.verbose)
276+
return Agent.info(verbose=options.verbose)
270277

271278
elif 'foreground' == command:
272279
logging.info('Running in foreground')

daemon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ def run(self):
227227
"""
228228
raise NotImplementedError
229229

230-
231-
def info(self):
230+
@classmethod
231+
def info(cls):
232232
"""
233233
You should override this method when you subclass Daemon. It will be
234234
called to provide information about the status of the process

dogstatsd.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ def run(self):
352352
if self.autorestart:
353353
sys.exit(AgentSupervisor.RESTART_EXIT_STATUS)
354354

355+
@classmethod
355356
def info(self):
356357
logging.getLogger().setLevel(logging.ERROR)
357358
return DogstatsdStatus.print_latest_status()
@@ -424,16 +425,23 @@ def main(config_path=None):
424425
from utils.deprecations import deprecate_old_command_line_tools
425426
deprecate_old_command_line_tools()
426427

428+
COMMANDS_START_DOGSTATSD = [
429+
'start',
430+
'stop',
431+
'restart',
432+
'status'
433+
]
434+
427435
parser = optparse.OptionParser("%prog [start|stop|restart|status]")
428436
parser.add_option('-u', '--use-local-forwarder', action='store_true',
429437
dest="use_forwarder", default=False)
430438
opts, args = parser.parse_args()
431439

432-
reporter, server, cnf = init(config_path, use_watchdog=True,
433-
use_forwarder=opts.use_forwarder, args=args)
434-
pid_file = PidFile('dogstatsd')
435-
daemon = Dogstatsd(pid_file.get_path(), server, reporter,
436-
cnf.get('autorestart', False))
440+
if not args or args[0] in COMMANDS_START_DOGSTATSD:
441+
reporter, server, cnf = init(config_path, use_watchdog=True, use_forwarder=opts.use_forwarder, args=args)
442+
pid_file = PidFile('dogstatsd')
443+
daemon = Dogstatsd(pid_file.get_path(), server, reporter,
444+
cnf.get('autorestart', False))
437445

438446
# If no args were passed in, run the server in the foreground.
439447
if not args:
@@ -453,7 +461,7 @@ def main(config_path=None):
453461
elif command == 'status':
454462
daemon.status()
455463
elif command == 'info':
456-
return daemon.info()
464+
return Dogstatsd.info()
457465
else:
458466
sys.stderr.write("Unknown command: %s\n\n" % command)
459467
parser.print_help()

0 commit comments

Comments
 (0)