Skip to content

Commit d3fb052

Browse files
committed
report method for manual reporting
1 parent dd2a909 commit d3fb052

11 files changed

+84
-56
lines changed

Gruntfile.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ module.exports = function(grunt) {
99
node: true,
1010
esversion: 6,
1111
globalstrict: true,
12-
'-W069': true
12+
'-W069': true,
13+
'-W082': true
1314
}
1415
}
1516
});

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ All initialization options:
7474
* `appVersion` (Optional) Sets application version, which can be used to associate profiling information with the source code release.
7575
* `appEnvironment` (Optional) Used to differentiate applications in different environments.
7676
* `hostName` (Optional) By default, host name will be the OS hostname.
77-
* `autoProfiling` (Optional) If set to `false`, disables automatic profiling and reporting. `agent.profile()` should be used instead. Useful for environments without support for timers or background tasks.
77+
* `autoProfiling` (Optional) If set to `false`, disables automatic profiling and reporting. `agent.profile()` and `agent.report(callback)` should be used instead. Useful for environments without support for timers or background tasks.
7878
* `debug` (Optional) Enables debug logging.
7979
* `cpuProfilerDisabled`, `allocationProfilerDisabled`, `asyncProfilerDisabled`, `errorProfilerDisabled` (Optional) Disables respective profiler when `true`.
8080
* `includeAgentFrames` (Optional) Set to `true` to not exclude agent stack frames from profiles.
@@ -90,9 +90,7 @@ const span = agent.profile();
9090

9191
// your code here
9292

93-
span.stop(() => {
94-
// stoppped
95-
});
93+
span.stop();
9694
```
9795

9896
Is no callback is provided, `stop()` method returns a promise.

examples/app.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const stackimpact = require('..');
55

66
// StackImpact agent initialization
77
let agent = stackimpact.start({
8+
dashboardAddress: process.env.DASHBOARD_ADDRESS,
89
agentKey: process.env.AGENT_KEY,
910
appName: 'ExampleNodejsApp',
1011
appVersion: '1.0.0',

lib/agent.js

+31-26
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class Agent {
167167
catch(err) {
168168
self.exception(err);
169169
}
170-
}
170+
};
171171

172172
process.once('exit', self.exitHandlerFunc);
173173

@@ -263,41 +263,46 @@ class Agent {
263263
}
264264

265265
if(reporters.length > 0) {
266-
rec = reporters[Math.floor(Math.random() * reporters.length)].record();
266+
rec = reporters[Math.floor(Math.random() * reporters.length)].record(true);
267267
}
268268
}
269269

270270
return {
271-
stop: function(callback) {
272-
try {
273-
if (rec) {
274-
rec.stop();
275-
}
276-
277-
if (callback) {
278-
self._tick(() => {
279-
self.isProfiling = false;
280-
callback();
281-
});
282-
}
283-
else {
284-
return new Promise((resolve, reject) => {
285-
self._tick(() => {
286-
self.isProfiling = false;
287-
resolve();
288-
});
289-
});
290-
}
291-
}
292-
catch(err) {
293-
self.exception(err);
271+
stop: function() {
272+
if (rec) {
273+
rec.stop();
294274
}
275+
276+
self.isProfiling = false;
295277
}
296278
};
297279
}
298280

299281

300-
_tick(callback) {
282+
report(callback) {
283+
let self = this;
284+
285+
try {
286+
if (callback) {
287+
self._report(() => {
288+
callback();
289+
});
290+
}
291+
else {
292+
return new Promise((resolve, reject) => {
293+
self._report(() => {
294+
resolve();
295+
});
296+
});
297+
}
298+
}
299+
catch(err) {
300+
self.exception(err);
301+
}
302+
}
303+
304+
305+
_report(callback) {
301306
let self = this;
302307

303308
if (self.getOption('autoProfiling')) {

lib/reporters/allocation_reporter.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class AllocationReporter {
9696
}
9797

9898

99-
record() {
99+
record(rateLimit) {
100100
let self = this;
101101

102102
if (!self.started) {
@@ -108,7 +108,7 @@ class AllocationReporter {
108108
return null;
109109
}
110110

111-
if (self.recordCount++ > self.MAX_RECORD_COUNT) {
111+
if (rateLimit && self.recordCount++ > self.MAX_RECORD_COUNT) {
112112
self.agent.log('Allocation profiler: max recording count reached.');
113113
return null;
114114
}
@@ -207,8 +207,14 @@ class AllocationReporter {
207207
return;
208208
}
209209

210-
if (!self.agent.getOption('autoProfiling') && self.profileStartTs > Date.now() - self.REPORT_INTERVAL) {
211-
return;
210+
if (!self.agent.getOption('autoProfiling')) {
211+
if (self.profileStartTs > Date.now() - self.REPORT_INTERVAL) {
212+
return;
213+
}
214+
else if (self.profileStartTs < Date.now() - 2 * self.REPORT_INTERVAL) {
215+
self.reset();
216+
return;
217+
}
212218
}
213219

214220
if (self.profileDuration === 0) {

lib/reporters/async_reporter.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class AsyncReporter {
194194
}
195195

196196

197-
record() {
197+
record(rateLimit) {
198198
let self = this;
199199

200200
if (!self.started) {
@@ -206,7 +206,7 @@ class AsyncReporter {
206206
return null;
207207
}
208208

209-
if (self.recordCount++ > self.MAX_RECORD_COUNT) {
209+
if (rateLimit && self.recordCount++ > self.MAX_RECORD_COUNT) {
210210
self.agent.log('Async profiler: max recording count reached.');
211211
return null;
212212
}
@@ -392,8 +392,14 @@ class AsyncReporter {
392392
return;
393393
}
394394

395-
if (!self.agent.getOption('autoProfiling') && self.profileStartTs > Date.now() - self.REPORT_INTERVAL) {
396-
return;
395+
if (!self.agent.getOption('autoProfiling')) {
396+
if (self.profileStartTs > Date.now() - self.REPORT_INTERVAL) {
397+
return;
398+
}
399+
else if (self.profileStartTs < Date.now() - 2 * self.REPORT_INTERVAL) {
400+
self.reset();
401+
return;
402+
}
397403
}
398404

399405
if (self.profileDuration === 0) {

lib/reporters/cpu_reporter.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class CpuReporter {
9494
}
9595

9696

97-
record() {
97+
record(rateLimit) {
9898
let self = this;
9999

100100
if (!self.started) {
@@ -106,7 +106,7 @@ class CpuReporter {
106106
return null;
107107
}
108108

109-
if (self.recordCount++ > self.MAX_RECORD_COUNT) {
109+
if (rateLimit && self.recordCount++ > self.MAX_RECORD_COUNT) {
110110
self.agent.log('CPU profiler: max recording count reached.');
111111
return null;
112112
}
@@ -209,8 +209,14 @@ class CpuReporter {
209209
return;
210210
}
211211

212-
if (!self.agent.getOption('autoProfiling') && self.profileStartTs > Date.now() - self.REPORT_INTERVAL) {
213-
return;
212+
if (!self.agent.getOption('autoProfiling')) {
213+
if (self.profileStartTs > Date.now() - self.REPORT_INTERVAL) {
214+
return;
215+
}
216+
else if (self.profileStartTs < Date.now() - 2 * self.REPORT_INTERVAL) {
217+
self.reset();
218+
return;
219+
}
214220
}
215221

216222
if (self.profileDuration === 0) {

node-gyp-fallback.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ catch(err) {
1717
let gyp = child_process.spawn('node-gyp', ['rebuild'], {cwd: process.cwd(), env: process.env, stdio: 'inherit'});
1818

1919
gyp.on('error', (err) => {
20+
console.log('node-gyp not found.');
2021
process.exit(1);
2122
});
2223

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stackimpact",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "StackImpact Node.js Agent",
55
"author": "StackImpact <devops@stackimpact.com>",
66
"keywords": [

test/agent.test.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ describe('Agent', () => {
6161
let p = agent.profile();
6262

6363
setTimeout(() => {
64-
p.stop(() => {
64+
p.stop();
65+
agent.report(() => {
6566
assert(
6667
agent.cpuReporter.profileDuration > 0 ||
6768
agent.allocationReporter.profileDuration > 0 ||
@@ -94,18 +95,20 @@ describe('Agent', () => {
9495
}
9596
};
9697

97-
agent.cpuReporter.profileStartTs = Date.now() - 130 * 1000;
98-
agent.cpuReporter.profileDuration = 1;
99-
agent.allocationReporter.profileStartTs = Date.now() - 130 * 1000;
100-
agent.allocationReporter.profileDuration = 1;
101-
agent.asyncReporter.profileStartTs = Date.now() - 130 * 1000;
102-
agent.asyncReporter.profileDuration = 1;
103-
agent.messageQueue.lastFlushTs = agent.utils.timestamp() - 20;
104-
10598
let p = agent.profile();
10699

107100
setTimeout(() => {
108-
p.stop(() => {
101+
p.stop();
102+
103+
agent.cpuReporter.profileStartTs = Date.now() - 130 * 1000;
104+
agent.cpuReporter.profileDuration = 1;
105+
agent.allocationReporter.profileStartTs = Date.now() - 130 * 1000;
106+
agent.allocationReporter.profileDuration = 1;
107+
agent.asyncReporter.profileStartTs = Date.now() - 130 * 1000;
108+
agent.asyncReporter.profileDuration = 1;
109+
agent.messageQueue.lastFlushTs = agent.utils.timestamp() - 20;
110+
111+
agent.report(() => {
109112
assert(configDone);
110113
assert(uploadDone);
111114

@@ -129,7 +132,8 @@ describe('Agent', () => {
129132
let p = agent.profile();
130133

131134
setTimeout(() => {
132-
p.stop(() => {
135+
p.stop();
136+
agent.report(() => {
133137
let metrics = agent.readMetrics();
134138
assert.equal(metrics[0].category, 'cpu-profile');
135139

0 commit comments

Comments
 (0)