Skip to content

Commit 1aa314f

Browse files
committed
added async tracking and cursor tracking
1 parent 67dfe22 commit 1aa314f

File tree

6 files changed

+89
-8
lines changed

6 files changed

+89
-8
lines changed

lib/hijack/async.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var Fibers = Npm.require('fibers');
2+
var Future = Npm.require('fibers/future');
3+
4+
var originalWait = Future.prototype.wait;
5+
Future.prototype.wait = function wait() {
6+
if(Fibers.current && Fibers.current.__apmInfo && !this._apmInfo) {
7+
var success = NotificationManager.methodTrackEvent('asyncstart');
8+
if(success) {
9+
this._apmInfo = Fibers.current.__apmInfo;
10+
}
11+
}
12+
return originalWait.call(this);
13+
};
14+
15+
var originalReturn = Future.prototype.return;
16+
Future.prototype.return = function(value) {
17+
if(this._apmInfo) {
18+
NotificationManager.methodTrackEvent('asyncend', null, this._apmInfo);
19+
}
20+
return originalReturn.call(this, value);
21+
};

lib/hijack/db.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,32 @@
1616
var ret = originalFunc.apply(this, arguments);
1717
NotificationManager.methodTrackEvent('dbend');
1818

19+
if(func == 'find' && !ret.constructor.prototype._ampOk) {
20+
hijackCursor(ret.constructor.prototype);
21+
}
22+
1923
return ret;
2024
};
21-
});
25+
});
26+
27+
function hijackCursor(cursorProto) {
28+
['forEach', 'map', 'rewind', 'fetch', 'count'].forEach(function(type) {
29+
var originalFunc = cursorProto[type];
30+
cursorProto[type] = function() {
31+
var cursorDescription = this._cursorDescription;
32+
var payload = {
33+
coll: cursorDescription.collectionName,
34+
selector: cursorDescription.selector,
35+
func: type
36+
};
37+
38+
NotificationManager.methodTrackEvent('cursorstart', payload);
39+
var ret = originalFunc.apply(this, arguments);
40+
NotificationManager.methodTrackEvent('cursorend');
41+
42+
return ret;
43+
};
44+
});
45+
46+
cursorProto._ampOk = true;
47+
}

lib/notification_manager.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ function _NotficationManager() {
66

77
}
88

9-
_NotficationManager.prototype.methodTrackEvent = function(type, data) {
10-
var ampInfo = Fibers.current.__apmInfo;
9+
_NotficationManager.prototype.methodTrackEvent = function(type, data, ampInfo) {
10+
ampInfo = ampInfo || Fibers.current.__apmInfo;
1111
if(ampInfo && ampInfo.method) {
1212
var method = this._ensureMethodExists(ampInfo);
13+
if(type =='asyncstart' && this._isLastEventIsStart(method)) {
14+
return false;
15+
}
16+
1317
var event = {type: type, at: Date.now()};
1418
if(data) {
1519
event.data = data;
1620
}
1721
method.events.push(event);
18-
console.log('method::', type, Fibers.current.__apmInfo, data);
22+
console.log('method::', type, ampInfo, data);
23+
return true;
24+
} else {
25+
return false;
1926
}
2027
};
2128

@@ -34,4 +41,13 @@ _NotficationManager.prototype._ensureMethodExists = function(ampInfo) {
3441
return MethodsStore[id];
3542
};
3643

44+
_NotficationManager.prototype._isLastEventIsStart = function(method) {
45+
var lastEvent = method.events[method.events.length -1];
46+
if(lastEvent && ['dbstart', 'httpstart', 'cursorstart'].indexOf(lastEvent.type) >= 0) {
47+
return true;
48+
} else {
49+
return false;
50+
}
51+
};
52+
3753
NotificationManager = new _NotficationManager();

lib/test_methods.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
Posts = new Meteor.Collection('posts');
2+
Future = Npm.require('fibers/future');
23

34
Meteor.methods({
45
hello: function() {
56
var content = HTTP.get('http://localhost:3000');
67
Posts.insert({aa: 100});
7-
return Posts.find({}).count();
8+
var cnt = Posts.find({}).fetch();
9+
10+
Meteor._wrapAsync(wait)(1000);
11+
return cnt;
12+
},
13+
14+
async: function() {
15+
var waitSync = Future.wrap(wait);
16+
waitSync(1000).wait();
17+
return 2000;
818
}
9-
});
19+
});
20+
21+
function wait(time, done) {
22+
setTimeout(function() {
23+
done();
24+
}, time)
25+
}

package.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Package.on_use(function(api) {
1414
'lib/hijack/session.js',
1515
'lib/hijack/db.js',
1616
'lib/hijack/http.js',
17+
'lib/hijack/async.js',
1718
'lib/test_methods.js'
1819
], 'server');
1920
});

todo

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
Script to Sync Package to Test
55
Hijack Sessions
66
Hijack DB
7+
add cursor methods
78
Notification Manager
89
* Api Initialization
910

1011
* Some Other Stuff to Hijack
1112
----------------------------
12-
* HTTP
13+
HTTP
1314
* Email
14-
* Other Async Time (HiJack Future)
15+
Other Async Time (HiJack Future)
1516

1617
##Pub/Sub
1718

0 commit comments

Comments
 (0)