Tracing

  1. Stability: 1 - Experimental

The tracing module is designed for instrumenting your Node application. It is
not meant for general purpose use.

Be very careful with callbacks used in conjunction with this module

Many of these callbacks interact directly with asynchronous subsystems in a
synchronous fashion. That is to say, you may be in a callback where a call to
console.log() could result in an infinite recursive loop. Also of note, many
of these callbacks are in hot execution code paths. That is to say your
callbacks are executed quite often in the normal operation of Node, so be wary
of doing CPU bound or synchronous workloads in these functions. Consider a ring
buffer and a timer to defer processing.

require('tracing') to use this module.

v8

The v8 property is an EventEmitter, it exposes events and interfaces
specific to the version of v8 built with node. These interfaces are subject
to change by upstream and are therefore not covered under the stability index.

Event: ‘gc’

function (before, after) { }

Emitted each time a GC run is completed.

before and after are objects with the following properties:

  1. {
  2. type: 'mark-sweep-compact',
  3. flags: 0,
  4. timestamp: 905535650119053,
  5. total_heap_size: 6295040,
  6. total_heap_size_executable: 4194304,
  7. total_physical_size: 6295040,
  8. used_heap_size: 2855416,
  9. heap_size_limit: 1535115264
  10. }

getHeapStatistics()

Returns an object with the following properties

  1. {
  2. total_heap_size: 7326976,
  3. total_heap_size_executable: 4194304,
  4. total_physical_size: 7326976,
  5. used_heap_size: 3476208,
  6. heap_size_limit: 1535115264
  7. }

Async Listeners

The AsyncListener API is the JavaScript interface for the AsyncWrap
class which allows developers to be notified about key events in the
lifetime of an asynchronous event. Node performs a lot of asynchronous
events internally, and significant use of this API may have a
significant performance impact on your application.

tracing.createAsyncListener(callbacksObj[, userData])

  • callbacksObj {Object} Contains optional callbacks that will fire at
    specific times in the life cycle of the asynchronous event.
  • userData {Value} a value that will be passed to all callbacks.

Returns a constructed AsyncListener object.

To begin capturing asynchronous events pass either the callbacksObj or
pass an existing AsyncListener instance to tracing.addAsyncListener().
The same AsyncListener instance can only be added once to the active
queue, and subsequent attempts to add the instance will be ignored.

To stop capturing pass the AsyncListener instance to
tracing.removeAsyncListener(). This does not mean the
AsyncListener previously added will stop triggering callbacks. Once
attached to an asynchronous event it will persist with the lifetime of the
asynchronous call stack.

Explanation of function parameters:

callbacksObj: An Object which may contain several optional fields:

  • create(userData): A Function called when an asynchronous
    event is instantiated. If a Value is returned then it will be attached
    to the event and overwrite any value that had been passed to
    tracing.createAsyncListener()‘s userData argument. If an initial
    userData was passed when created, then create() will
    receive that as a function argument.

  • before(context, userData): A Function that is called immediately
    before the asynchronous callback is about to run. It will be passed both
    the context (i.e. this) of the calling function and the userData
    either returned from create() or passed during construction (if
    either occurred).

  • after(context, userData): A Function called immediately after
    the asynchronous event’s callback has run. Note this will not be called
    if the callback throws and the error is not handled.

  • error(userData, error): A Function called if the event’s
    callback threw. If this registered callback returns true then Node will
    assume the error has been properly handled and resume execution normally.
    When multiple error() callbacks have been registered only one of
    those callbacks needs to return true for AsyncListener to accept that
    the error has been handled, but all error() callbacks will always be run.

userData: A Value (i.e. anything) that will be, by default,
attached to all new event instances. This will be overwritten if a Value
is returned by create().

Here is an example of overwriting the userData:

  1. tracing.createAsyncListener({
  2. create: function listener(value) {
  3. // value === true
  4. return false;
  5. }, {
  6. before: function before(context, value) {
  7. // value === false
  8. }
  9. }, true);

Note: The EventEmitter, while used to emit status of an asynchronous
event, is not itself asynchronous. So create() will not fire when
an event is added, and before()/after() will not fire when emitted
callbacks are called.

tracing.addAsyncListener(callbacksObj[, userData])

tracing.addAsyncListener(asyncListener)

Returns a constructed AsyncListener object and immediately adds it to
the listening queue to begin capturing asynchronous events.

Function parameters can either be the same as
tracing.createAsyncListener(), or a constructed AsyncListener
object.

Example usage for capturing errors:

  1. var fs = require('fs');
  2. var cntr = 0;
  3. var key = tracing.addAsyncListener({
  4. create: function onCreate() {
  5. return { uid: cntr++ };
  6. },
  7. before: function onBefore(context, storage) {
  8. // Write directly to stdout or we'll enter a recursive loop
  9. fs.writeSync(1, 'uid: ' + storage.uid + ' is about to run\n');
  10. },
  11. after: function onAfter(context, storage) {
  12. fs.writeSync(1, 'uid: ' + storage.uid + ' ran\n');
  13. },
  14. error: function onError(storage, err) {
  15. // Handle known errors
  16. if (err.message === 'everything is fine') {
  17. // Writing to stderr this time.
  18. fs.writeSync(2, 'handled error just threw:\n');
  19. fs.writeSync(2, err.stack + '\n');
  20. return true;
  21. }
  22. }
  23. });
  24. process.nextTick(function() {
  25. throw new Error('everything is fine');
  26. });
  27. // Output:
  28. // uid: 0 is about to run
  29. // handled error just threw:
  30. // Error: really, it's ok
  31. // at /tmp/test2.js:27:9
  32. // at process._tickCallback (node.js:583:11)
  33. // at Function.Module.runMain (module.js:492:11)
  34. // at startup (node.js:123:16)
  35. // at node.js:1012:3

tracing.removeAsyncListener(asyncListener)

Removes the AsyncListener from the listening queue.

Removing the AsyncListener from the active queue does not mean the
asyncListener callbacks will cease to fire on the events they’ve been
registered. Subsequently, any asynchronous events fired during the
execution of a callback will also have the same asyncListener callbacks
attached for future execution. For example:

  1. var fs = require('fs');
  2. var key = tracing.createAsyncListener({
  3. create: function asyncListener() {
  4. // Write directly to stdout or we'll enter a recursive loop
  5. fs.writeSync(1, 'You summoned me?\n');
  6. }
  7. });
  8. // We want to begin capturing async events some time in the future.
  9. setTimeout(function() {
  10. tracing.addAsyncListener(key);
  11. // Perform a few additional async events.
  12. setTimeout(function() {
  13. setImmediate(function() {
  14. process.nextTick(function() { });
  15. });
  16. });
  17. // Removing the listener doesn't mean to stop capturing events that
  18. // have already been added.
  19. tracing.removeAsyncListener(key);
  20. }, 100);
  21. // Output:
  22. // You summoned me?
  23. // You summoned me?
  24. // You summoned me?
  25. // You summoned me?

The fact that we logged 4 asynchronous events is an implementation detail
of Node’s Timers.

To stop capturing from a specific asynchronous event stack
tracing.removeAsyncListener() must be called from within the call
stack itself. For example:

  1. var fs = require('fs');
  2. var key = tracing.createAsyncListener({
  3. create: function asyncListener() {
  4. // Write directly to stdout or we'll enter a recursive loop
  5. fs.writeSync(1, 'You summoned me?\n');
  6. }
  7. });
  8. // We want to begin capturing async events some time in the future.
  9. setTimeout(function() {
  10. tracing.addAsyncListener(key);
  11. // Perform a few additional async events.
  12. setImmediate(function() {
  13. // Stop capturing from this call stack.
  14. tracing.removeAsyncListener(key);
  15. process.nextTick(function() { });
  16. });
  17. }, 100);
  18. // Output:
  19. // You summoned me?

The user must be explicit and always pass the AsyncListener they wish
to remove. It is not possible to simply remove all listeners at once.