GeezerVison is showing github.com — http: add diagnostic channel `http.client.request.created` · nodejs/node@25b1422
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/diagnostics_channel.md +7Lines changed: 7 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1123,6 +1123,13 @@ independently. | 1123 | 1123 |
| 1124 | 1124 | #### HTTP | 1125 | 1125 |
| | 1126 | +`http.client.request.created` | | 1127 | + | | 1128 | +* `request` {http.ClientRequest} | | 1129 | + | | 1130 | +Emitted when client creates a request object. | | 1131 | +Unlike `http.client.request.start`, this event is emitted before the request has been sent. | | 1132 | + | 1126 | 1133 | `http.client.request.start` | 1127 | 1134 |
| 1128 | 1135 | * `request` {http.ClientRequest} | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_client.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -89,6 +89,7 @@ const { | 89 | 89 | const kClientRequestStatistics = Symbol('ClientRequestStatistics'); | 90 | 90 |
| 91 | 91 | const dc = require('diagnostics_channel'); | | 92 | +const onClientRequestCreatedChannel = dc.channel('http.client.request.created'); | 92 | 93 | const onClientRequestStartChannel = dc.channel('http.client.request.start'); | 93 | 94 | const onClientRequestErrorChannel = dc.channel('http.client.request.error'); | 94 | 95 | const onClientResponseFinishChannel = dc.channel('http.client.response.finish'); | @@ -373,6 +374,11 @@ function ClientRequest(input, options, cb) { | 373 | 374 | this.onSocket(net.createConnection(opts)); | 374 | 375 | } | 375 | 376 | } | | 377 | + if (onClientRequestCreatedChannel.hasSubscribers) { | | 378 | + onClientRequestCreatedChannel.publish({ | | 379 | + request: this, | | 380 | + }); | | 381 | + } | 376 | 382 | } | 377 | 383 | ObjectSetPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype); | 378 | 384 | ObjectSetPrototypeOf(ClientRequest, OutgoingMessage); | |
Collapse file Copy file name to clipboard +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const common = require('../common'); | | 3 | +const assert = require('assert'); | | 4 | +const http = require('http'); | | 5 | +const dc = require('diagnostics_channel'); | | 6 | + | | 7 | +const isHTTPServer = (server) => server instanceof http.Server; | | 8 | +const isOutgoingMessage = (object) => object instanceof http.OutgoingMessage; | | 9 | + | | 10 | +dc.subscribe('http.client.request.created', common.mustCall(({ request }) => { | | 11 | + assert.strictEqual(request.getHeader('foo'), 'bar'); | | 12 | + assert.strictEqual(request.getHeader('baz'), undefined); | | 13 | + assert.strictEqual(isOutgoingMessage(request), true); | | 14 | + assert.strictEqual(isHTTPServer(server), true); | | 15 | +})); | | 16 | + | | 17 | +dc.subscribe('http.client.request.start', common.mustCall(({ request }) => { | | 18 | + assert.strictEqual(request.getHeader('foo'), 'bar'); | | 19 | + assert.strictEqual(request.getHeader('baz'), 'bar'); | | 20 | + assert.strictEqual(isOutgoingMessage(request), true); | | 21 | +})); | | 22 | + | | 23 | +const server = http.createServer(common.mustCall((_, res) => { | | 24 | + res.end('done'); | | 25 | +})); | | 26 | + | | 27 | +server.listen(async () => { | | 28 | + const { port } = server.address(); | | 29 | + const req = http.request({ | | 30 | + port, | | 31 | + headers: { | | 32 | + 'foo': 'bar', | | 33 | + } | | 34 | + }, common.mustCall(() => { | | 35 | + server.close(); | | 36 | + })); | | 37 | + req.setHeader('baz', 'bar'); | | 38 | + req.end(); | | 39 | +}); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-diagnostics-channel-http.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,11 @@ dc.subscribe('http.server.response.finish', common.mustCall(({ | 53 | 53 | assert.strictEqual(isHTTPServer(server), true); | 54 | 54 | })); | 55 | 55 |
| | 56 | +dc.subscribe('http.client.request.created', common.mustCall(({ request }) => { | | 57 | + assert.strictEqual(isOutgoingMessage(request), true); | | 58 | + assert.strictEqual(isHTTPServer(server), true); | | 59 | +}, 2)); | | 60 | + | 56 | 61 | const server = http.createServer(common.mustCall((req, res) => { | 57 | 62 | res.end('done'); | 58 | 63 | })); | |
|