How to enable NODE_DEBUG without environment variables?

I am trying to debug what seems like a socket error in the node.js https library environment when working in the AWS Lambda environment (Node.js 4.3). This problem arises extremely quickly and only under heavy load. My team was able to consistently reproduce the load test problem, and we would like to enable debug logging from the https module.

I found in the node documentation that I can turn on debug logging by setting the environment variable NODE_DEBUG=https . However, I don't think I can set environment variables: How do I use environment variables for AWS Lambda? . Also, I have no way to change the command line that Lambda uses to call my function.

Is there any other way to create the same debug log as setting NODE_DEBUG ?

+7
aws-lambda node-debugger
source share
4 answers

Here monkeypatch:

 const util = require('util'); let debuglog = util.debuglog; util.debuglog = set => { if (set === 'https') { let pid = process.pid; return function() { let msg = util.format.apply(util, arguments); console.error('%s %d: %s', set, pid, msg); } } return debuglog(set); } // This has to occur _after_ the code above: const https = require('https'); 

Basically, it allows debugging logs for https regardless of $NODE_DEBUG . Easily rewritable to work in any module tested with Node v4 and v6.

+3
source share

child_process.fork () allows a single module to create a new node environment with the specified environment variables. These two processes can send messages to each other via send() and receive these messages through message events.

For example, if your current main module is named server.js , you can add a temporary start.js module (which will be a new lambda function) in the same directory, which looks something like this:

 // Add NODE_DEBUG to this process environment variables, which are passed by default to // the forked node environment. process.env.NODE_DEBUG = 'https'; const cp = require('child_process'); const n = cp.fork('server.js'); // Cached callback functions, in case client can pass in different callbacks. // Use an object instead of array, to avoid memory leaks. const cbs = {}; var cbIndexCounter = 0; // To make callback indices unique // Call appropriate callback with response from child process, and delete cached callback. n.on('message', (m) => { cbs[m.cbIndex](m.error, m.result); delete cbs[m.cbIndex]; }); n.on('error', (err) => { console.log('Child node env error: ', err); }); // Cache the callback; forward event, context, index to child process; and increment index. exports.myHandler = function(event, context, callback) { cbs[cbIndexCounter] = callback; n.send({ event: event, context: context, cbIndex: cbIndexCounter++ }); } 

The server.js module can be slightly modified by adding a message event listener:

 process.on('message', (m) => { exports.myHandler(m.event, m.context, function(error, result) { process.send({ error: error, result: result, cbIndex: m.cbIndex }); }); }); // The rest of your original code ... exports.myHandler = function (event, context, callback) { // Whatever you need here... } 
+1
source share

do the following:

 node app.js --https-debug 

and inside your app.js there is this at the beginning of the script

  process.argv.forEach((val, index) => { if(val.match(/--https-debug/)) { process.env.NODE_DEBUG = "https"; } }); 

process.env - an object, like any object, but node sets environment variables on it, you can always hack it and override any variables from the global node environment.

we use process.argv to capture all the arguments sent to the node js file in the terminal.

0
source share

I am not familiar with Aws Lambda, but maybe you can still export the variable to a command, as shown below:

 NODE_DEBUG=https node app.js 
-2
source share

All Articles