Skip to content

Commit 4debc61

Browse files
committed
run: allow profiling to be explicitly started
It used to be profiling was only started when StrongOps was started. This caused problems for metrics and other users of the agent, because when agent is no started first, it doesn't behave well. In particular, it doesn't monkey patch anything, causing metrics to be lacking probe metrics, and agent traces and express records not be available.
1 parent 7d7acc4 commit 4debc61

File tree

4 files changed

+53
-21
lines changed

4 files changed

+53
-21
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ signalled with SIGHUP, see
176176
### slc run
177177

178178
``` text
179-
usage: slc run [options] [app [app-options...]]
180179
usage: slr [options] [app [app-options...]]
181180
182181
Run an app, allowing it to be profiled (using StrongOps) and supervised.
@@ -203,14 +202,15 @@ Options:
203202
Disable timestamping of supervisor log messages.
204203
--syslog Send supervisor and collected worker logs to syslog,
205204
unsupported on Windows.
206-
--metrics BACKEND Send metrics to custom backend (default is no custom).
205+
--metrics BACKEND Report metrics to custom backend. Implies `--profile`.
207206
-p,--pid FILE Write supervisor's pid to FILE, failing if FILE already
208207
has a valid pid in it (default is no pid file).
209208
--cluster N Set the cluster size (default is off, but see below).
210-
--no-profile Disable reporting profile data to StrongOps (default is to
211-
profile if registration data is found). Does not affect
212-
local reporting using --metrics option.
213-
-C,--control CTL Listen for local control messages on CTL (default `pmctl),
209+
--profile Start the agent. Report to StrongOps if registration data
210+
is found (this is the default).
211+
--no-profile Do not start the agent, do not report to StrongOps,
212+
do not report metrics.
213+
-C,--control CTL Listen for local control messages on CTL (default `pmctl`),
214214
only supported when clustered.
215215
--no-control Do not listen for local control messages.
216216

bin/sl-run.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,42 @@ process.on('disconnect', function() {
1111
var config = require('../lib/config'); // May exit, depending on argv
1212
var log = config.logger;
1313

14-
if(!config.profile) {
15-
if(config.isMaster) {
16-
log.error('supervisor running without StrongOps (unprofiled)');
17-
}
18-
} else {
19-
require('strong-agent').profile(undefined, undefined, {
20-
quiet: config.isWorker, // Quiet in worker, to avoid repeated log messages
21-
logger: config.logger,
22-
});
14+
var agent = require('../lib/agent');
15+
var agentOptions = {
16+
quiet: config.isWorker, // Quiet in worker, to avoid repeated log messages
17+
logger: config.logger,
18+
};
19+
20+
switch(config.profile) {
21+
case false: // Profiling explicitly disabled.
22+
if(config.isMaster) {
23+
log.error('supervisor running without profiling');
24+
}
25+
break;
26+
27+
case undefined: // No explicit request for profiling or metrics.
28+
// Start with StrongOps if app is registered. This will print warning
29+
// messages to the console if the api key is not found, which is backwards
30+
// compatible.
31+
agent().profile(undefined, undefined, agentOptions);
32+
// Otherwise, just start. This is a no-op if it is already started.
33+
agent().start();
34+
break;
35+
36+
case true: // Profiling or metrics explicitly enabled.
37+
agent().configure(agentOptions);
38+
// Only try to start StrongOps if they have registered, to avoid legacy
39+
// warning messages. If an app is missing a name, profile may still fail
40+
// to start, so drop-through to start(). We must re-supply options.
41+
if (agent().config.key)
42+
agent().profile(undefined, undefined, agentOptions);
43+
// Otherwise, just start. This is a no-op if already started.
44+
agent().start();
45+
break;
46+
47+
default:
48+
assert(false, 'invalid profile value');
49+
break;
2350
}
2451

2552
if((config.clustered && config.isMaster) || config.detach){

bin/sl-run.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ Options:
2424
Disable timestamping of supervisor log messages.
2525
--syslog Send supervisor and collected worker logs to syslog,
2626
unsupported on Windows.
27-
--metrics BACKEND Send metrics to custom backend (default is no custom).
27+
--metrics BACKEND Report metrics to custom backend. Implies `--profile`.
2828
-p,--pid FILE Write supervisor's pid to FILE, failing if FILE already
2929
has a valid pid in it (default is no pid file).
3030
--cluster N Set the cluster size (default is off, but see below).
31-
--no-profile Disable reporting profile data to StrongOps (default is to
32-
profile if registration data is found). Does not affect
33-
local reporting using --metrics option.
34-
-C,--control CTL Listen for local control messages on CTL (default `pmctl),
31+
--profile Start the agent. Report to StrongOps if registration data
32+
is found (this is the default).
33+
--no-profile Do not start the agent, do not report to StrongOps,
34+
do not report metrics.
35+
-C,--control CTL Listen for local control messages on CTL (default `pmctl`),
3536
only supported when clustered.
3637
--no-control Do not listen for local control messages.
3738

lib/options.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exports.parse = function parse(argv) {
2424
HELP: exports.HELP,
2525
argv: argv, // process.argv stripped of command...
2626
args: [ '.' ], // app app-options...
27-
profile: true,
27+
profile: process.env.STRONGLOOP_METRICS ? true : undefined,
2828
channel: 'runctl',
2929
log: false,
3030
metrics: null,
@@ -75,10 +75,12 @@ exports.parse = function parse(argv) {
7575
i++;
7676
options.metrics = options.metrics || []
7777
options.metrics.push(argv[i]);
78+
options.profile = true;
7879
}
7980
else if(/^--metrics=/.test(option)) {
8081
options.metrics = options.metrics || []
8182
options.metrics.push(value(option));
83+
options.profile = true;
8284
}
8385
else if(option === '--pid' || option === '-p') {
8486
i++;
@@ -92,6 +94,8 @@ exports.parse = function parse(argv) {
9294
}
9395
else if(option === '--no-profile') {
9496
options.profile = false;
97+
options.metrics = null;
98+
delete process.env.STRONGLOOP_METRICS;
9599
}
96100
else if(option === '--cluster') {
97101
i++;

0 commit comments

Comments
 (0)