Permalink
Browse files

session hijacked and add context to the fiber

  • Loading branch information...
arunoda committed Nov 13, 2013
1 parent 5a6a606 commit a5ae02717795884e0b86dfd00c4ed23aa66564ac
View
@@ -0,0 +1,27 @@
Meteor.methods({
'__hijackSession': function() {
var sessions = Meteor.server.sessions;
for(var id in sessions) {
var session = sessions[id];
//if called __hijackSession even after the hijack is completed
if(session.__apkOk) {
return;
}
sessionProto = session.constructor.prototype;
//do the wrapping
MeteorAPM._wrapSession(sessionProto);
console.log('Meteor APM Initialized!');
session.__apkOk = true;
}
}
});
Meteor.startup(function() {
var conn = DDP.connect('http://localhost:' + process.env.PORT);
conn.call('__hijackSession', function() {
conn.disconnect();
});
});
View
@@ -0,0 +1 @@
MeteorAPM = {};
View
@@ -0,0 +1,35 @@
var Fibers = Npm.require('fibers');
MeteorAPM._wrapSession = function(sessionProto) {
console.log('wrapping.....');
//adding the method context to the current fiber
//track the start of the method
var originalMethodHandler = sessionProto.protocol_handlers.method;
sessionProto.protocol_handlers.method = function(msg, unblock) {
//add context
Fibers.current.__apmInfo = {
session: this.id,
userId: this.userId,
method: msg.id
};
//todo: Track Method Start Time
originalMethodHandler.call(this, msg, unblock);
};
//track method ending (to get the result of error)
var originalSend = sessionProto.send;
sessionProto.send = function(msg) {
if(msg.msg == 'result') {
if(msg.error) {
//todo: send the details of the error object with stack trace
//todo: track the end of the message
} else if(msg.result) {
//todo: track the end of the message
}
}
originalSend.call(this, msg);
};
};
View
@@ -0,0 +1,16 @@
Package.describe({
"summery": "APM for Meteor"
});
Npm.depends({
});
Package.on_use(function(api) {
api.use(['minimongo', 'livedata', 'mongo-livedata', 'ejson'], ['server']);
api.add_files([
'lib/namespace.js',
'lib/wrap_session.js',
'lib/hijack_session.js'
], 'server');
});
View
14 sync.sh
@@ -0,0 +1,14 @@
#!/bin/bash
## Tiny script to copy the package into the test meteor app
DEST_PATH=./test/packages/meteor-apm
# reset and create the package-folder
if [[ -d $DEST_PATH ]]; then
rm -rf $DEST_PATH
fi
mkdir -p $DEST_PATH
cp -rf lib $DEST_PATH/lib
cp package.js $DEST_PATH/package.js
View
@@ -0,0 +1 @@
local
View
@@ -0,0 +1,10 @@
# Meteor packages used by this project, one per line.
#
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.
standard-app-packages
autopublish
insecure
preserve-inputs
meteor-apm
View
@@ -0,0 +1 @@
0.6.6.3
@@ -0,0 +1 @@
.build*
@@ -0,0 +1,27 @@
Meteor.methods({
'__hijackSession': function() {
var sessions = Meteor.server.sessions;
for(var id in sessions) {
var session = sessions[id];
//if called __hijackSession even after the hijack is completed
if(session.__apkOk) {
return;
}
sessionProto = session.constructor.prototype;
//do the wrapping
MeteorAPM._wrapSession(sessionProto);
console.log('Meteor APM Initialized!');
session.__apkOk = true;
}
}
});
Meteor.startup(function() {
var conn = DDP.connect('http://localhost:' + process.env.PORT);
conn.call('__hijackSession', function() {
conn.disconnect();
});
});
@@ -0,0 +1 @@
MeteorAPM = {};
@@ -0,0 +1,35 @@
var Fibers = Npm.require('fibers');
MeteorAPM._wrapSession = function(sessionProto) {
console.log('wrapping.....');
//adding the method context to the current fiber
//track the start of the method
var originalMethodHandler = sessionProto.protocol_handlers.method;
sessionProto.protocol_handlers.method = function(msg, unblock) {
//add context
Fibers.current.__apmInfo = {
session: this.id,
userId: this.userId,
method: msg.id
};
//todo: Track Method Start Time
originalMethodHandler.call(this, msg, unblock);
};
//track method ending (to get the result of error)
var originalSend = sessionProto.send;
sessionProto.send = function(msg) {
if(msg.msg == 'result') {
if(msg.error) {
//todo: send the details of the error object with stack trace
//todo: track the end of the message
} else if(msg.result) {
//todo: track the end of the message
}
}
originalSend.call(this, msg);
};
};
@@ -0,0 +1,16 @@
Package.describe({
"summery": "APM for Meteor"
});
Npm.depends({
});
Package.on_use(function(api) {
api.use(['minimongo', 'livedata', 'mongo-livedata', 'ejson'], ['server']);
api.add_files([
'lib/namespace.js',
'lib/wrap_session.js',
'lib/hijack_session.js'
], 'server');
});
View
8 todo
@@ -0,0 +1,8 @@
Script to Sync Package to Test
Hijack Sessions
* Hijack DB
* Hijack HTTP
* Notification Manager
* Api Initialization

0 comments on commit a5ae027

Please sign in to comment.