Skip to content

Commit 9045232

Browse files
committed
added basic api initialization
1 parent 1aa314f commit 9045232

File tree

6 files changed

+99
-2
lines changed

6 files changed

+99
-2
lines changed

lib/apm.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var http = Npm.require('http');
2+
Apm = {};
3+
4+
Apm.connect = function(appId, appSecret, options) {
5+
options = options || {};
6+
7+
var endpoint = options.endpoint || "http://localhost:11011";
8+
9+
if(appId && appSecret) {
10+
schedulePayloadSend();
11+
} else {
12+
throw new Error('APM: AppId and AppSecret required!');
13+
}
14+
15+
var retries = 0;
16+
17+
function sendPayload() {
18+
var payload = {methods: _.values(MethodsStore)};
19+
MethodsStore = {};
20+
var headers = {'METEOR_APM_APPID': appId, 'METEOR_APM_SECRET': appSecret};
21+
var httpOptions = {headers: headers, data: payload};
22+
23+
callHTTP();
24+
25+
function callHTTP() {
26+
try {
27+
var response = HTTP.call('POST', endpoint, httpOptions);
28+
processResponse(response);
29+
} catch(err) {
30+
tryAgain(err);
31+
}
32+
}
33+
34+
function processResponse(response) {
35+
if(response.statusCode == '401') {
36+
throw new Error('APM: AppId, AppSecret combination is invalid');
37+
} else if(response.statusCode == '200') {
38+
//success send again in 10 secs
39+
schedulePayloadSend();
40+
} else {
41+
tryAgain();
42+
}
43+
}
44+
45+
function tryAgain(err) {
46+
if(++retries < 5) {
47+
Meteor.setTimeout(callHTTP, 1000 * retries);
48+
} else {
49+
console.error('APM: Error sending payload(dropped after 5 tries) ', err.message);
50+
retries = 0;
51+
schedulePayloadSend();
52+
}
53+
}
54+
55+
}
56+
57+
function schedulePayloadSend() {
58+
Meteor.setTimeout(sendPayload, 1000 * 10);
59+
}
60+
};

lib/notification_manager.js

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ function _NotficationManager() {
77
}
88

99
_NotficationManager.prototype.methodTrackEvent = function(type, data, ampInfo) {
10+
if(!Fibers.current) {
11+
//we don't need to track methodEvents when there don't exists a fiber
12+
return;
13+
}
14+
1015
ampInfo = ampInfo || Fibers.current.__apmInfo;
1116
if(ampInfo && ampInfo.method) {
1217
var method = this._ensureMethodExists(ampInfo);

package.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Npm.depends({
66

77
});
88

9+
910
Package.on_use(function(api) {
1011
api.use(['minimongo', 'livedata', 'mongo-livedata', 'ejson', 'underscore', 'http'], ['server']);
1112
api.add_files([
@@ -15,6 +16,9 @@ Package.on_use(function(api) {
1516
'lib/hijack/db.js',
1617
'lib/hijack/http.js',
1718
'lib/hijack/async.js',
19+
'lib/apm.js',
1820
'lib/test_methods.js'
1921
], 'server');
20-
});
22+
23+
api.export('Apm');
24+
});

test/connect.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if(Meteor.isServer) {
2+
Apm.connect('appId', 'appSecret');
3+
}

todo

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Hijack Sessions
66
Hijack DB
77
add cursor methods
88
Notification Manager
9-
* Api Initialization
9+
Api Initialization
1010

1111
* Some Other Stuff to Hijack
1212
----------------------------

tools/mock_engine.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var http = require('http');
2+
var app = http.createServer(function(req, res) {
3+
console.log("AUTH:", req.headers);
4+
var data = "";
5+
req.on('data', ondata);
6+
req.once('end', onend);
7+
req.socket.setKeepAlive(false);
8+
9+
res.writeHead(200);
10+
11+
function ondata(d) {
12+
data += d.toString();
13+
}
14+
15+
function onend() {
16+
console.log('DATA:', data);
17+
req.removeListener('data', ondata);
18+
res.end();
19+
}
20+
});
21+
22+
var port = process.env.PORT || 11011;
23+
console.log('Mock APM Engine started on port: ', port);
24+
app.listen(port);
25+

0 commit comments

Comments
 (0)