{
  "source": "doc/api/tracing.markdown",
  "modules": [
    {
      "textRaw": "Tracing",
      "name": "tracing",
      "stability": 1,
      "stabilityText": "Experimental",
      "desc": "<p>The tracing module is designed for instrumenting your Node application. It is\nnot meant for general purpose use.\n\n</p>\n<p><strong><em>Be very careful with callbacks used in conjunction with this module</em></strong>\n\n</p>\n<p>Many of these callbacks interact directly with asynchronous subsystems in a\nsynchronous fashion. That is to say, you may be in a callback where a call to\n<code>console.log()</code> could result in an infinite recursive loop.  Also of note, many\nof these callbacks are in hot execution code paths. That is to say your\ncallbacks are executed quite often in the normal operation of Node, so be wary\nof doing CPU bound or synchronous workloads in these functions. Consider a ring\nbuffer and a timer to defer processing.\n\n</p>\n<p><code>require(&#39;tracing&#39;)</code> to use this module.\n\n</p>\n",
      "modules": [
        {
          "textRaw": "v8",
          "name": "v8",
          "desc": "<p>The <code>v8</code> property is an [EventEmitter][], it exposes events and interfaces\nspecific to the version of <code>v8</code> built with node. These interfaces are subject\nto change by upstream and are therefore not covered under the stability index.\n\n</p>\n",
          "events": [
            {
              "textRaw": "Event: 'gc'",
              "type": "event",
              "name": "gc",
              "desc": "<p><code>function (before, after) { }</code>\n\n</p>\n<p>Emitted each time a GC run is completed.\n\n</p>\n<p><code>before</code> and <code>after</code> are objects with the following properties:\n\n</p>\n<pre><code>{\n  type: &#39;mark-sweep-compact&#39;,\n  flags: 0,\n  timestamp: 905535650119053,\n  total_heap_size: 6295040,\n  total_heap_size_executable: 4194304,\n  total_physical_size: 6295040,\n  used_heap_size: 2855416,\n  heap_size_limit: 1535115264\n}</code></pre>\n",
              "params": []
            }
          ],
          "methods": [
            {
              "textRaw": "getHeapStatistics()",
              "type": "method",
              "name": "getHeapStatistics",
              "desc": "<p>Returns an object with the following properties\n\n</p>\n<pre><code>{\n  total_heap_size: 7326976,\n  total_heap_size_executable: 4194304,\n  total_physical_size: 7326976,\n  used_heap_size: 3476208,\n  heap_size_limit: 1535115264\n}</code></pre>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            }
          ],
          "type": "module",
          "displayName": "v8"
        }
      ],
      "type": "module",
      "displayName": "Tracing"
    },
    {
      "textRaw": "Async Listeners",
      "name": "async_listeners",
      "desc": "<p>The <code>AsyncListener</code> API is the JavaScript interface for the <code>AsyncWrap</code>\nclass which allows developers to be notified about key events in the\nlifetime of an asynchronous event. Node performs a lot of asynchronous\nevents internally, and significant use of this API may have a\n<strong>significant performance impact</strong> on your application.\n\n\n</p>\n",
      "methods": [
        {
          "textRaw": "tracing.createAsyncListener(callbacksObj[, userData])",
          "type": "method",
          "name": "createAsyncListener",
          "signatures": [
            {
              "params": [
                {
                  "textRaw": "`callbacksObj` {Object} Contains optional callbacks that will fire at specific times in the life cycle of the asynchronous event. ",
                  "name": "callbacksObj",
                  "type": "Object",
                  "desc": "Contains optional callbacks that will fire at specific times in the life cycle of the asynchronous event."
                },
                {
                  "textRaw": "`userData` {Value} a value that will be passed to all callbacks. ",
                  "name": "userData",
                  "type": "Value",
                  "desc": "a value that will be passed to all callbacks."
                }
              ]
            },
            {
              "params": [
                {
                  "name": "callbacksObj["
                },
                {
                  "name": "userData"
                }
              ]
            }
          ],
          "desc": "<p>Returns a constructed <code>AsyncListener</code> object.\n\n</p>\n<p>To begin capturing asynchronous events pass either the <code>callbacksObj</code> or\npass an existing <code>AsyncListener</code> instance to [<code>tracing.addAsyncListener()</code>][].\nThe same <code>AsyncListener</code> instance can only be added once to the active\nqueue, and subsequent attempts to add the instance will be ignored.\n\n</p>\n<p>To stop capturing pass the <code>AsyncListener</code> instance to\n[<code>tracing.removeAsyncListener()</code>][]. This does <em>not</em> mean the\n<code>AsyncListener</code> previously added will stop triggering callbacks. Once\nattached to an asynchronous event it will persist with the lifetime of the\nasynchronous call stack.\n\n</p>\n<p>Explanation of function parameters:\n\n\n</p>\n<p><code>callbacksObj</code>: An <code>Object</code> which may contain several optional fields:\n\n</p>\n<ul>\n<li><p><code>create(userData)</code>: A <code>Function</code> called when an asynchronous\nevent is instantiated. If a <code>Value</code> is returned then it will be attached\nto the event and overwrite any value that had been passed to\n<code>tracing.createAsyncListener()</code>&#39;s <code>userData</code> argument. If an initial\n<code>userData</code> was passed when created, then <code>create()</code> will\nreceive that as a function argument.</p>\n</li>\n<li><p><code>before(context, userData)</code>: A <code>Function</code> that is called immediately\nbefore the asynchronous callback is about to run. It will be passed both\nthe <code>context</code> (i.e. <code>this</code>) of the calling function and the <code>userData</code>\neither returned from <code>create()</code> or passed during construction (if\neither occurred).</p>\n</li>\n<li><p><code>after(context, userData)</code>: A <code>Function</code> called immediately after\nthe asynchronous event&#39;s callback has run. Note this will not be called\nif the callback throws and the error is not handled.</p>\n</li>\n<li><p><code>error(userData, error)</code>: A <code>Function</code> called if the event&#39;s\ncallback threw. If this registered callback returns <code>true</code> then Node will\nassume the error has been properly handled and resume execution normally.\nWhen multiple <code>error()</code> callbacks have been registered only <strong>one</strong> of\nthose callbacks needs to return <code>true</code> for <code>AsyncListener</code> to accept that\nthe error has been handled, but all <code>error()</code> callbacks will always be run.</p>\n</li>\n</ul>\n<p><code>userData</code>: A <code>Value</code> (i.e. anything) that will be, by default,\nattached to all new event instances. This will be overwritten if a <code>Value</code>\nis returned by <code>create()</code>.\n\n</p>\n<p>Here is an example of overwriting the <code>userData</code>:\n\n</p>\n<pre><code>tracing.createAsyncListener({\n  create: function listener(value) {\n    // value === true\n    return false;\n}, {\n  before: function before(context, value) {\n    // value === false\n  }\n}, true);</code></pre>\n<p><strong>Note:</strong> The [EventEmitter][], while used to emit status of an asynchronous\nevent, is not itself asynchronous. So <code>create()</code> will not fire when\nan event is added, and <code>before()</code>/<code>after()</code> will not fire when emitted\ncallbacks are called.\n\n\n</p>\n"
        },
        {
          "textRaw": "tracing.addAsyncListener(callbacksObj[, userData])",
          "type": "method",
          "name": "addAsyncListener",
          "desc": "<p>Returns a constructed <code>AsyncListener</code> object and immediately adds it to\nthe listening queue to begin capturing asynchronous events.\n\n</p>\n<p>Function parameters can either be the same as\n[<code>tracing.createAsyncListener()</code>][], or a constructed <code>AsyncListener</code>\nobject.\n\n</p>\n<p>Example usage for capturing errors:\n\n</p>\n<pre><code>var fs = require(&#39;fs&#39;);\n\nvar cntr = 0;\nvar key = tracing.addAsyncListener({\n  create: function onCreate() {\n    return { uid: cntr++ };\n  },\n  before: function onBefore(context, storage) {\n    // Write directly to stdout or we&#39;ll enter a recursive loop\n    fs.writeSync(1, &#39;uid: &#39; + storage.uid + &#39; is about to run\\n&#39;);\n  },\n  after: function onAfter(context, storage) {\n    fs.writeSync(1, &#39;uid: &#39; + storage.uid + &#39; ran\\n&#39;);\n  },\n  error: function onError(storage, err) {\n    // Handle known errors\n    if (err.message === &#39;everything is fine&#39;) {\n      // Writing to stderr this time.\n      fs.writeSync(2, &#39;handled error just threw:\\n&#39;);\n      fs.writeSync(2, err.stack + &#39;\\n&#39;);\n      return true;\n    }\n  }\n});\n\nprocess.nextTick(function() {\n  throw new Error(&#39;everything is fine&#39;);\n});\n\n// Output:\n// uid: 0 is about to run\n// handled error just threw:\n// Error: really, it&#39;s ok\n//     at /tmp/test2.js:27:9\n//     at process._tickCallback (node.js:583:11)\n//     at Function.Module.runMain (module.js:492:11)\n//     at startup (node.js:123:16)\n//     at node.js:1012:3</code></pre>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "asyncListener"
                }
              ]
            },
            {
              "params": [
                {
                  "name": "callbacksObj["
                },
                {
                  "name": "userData"
                }
              ]
            }
          ]
        },
        {
          "textRaw": "tracing.addAsyncListener(asyncListener)",
          "type": "method",
          "name": "addAsyncListener",
          "desc": "<p>Returns a constructed <code>AsyncListener</code> object and immediately adds it to\nthe listening queue to begin capturing asynchronous events.\n\n</p>\n<p>Function parameters can either be the same as\n[<code>tracing.createAsyncListener()</code>][], or a constructed <code>AsyncListener</code>\nobject.\n\n</p>\n<p>Example usage for capturing errors:\n\n</p>\n<pre><code>var fs = require(&#39;fs&#39;);\n\nvar cntr = 0;\nvar key = tracing.addAsyncListener({\n  create: function onCreate() {\n    return { uid: cntr++ };\n  },\n  before: function onBefore(context, storage) {\n    // Write directly to stdout or we&#39;ll enter a recursive loop\n    fs.writeSync(1, &#39;uid: &#39; + storage.uid + &#39; is about to run\\n&#39;);\n  },\n  after: function onAfter(context, storage) {\n    fs.writeSync(1, &#39;uid: &#39; + storage.uid + &#39; ran\\n&#39;);\n  },\n  error: function onError(storage, err) {\n    // Handle known errors\n    if (err.message === &#39;everything is fine&#39;) {\n      // Writing to stderr this time.\n      fs.writeSync(2, &#39;handled error just threw:\\n&#39;);\n      fs.writeSync(2, err.stack + &#39;\\n&#39;);\n      return true;\n    }\n  }\n});\n\nprocess.nextTick(function() {\n  throw new Error(&#39;everything is fine&#39;);\n});\n\n// Output:\n// uid: 0 is about to run\n// handled error just threw:\n// Error: really, it&#39;s ok\n//     at /tmp/test2.js:27:9\n//     at process._tickCallback (node.js:583:11)\n//     at Function.Module.runMain (module.js:492:11)\n//     at startup (node.js:123:16)\n//     at node.js:1012:3</code></pre>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "asyncListener"
                }
              ]
            }
          ]
        },
        {
          "textRaw": "tracing.removeAsyncListener(asyncListener)",
          "type": "method",
          "name": "removeAsyncListener",
          "desc": "<p>Removes the <code>AsyncListener</code> from the listening queue.\n\n</p>\n<p>Removing the <code>AsyncListener</code> from the active queue does <em>not</em> mean the\n<code>asyncListener</code> callbacks will cease to fire on the events they&#39;ve been\nregistered. Subsequently, any asynchronous events fired during the\nexecution of a callback will also have the same <code>asyncListener</code> callbacks\nattached for future execution. For example:\n\n</p>\n<pre><code>var fs = require(&#39;fs&#39;);\n\nvar key = tracing.createAsyncListener({\n  create: function asyncListener() {\n    // Write directly to stdout or we&#39;ll enter a recursive loop\n    fs.writeSync(1, &#39;You summoned me?\\n&#39;);\n  }\n});\n\n// We want to begin capturing async events some time in the future.\nsetTimeout(function() {\n  tracing.addAsyncListener(key);\n\n  // Perform a few additional async events.\n  setTimeout(function() {\n    setImmediate(function() {\n      process.nextTick(function() { });\n    });\n  });\n\n  // Removing the listener doesn&#39;t mean to stop capturing events that\n  // have already been added.\n  tracing.removeAsyncListener(key);\n}, 100);\n\n// Output:\n// You summoned me?\n// You summoned me?\n// You summoned me?\n// You summoned me?</code></pre>\n<p>The fact that we logged 4 asynchronous events is an implementation detail\nof Node&#39;s [Timers][].\n\n</p>\n<p>To stop capturing from a specific asynchronous event stack\n<code>tracing.removeAsyncListener()</code> must be called from within the call\nstack itself. For example:\n\n</p>\n<pre><code>var fs = require(&#39;fs&#39;);\n\nvar key = tracing.createAsyncListener({\n  create: function asyncListener() {\n    // Write directly to stdout or we&#39;ll enter a recursive loop\n    fs.writeSync(1, &#39;You summoned me?\\n&#39;);\n  }\n});\n\n// We want to begin capturing async events some time in the future.\nsetTimeout(function() {\n  tracing.addAsyncListener(key);\n\n  // Perform a few additional async events.\n  setImmediate(function() {\n    // Stop capturing from this call stack.\n    tracing.removeAsyncListener(key);\n\n    process.nextTick(function() { });\n  });\n}, 100);\n\n// Output:\n// You summoned me?</code></pre>\n<p>The user must be explicit and always pass the <code>AsyncListener</code> they wish\nto remove. It is not possible to simply remove all listeners at once.\n\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "asyncListener"
                }
              ]
            }
          ]
        }
      ],
      "type": "module",
      "displayName": "Async Listeners"
    }
  ]
}
