Reputation: 3915
I'm building a small developer tool (HUD) for an app written in node. In the tool, I want to surface all HTTP/HTTPS requests that were made in order to serve that page (to display them to the developer and record their number). Is there a clean way to do so, which does not involve wrapping something like http.request()
? If not, what is the lowest level method that would need to be wrapped in order to log both HTTP & HTTPS? How about for TCP connections?
Upvotes: 4
Views: 5633
Reputation: 9529
The answer to this depends on how complicated your code is. If you make 3 or 4 requests for external resources on each request, just manually inserting profiling code there is probably the cleanest and simplest solution.
If you fire many many requests, monkey-patching (wrapping) http.request
and https.request
can be done quite easily.
patcher = (patchMe) ->
original = patchMe.request
patchMe.request = (options, callback) ->
console.log options.host, options.method # or any other counting, profiling
original(options, callback)
http = require 'http'
https = require 'https'
patcher(http)
patcher(https)
If you want to patch more than those requests, on the tcp/udp level, you'll unfortunately have to go read the source to figure out which underlying method you could patch, I haven't gone there.
I enjoyed reading the source of nodetime, which monkey-patches the require
function to profile any uses of specific functions / modules deeper in your code. Go read that source! :)
Upvotes: 6