Last active
November 24, 2015 21:45
-
-
Save duffn/ef39bcb91bf1af5126d1 to your computer and use it in GitHub Desktop.
How to log LoopBack API requests to Keen.io. https://medium.com/@duffn/logging-loopback-api-requests-to-keen-io-9945b52f4243
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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