Long stack traces for node.js inspired by https://github.com/tlrobinson/long-stack-traces

Overview

longjohn

Long stack traces for node.js with configurable call trace length

Inspiration

I wrote this while trying to add long-stack-traces to my server and realizing that there were issues with support of EventEmitter::removeListener. The node HTTP Server will begin to leak callbacks and any of your own code that relies on removing listeners would not work as anticipated.

So what to do... I stole the code and rewrote it. I've added support for removeListener along with the ability to cut off the number of async calls the library will trace. I hope you like it!

Please thank tlrobinson for the initial implementation!

Production Use

Longjohn collects a large amount of data in order to provide useful stack traces. While it is very helpful in development and testing environments, it is not recommended to use longjohn in production. The data collection puts a lot of strain on V8's garbage collector and can greatly slow down heavily-loaded applications.

Installation

Just npm install it!

$ npm install longjohn

Usage

To use longjohn, require it in your code (probably in some initialization code). That's all!

if (process.env.NODE_ENV !== 'production'){
  require('longjohn');
}

// ... your code

Options

Limit traced async calls

longjohn.async_trace_limit = 5;   // defaults to 10
longjohn.async_trace_limit = -1;  // unlimited

Change callback frame text

longjohn.empty_frame = 'ASYNC CALLBACK';  // defaults to '---------------------------------------------'

Use with Source Maps

As of version 0.2.9 longjonn supports source maps. Just compile your code down to javascript with source map support and run like normal.

For coffee-script, this would look like:

$ coffee --map --compile script.coffee
Comments
  • Play nicely with CoffeeScript source maps

    Play nicely with CoffeeScript source maps

    CoffeeScript now supports source maps - and correct line number mapping with errors.

    http://jashkenas.github.com/coffee-script/documentation/docs/coffee-script.html

    However, when longjohn is require'd, it messes with the line mapping (I imagine it's something to do with the override of Error.prepareStackTrace).

    opened by mhart 11
  • Huge CPU usage

    Huge CPU usage

    I have used Node.js (0.10.33) quite long time with 'longjohn' in the clustered application. No any bad affect of 'longjohn' was detected there. The application worked stable and used about 20% of CPU time. Recently I have updated my Nodejs engine up to latest version (0.12.5) and correspondingly did update for all dependencies. The 'longjonh' was updated to 0.2.7 version. But, unfortunately, after restart my application began to eat up to 95-100% of CPU time. The commenting of 'var longjohn = require('longjohn');' fixed the situation and now it's using no more than 20% of CPU time. Can you please explain what is happening? Is it my fault? Or there is some bug?

    opened by shunanya 10
  • Doesn't work with node 0.12.

    Doesn't work with node 0.12.

    Test script:

    var longjohn = require('longjohn');
    longjohn.async_trace_limit = -1;
    
    function timeoutHandler() {
      console.log('timeout fired!');
      throw new Error('EXPLODE');
    }
    
    function createTimeout() {
      setTimeout(timeoutHandler, 1000);
    }
    
    createTimeout();
    

    Results:

    image

    image

    opened by chevtek 8
  • res.json(err) causes

    res.json(err) causes "Converting circular structure to JSON"

    In my app I am json-sending errors to the client for some development environments so that these become visible on the browser.

    I understand that longjohn is adding circular stuff to the default error instances. And so, Express' res.json cannot deal with these error instances anymore.

    So, do you have any tricks or an advice for that?

    opened by binarykitchen 7
  • 0.2.5 Invalid array length

    0.2.5 Invalid array length

    An example stack trace:

    "RangeError: Invalid array length",
    "    at onwrite (_stream_writable.js:265:15)",
    "    at WritableState.onwrite (_stream_writable.js:96:5)",
    "    at Socket._write (net.js:653:5)",
    "    at doWrite (_stream_writable.js:225:10)",
    "    at writeOrBuffer (_stream_writable.js:215:5)",
    "    at Writable.write (_stream_writable.js:182:11)",
    "    at Socket.write (net.js:615:40)",
    

    But these came up from a few other places too. We solved the problem by reverting to 0.2.2

    opened by Trindaz 5
  • No limit on serialization of error

    No limit on serialization of error

    Every so often, we'll run into an error that longjohn tries to serialize, and in attempting to do so will peg the CPU and gobble up RAM (like 1+GB).

    It seems like it attempts to serialize our entire application in a single Object graph...

    uncaughtException: { [Error: Invalid timestamp. Please provide timezone as fallback]
      __cached_trace__: 
       [ { receiver: 
            { localTime: [Function],
              humanizeDuration: [Function] },
           fun: [Function],
           pos: 401 },
         { receiver: undefined,
           fun: [Function: grouper],
           pos: 4432 },
         { receiver: 
            { ArrayBuffer: [Function: ArrayBuffer],
              Int8Array: { [Function: Int8Array] BYTES_PER_ELEMENT: 1 },
              Uint8Array: { [Function: Uint8Array] BYTES_PER_ELEMENT: 1 },
              Uint8ClampedArray: { [Function: Uint8ClampedArray] BYTES_PER_ELEMENT: 1 },
              Int16Array: { [Function: Int16Array] BYTES_PER_ELEMENT: 2 },
              Uint16Array: { [Function: Uint16Array] BYTES_PER_ELEMENT: 2 },
              Int32Array: { [Function: Int32Array] BYTES_PER_ELEMENT: 4 },
              Uint32Array: { [Function: Uint32Array] BYTES_PER_ELEMENT: 4 },
              Float32Array: { [Function: Float32Array] BYTES_PER_ELEMENT: 4 },
              Float64Array: { [Function: Float64Array] BYTES_PER_ELEMENT: 8 },
              DataView: [Function: DataView],
              global: [Circular],
              process: 
               { title: 'node',
                 version: 'v0.8.21',
                 moduleLoadList: 
                  [ 'Binding evals',
                    'Binding natives',
                    'NativeModule events',
                    'NativeModule buffer',
                    'Binding buffer',
                    'NativeModule assert',
                    // several thousands of additional lines omitted ...
    

    It might be worth using util's inspect to mitigate this.

    opened by cdnbacon 5
  • play nicely with coffeescript source maps

    play nicely with coffeescript source maps

    CoffeeScript supports source maps - and correct line number mapping with errors.

    http://jashkenas.github.com/coffee-script/documentation/docs/coffee-script.html

    However, when longjohn is require'd, it messes with the line mapping -- probably something to do with the override of Error.prepareStackTrace. It would be great if longjohn could be fixed to show coffeescript source line numbers, when a coffeescript source map is available.

    opened by zacronos 4
  • Upgrade to grunt 0.4.0, reduce devDependencies down to grunt-contrib-coffee.

    Upgrade to grunt 0.4.0, reduce devDependencies down to grunt-contrib-coffee.

    No need to import the entirety of grunt-contrib.

    Upgraded to grunt 0.4.0 so we can use the newest grunt-contrib-coffee version. File syntax is a little different so I updated that. Tests & compiles are still working.

    opened by ssafejava 4
  • [package] Allow newer versions of source-map-support.

    [package] Allow newer versions of source-map-support.

    I’m using the latest version of babel-node and with the version of source-map-support pinned by longjohn the symbolicated stack traces are incorrect. This change allows newer versions, I did constraint it to < 1.0.0 just in case, but I don’t feel strongly about it so can remove it if you prefer.

    opened by alloy 3
  • Issues with gunzip

    Issues with gunzip

    Hello,

    At work we are using longjohn together with our own async library, which works wonders, only recently we began parsing gzipped files.

    The issue is that when using longjohn that the gunzip of the zlib core module virtually stops responding. We have tested and it responds after a few hours.

    I will include the code that will give this issue:

    var zlib = require('zlib'),
        request = require('request'),
        longjohn = require('longjohn');
    
    request({ url: 'http://www.jaap.nl/sitemaps/sitemap1.xml.gz', encoding: null }, function(error, response, body) {
      if(response.statusCode === 200) {
        zlib.gunzip(body, function (err, data) {
          console.log(data); // This callback is never executed.
        });
      }
    });
    

    When removing the require of longjohn the callback is called without any issue.

    I hope this issue can be fixed.

    Kind regards and Thanks in advance,

    Jasper van Rijbroek

    opened by JaspervRijbroek 3
  • Ensures call stack location object exists before calling methods on it

    Ensures call stack location object exists before calling methods on it

    Fixes the error: TypeError: Cannot call method 'getFunctionName' of undefined

    In longjohn.js:call_stack_location(), if the object stack[2] is not defined it crashes the server with an uncaught exception.

    Signed-off-by: Jim Knochelmann [email protected]

    opened by ghost 3
  • .once only working if there is an .on call for the same event type

    .once only working if there is an .on call for the same event type

    Seeing an issue where .once( 'data' ... ) on a stream will only be hit if there is an .on( 'data'... ) call somewhere on the same eventemitter object.

    I've spent an hour or so in the code trying to figure out the issue, but I am stumped. Could you give me any guidance on why a .once() would not be hit unless there's a .on() for the same event type?

    Specifically I am seeing this using the node-postgres module with longjohn included. In their connection module, I tracked it down to this:

      // without this, .once below seems inconsistent
      this.stream.on( 'data', function() {
        var nop = 1;
      });
    
      this.stream.once('data', function(buffer) {
        var responseCode = buffer.toString('utf8');
        if(responseCode != 'S') {
          return self.emit('error', new Error('The server does not support SSL connections'));
        }
        var tls = require('tls');
        self.stream = tls.connect({
          socket: self.stream,
          servername: host,
          rejectUnauthorized: self.ssl.rejectUnauthorized,
          ca: self.ssl.ca,
          pfx: self.ssl.pfx,
          key: self.ssl.key,
          passphrase: self.ssl.passphrase,
          cert: self.ssl.cert,
          NPNProtocols: self.ssl.NPNProtocols
        });
        self.attachListeners(self.stream);
        self.emit('sslconnect');
    
        self.stream.on('error', function(error){
          self.emit('error', error);
        });
      });
    

    If I don't add the .on( 'data'...) the .once( 'data'... will never be hit while longjohn is loaded.

    opened by andyburke 1
  • Doesn't work with promises

    Doesn't work with promises

    All I get is:

        at Promise.resolve.then (/usr/src/app/lib/promises/index.js:61:15)
        at process._tickDomainCallback (internal/process/next_tick.js:129:7)
    

    Is this project still alive?

    opened by dypsilon 5
  • Using with TypeScript?

    Using with TypeScript?

    I'm using TypeScript and would like to use longjohn conditionally just in dev mode. Documentation suggests using conditional require:

    if (process.env.NODE_ENV !== 'production'){
      require('longjohn');
    }
    

    However, TypeScript imports are restricted to top-level, making it impossible to do such conditional check without falling back to commonJS-style require.

    There is also a related issue: https://github.com/mattinsler/longjohn/issues/61 - having the ability to disable longjohn after require would be a sufficient fix.

    opened by jsalonen 1
  • Can LongJohn be disabled after having been required?

    Can LongJohn be disabled after having been required?

    I'm writing some tests where I want longjohn on and others when it should be off. I could just run the non long john tests first, but it'd be nice to just be able to turn it on and off depending on the sub test suite.

    Is there a way?

    Thanks in advance, Francois

    opened by seflless 0
  • longjohn is not compatible to other modules using a similar wrapping of EventEmitter

    longjohn is not compatible to other modules using a similar wrapping of EventEmitter

    The way how longjohn wraps EventEmitter is incompatible with other modules doing a similar/same wrapping. I tested by creating a clone of longjohn (incl. modifiny the 'longjohn' property of on()) and used it together with unmodified longjohn in following script:

    require('longjohn');
    
    var assert = require('assert');
    var EventEmitter = require('events').EventEmitter;
    
    function onListener() {}
    function onceListener() {}
    
    function test() {
        var emitter = new EventEmitter;
        emitter.on('xxx', onListener);
        emitter.once('xxx', onceListener);
        assert.equal(emitter.listenerCount('xxx'), 2);
    
        emitter.removeListener('xxx', onListener);
        // fails on second call as onListener was not removed
        assert.equal(emitter.listenerCount('xxx'), 1);
    
        emitter.removeListener('xxx', onceListener);
        assert.equal(emitter.listenerCount('xxx'), 0);
    }
    
    test();
    
    require('longjohn-clone');
    
    test();
    
    opened by Flarna 1
Owner
Matt Insler
Matt Insler
📡 Encrypt and authenticate DevTools to use it securely remotely. Add HTTPS, and authentication to --remote-debugging-port to debug, inspect and automate from anywhere and collaborate securely on bugs.

?? Encrypt and authenticate DevTools to use it securely remotely. Add HTTPS, and authentication to --remote-debugging-port to debug, inspect and automate from anywhere and collaborate securely on bugs.

Cris 9 May 5, 2022
A tiny JavaScript debugging utility modelled after Node.js core's debugging technique. Works in Node.js and web browsers

debug A tiny JavaScript debugging utility modelled after Node.js core's debugging technique. Works in Node.js and web browsers. Installation $ npm ins

Sloth 10.5k Dec 30, 2022
Node is running but you don't know why? why-is-node-running is here to help you.

why-is-node-running Node is running but you don't know why? why-is-node-running is here to help you. Installation Node 8 and above: npm i why-is-node-

Mathias Buus 1.5k Dec 30, 2022
An lldb plugin for Node.js and V8, which enables inspection of JavaScript states for insights into Node.js processes and their core dumps.

Node.js v10.x+ C++ plugin for the LLDB debugger. The llnode plugin adds the ability to inspect JavaScript stack frames, objects, source code and more

Node.js 1k Dec 14, 2022
Node.js debugger based on Blink Developer Tools

Node Inspector Overview Node Inspector is a debugger interface for Node.js applications that uses the Blink Developer Tools (formerly WebKit Web Inspe

null 12.6k Dec 29, 2022
ndb is an improved debugging experience for Node.js, enabled by Chrome DevTools

ndb ndb is an improved debugging experience for Node.js, enabled by Chrome DevTools Installation Compatibility: ndb requires Node >=8.0.0. It works be

null 10.8k Dec 28, 2022
[OBSOLETE] runs Node.js programs through Chromium DevTools

devtool ⚠️ Update: This tool is mostly obsolete as much of the philosophy has been brought into Node/DevTool core, see here for details. If you wish t

Jam3 3.8k Dec 20, 2022
Debug Node.js code with Chrome Developer Tools.

Debug Node.js code with Chrome Developer Tools on Linux, Windows and OS X. This software aims to make things easier ?? . With ironNode you have the fu

Stephan Ahlf 2.3k Dec 30, 2022
🐛 Memory leak testing for node.

Leakage - Memory Leak Testing for Node Write leakage tests using Mocha or another test runner of your choice. Does not only support spotting and fixin

Andy Wermke 1.6k Dec 28, 2022
API Observability. Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices.

swagger-stats | API Observability https://swaggerstats.io | Guide Trace API calls and Monitor API performance, health and usage statistics in Node.js

slana.tech 773 Jan 4, 2023
A Node.js tracing and instrumentation utility

njsTrace - Instrumentation and Tracing njstrace lets you easily instrument and trace you code, see all function calls, arguments, return values, as we

Yuval 354 Dec 29, 2022
Locus is a debugging module for node.js

ʆ Locus Locus is a debugging module which allows you to execute commands at runtime via a REPL. Installing npm install locus --save-dev Using require(

Ali Davut 300 Nov 13, 2022
Streamline Your Node.js Debugging Workflow with Chromium (Chrome, Edge, More) DevTools.

NiM (Node.js --inspector Manager) Streamlines your development process Google Chrome Web Store (works with any Chromium browsers: Google's Chrome, Mic

Will 188 Dec 28, 2022
thetool is a CLI tool to capture different cpu, memory and other profiles for your node app in Chrome DevTools friendly format

thetool thetool is a CLI tool to capture different cpu, memory and other profiles for your node app in Chrome DevTools friendly format. Quick start np

null 200 Oct 28, 2022
nest연습용 (w. https://github.com/seuiggi, https://github.com/okysky1121)

A progressive Node.js framework for building efficient and scalable server-side applications. Description Nest framework TypeScript starter repository

이름 2 Oct 5, 2022
He is like Batman, but for Node.js stack traces

Stackman Give Stackman an error and he will give an array of stack frames with extremely detailed information for each frame in the stack trace. With

Thomas Watson 242 Jan 1, 2023
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
The Remix version of the fakebooks app demonstrated on https://remix.run. Check out the CRA version: https://github.com/kentcdodds/fakebooks-cra

Remix Fakebooks App This is a (very) simple implementation of the fakebooks mock app demonstrated on remix.run. There is no database, but there is an

Kent C. Dodds 61 Dec 22, 2022