Skip to content

Instantly share code, notes, and snippets.

@duffn
Last active November 24, 2015 21:45
Show Gist options
  • Save duffn/ef39bcb91bf1af5126d1 to your computer and use it in GitHub Desktop.
Save duffn/ef39bcb91bf1af5126d1 to your computer and use it in GitHub Desktop.
var Keen = require('keen-js');
module.exports = function (myModel) {
myModel.afterRemote('*', function (ctx, affectedModelInstance, next) {
// Listen to the finish event of the response to wait
// for the response to be available
ctx.res.on('finish', function () {
var client = new Keen({
projectId: 'keenprojectid',
writeKey: 'keenwritekey'
});
var queryEvent = {
ip: ctx.req.ip,
baseUrl: ctx.req.baseUrl,
url: ctx.req.url,
route: ctx.req.route,
query: ctx.req.query,
method: ctx.methodString,
// Here's the header that I wanted to grab
responseTime: Number(ctx.res._headers['x-response-time']),
keen: {
timestamp: new Date().toISOString()
}
};
client.addEvent('queries', queryEvent,
function (err, res) {
if (err) {
console.log(err);
} else {
console.log(res);
}
});
});
// Don't forget to call next
next();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment