|
5 | 5 | The tracing module is designed for instrumenting your Node application. It is
|
6 | 6 | not meant for general purpose use.
|
7 | 7 |
|
8 |
| -***Be very careful with callbacks used in conjunction with this module*** |
9 |
| - |
10 |
| -Many of these callbacks interact directly with asynchronous subsystems in a |
11 |
| -synchronous fashion. That is to say, you may be in a callback where a call to |
12 |
| -`console.log()` could result in an infinite recursive loop. Also of note, many |
13 |
| -of these callbacks are in hot execution code paths. That is to say your |
14 |
| -callbacks are executed quite often in the normal operation of Node, so be wary |
15 |
| -of doing CPU bound or synchronous workloads in these functions. Consider a ring |
16 |
| -buffer and a timer to defer processing. |
17 |
| - |
18 | 8 | `require('tracing')` to use this module.
|
19 | 9 |
|
20 | 10 | ## v8
|
@@ -73,216 +63,4 @@ v8.setFlagsFromString('--trace_gc');
|
73 | 63 | setTimeout(function() { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
|
74 | 64 | ```
|
75 | 65 |
|
76 |
| - |
77 |
| -# Async Listeners |
78 |
| - |
79 |
| -The `AsyncListener` API is the JavaScript interface for the `AsyncWrap` |
80 |
| -class which allows developers to be notified about key events in the |
81 |
| -lifetime of an asynchronous event. Node performs a lot of asynchronous |
82 |
| -events internally, and significant use of this API may have a |
83 |
| -**significant performance impact** on your application. |
84 |
| - |
85 |
| - |
86 |
| -## tracing.createAsyncListener(callbacksObj[, userData]) |
87 |
| - |
88 |
| -* `callbacksObj` {Object} Contains optional callbacks that will fire at |
89 |
| -specific times in the life cycle of the asynchronous event. |
90 |
| -* `userData` {Value} a value that will be passed to all callbacks. |
91 |
| - |
92 |
| -Returns a constructed `AsyncListener` object. |
93 |
| - |
94 |
| -To begin capturing asynchronous events pass either the `callbacksObj` or |
95 |
| -pass an existing `AsyncListener` instance to [`tracing.addAsyncListener()`][]. |
96 |
| -The same `AsyncListener` instance can only be added once to the active |
97 |
| -queue, and subsequent attempts to add the instance will be ignored. |
98 |
| - |
99 |
| -To stop capturing pass the `AsyncListener` instance to |
100 |
| -[`tracing.removeAsyncListener()`][]. This does _not_ mean the |
101 |
| -`AsyncListener` previously added will stop triggering callbacks. Once |
102 |
| -attached to an asynchronous event it will persist with the lifetime of the |
103 |
| -asynchronous call stack. |
104 |
| - |
105 |
| -Explanation of function parameters: |
106 |
| - |
107 |
| - |
108 |
| -`callbacksObj`: An `Object` which may contain several optional fields: |
109 |
| - |
110 |
| -* `create(userData)`: A `Function` called when an asynchronous |
111 |
| -event is instantiated. If a `Value` is returned then it will be attached |
112 |
| -to the event and overwrite any value that had been passed to |
113 |
| -`tracing.createAsyncListener()`'s `userData` argument. If an initial |
114 |
| -`userData` was passed when created, then `create()` will |
115 |
| -receive that as a function argument. |
116 |
| - |
117 |
| -* `before(context, userData)`: A `Function` that is called immediately |
118 |
| -before the asynchronous callback is about to run. It will be passed both |
119 |
| -the `context` (i.e. `this`) of the calling function and the `userData` |
120 |
| -either returned from `create()` or passed during construction (if |
121 |
| -either occurred). |
122 |
| - |
123 |
| -* `after(context, userData)`: A `Function` called immediately after |
124 |
| -the asynchronous event's callback has run. Note this will not be called |
125 |
| -if the callback throws and the error is not handled. |
126 |
| - |
127 |
| -* `error(userData, error)`: A `Function` called if the event's |
128 |
| -callback threw. If this registered callback returns `true` then Node will |
129 |
| -assume the error has been properly handled and resume execution normally. |
130 |
| -When multiple `error()` callbacks have been registered only **one** of |
131 |
| -those callbacks needs to return `true` for `AsyncListener` to accept that |
132 |
| -the error has been handled, but all `error()` callbacks will always be run. |
133 |
| - |
134 |
| -`userData`: A `Value` (i.e. anything) that will be, by default, |
135 |
| -attached to all new event instances. This will be overwritten if a `Value` |
136 |
| -is returned by `create()`. |
137 |
| - |
138 |
| -Here is an example of overwriting the `userData`: |
139 |
| - |
140 |
| - tracing.createAsyncListener({ |
141 |
| - create: function listener(value) { |
142 |
| - // value === true |
143 |
| - return false; |
144 |
| - }, { |
145 |
| - before: function before(context, value) { |
146 |
| - // value === false |
147 |
| - } |
148 |
| - }, true); |
149 |
| - |
150 |
| -**Note:** The [EventEmitter][], while used to emit status of an asynchronous |
151 |
| -event, is not itself asynchronous. So `create()` will not fire when |
152 |
| -an event is added, and `before()`/`after()` will not fire when emitted |
153 |
| -callbacks are called. |
154 |
| - |
155 |
| - |
156 |
| -## tracing.addAsyncListener(callbacksObj[, userData]) |
157 |
| -## tracing.addAsyncListener(asyncListener) |
158 |
| - |
159 |
| -Returns a constructed `AsyncListener` object and immediately adds it to |
160 |
| -the listening queue to begin capturing asynchronous events. |
161 |
| - |
162 |
| -Function parameters can either be the same as |
163 |
| -[`tracing.createAsyncListener()`][], or a constructed `AsyncListener` |
164 |
| -object. |
165 |
| - |
166 |
| -Example usage for capturing errors: |
167 |
| - |
168 |
| - var fs = require('fs'); |
169 |
| - |
170 |
| - var cntr = 0; |
171 |
| - var key = tracing.addAsyncListener({ |
172 |
| - create: function onCreate() { |
173 |
| - return { uid: cntr++ }; |
174 |
| - }, |
175 |
| - before: function onBefore(context, storage) { |
176 |
| - // Write directly to stdout or we'll enter a recursive loop |
177 |
| - fs.writeSync(1, 'uid: ' + storage.uid + ' is about to run\n'); |
178 |
| - }, |
179 |
| - after: function onAfter(context, storage) { |
180 |
| - fs.writeSync(1, 'uid: ' + storage.uid + ' ran\n'); |
181 |
| - }, |
182 |
| - error: function onError(storage, err) { |
183 |
| - // Handle known errors |
184 |
| - if (err.message === 'everything is fine') { |
185 |
| - // Writing to stderr this time. |
186 |
| - fs.writeSync(2, 'handled error just threw:\n'); |
187 |
| - fs.writeSync(2, err.stack + '\n'); |
188 |
| - return true; |
189 |
| - } |
190 |
| - } |
191 |
| - }); |
192 |
| - |
193 |
| - process.nextTick(function() { |
194 |
| - throw new Error('everything is fine'); |
195 |
| - }); |
196 |
| - |
197 |
| - // Output: |
198 |
| - // uid: 0 is about to run |
199 |
| - // handled error just threw: |
200 |
| - // Error: really, it's ok |
201 |
| - // at /tmp/test2.js:27:9 |
202 |
| - // at process._tickCallback (node.js:583:11) |
203 |
| - // at Function.Module.runMain (module.js:492:11) |
204 |
| - // at startup (node.js:123:16) |
205 |
| - // at node.js:1012:3 |
206 |
| - |
207 |
| -## tracing.removeAsyncListener(asyncListener) |
208 |
| - |
209 |
| -Removes the `AsyncListener` from the listening queue. |
210 |
| - |
211 |
| -Removing the `AsyncListener` from the active queue does _not_ mean the |
212 |
| -`asyncListener` callbacks will cease to fire on the events they've been |
213 |
| -registered. Subsequently, any asynchronous events fired during the |
214 |
| -execution of a callback will also have the same `asyncListener` callbacks |
215 |
| -attached for future execution. For example: |
216 |
| - |
217 |
| - var fs = require('fs'); |
218 |
| - |
219 |
| - var key = tracing.createAsyncListener({ |
220 |
| - create: function asyncListener() { |
221 |
| - // Write directly to stdout or we'll enter a recursive loop |
222 |
| - fs.writeSync(1, 'You summoned me?\n'); |
223 |
| - } |
224 |
| - }); |
225 |
| - |
226 |
| - // We want to begin capturing async events some time in the future. |
227 |
| - setTimeout(function() { |
228 |
| - tracing.addAsyncListener(key); |
229 |
| - |
230 |
| - // Perform a few additional async events. |
231 |
| - setTimeout(function() { |
232 |
| - setImmediate(function() { |
233 |
| - process.nextTick(function() { }); |
234 |
| - }); |
235 |
| - }); |
236 |
| - |
237 |
| - // Removing the listener doesn't mean to stop capturing events that |
238 |
| - // have already been added. |
239 |
| - tracing.removeAsyncListener(key); |
240 |
| - }, 100); |
241 |
| - |
242 |
| - // Output: |
243 |
| - // You summoned me? |
244 |
| - // You summoned me? |
245 |
| - // You summoned me? |
246 |
| - // You summoned me? |
247 |
| - |
248 |
| -The fact that we logged 4 asynchronous events is an implementation detail |
249 |
| -of Node's [Timers][]. |
250 |
| - |
251 |
| -To stop capturing from a specific asynchronous event stack |
252 |
| -`tracing.removeAsyncListener()` must be called from within the call |
253 |
| -stack itself. For example: |
254 |
| - |
255 |
| - var fs = require('fs'); |
256 |
| - |
257 |
| - var key = tracing.createAsyncListener({ |
258 |
| - create: function asyncListener() { |
259 |
| - // Write directly to stdout or we'll enter a recursive loop |
260 |
| - fs.writeSync(1, 'You summoned me?\n'); |
261 |
| - } |
262 |
| - }); |
263 |
| - |
264 |
| - // We want to begin capturing async events some time in the future. |
265 |
| - setTimeout(function() { |
266 |
| - tracing.addAsyncListener(key); |
267 |
| - |
268 |
| - // Perform a few additional async events. |
269 |
| - setImmediate(function() { |
270 |
| - // Stop capturing from this call stack. |
271 |
| - tracing.removeAsyncListener(key); |
272 |
| - |
273 |
| - process.nextTick(function() { }); |
274 |
| - }); |
275 |
| - }, 100); |
276 |
| - |
277 |
| - // Output: |
278 |
| - // You summoned me? |
279 |
| - |
280 |
| -The user must be explicit and always pass the `AsyncListener` they wish |
281 |
| -to remove. It is not possible to simply remove all listeners at once. |
282 |
| - |
283 |
| - |
284 |
| -[EventEmitter]: events.html#events_class_events_eventemitter |
285 |
| -[Timers]: timers.html |
286 |
| -[`tracing.createAsyncListener()`]: #tracing_tracing_createasynclistener_asynclistener_callbacksobj_storagevalue |
287 |
| -[`tracing.addAsyncListener()`]: #tracing_tracing_addasynclistener_asynclistener |
288 |
| -[`tracing.removeAsyncListener()`]: #tracing_tracing_removeasynclistener_asynclistener |
| 66 | + |
0 commit comments