From e91169a8e16725ab749cf4889229364302960bdd Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Thu, 31 Oct 2024 08:04:25 -0500 Subject: [PATCH] http: add diagnostic channel `http.client.request.created` PR-URL: https://github.com/nodejs/node/pull/55586 Fixes: https://github.com/nodejs/node/issues/55352 Reviewed-By: Paolo Insogna Reviewed-By: Jake Yuesong Li Reviewed-By: theanarkh --- doc/api/diagnostics_channel.md | 7 ++++ lib/_http_client.js | 6 +++ ...diagnostic-channel-http-request-created.js | 39 +++++++++++++++++++ .../parallel/test-diagnostics-channel-http.js | 5 +++ 4 files changed, 57 insertions(+) create mode 100644 test/parallel/test-diagnostic-channel-http-request-created.js diff --git a/doc/api/diagnostics_channel.md b/doc/api/diagnostics_channel.md index 66f395fa70319e..2467329c7729a2 100644 --- a/doc/api/diagnostics_channel.md +++ b/doc/api/diagnostics_channel.md @@ -1123,6 +1123,13 @@ independently. #### HTTP +`http.client.request.created` + +* `request` {http.ClientRequest} + +Emitted when client creates a request object. +Unlike `http.client.request.start`, this event is emitted before the request has been sent. + `http.client.request.start` * `request` {http.ClientRequest} diff --git a/lib/_http_client.js b/lib/_http_client.js index a0009cfb8f432b..91ba264339fa4f 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -89,6 +89,7 @@ const { const kClientRequestStatistics = Symbol('ClientRequestStatistics'); const dc = require('diagnostics_channel'); +const onClientRequestCreatedChannel = dc.channel('http.client.request.created'); const onClientRequestStartChannel = dc.channel('http.client.request.start'); const onClientRequestErrorChannel = dc.channel('http.client.request.error'); const onClientResponseFinishChannel = dc.channel('http.client.response.finish'); @@ -373,6 +374,11 @@ function ClientRequest(input, options, cb) { this.onSocket(net.createConnection(opts)); } } + if (onClientRequestCreatedChannel.hasSubscribers) { + onClientRequestCreatedChannel.publish({ + request: this, + }); + } } ObjectSetPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype); ObjectSetPrototypeOf(ClientRequest, OutgoingMessage); diff --git a/test/parallel/test-diagnostic-channel-http-request-created.js b/test/parallel/test-diagnostic-channel-http-request-created.js new file mode 100644 index 00000000000000..bbadfabcd9704f --- /dev/null +++ b/test/parallel/test-diagnostic-channel-http-request-created.js @@ -0,0 +1,39 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); +const dc = require('diagnostics_channel'); + +const isHTTPServer = (server) => server instanceof http.Server; +const isOutgoingMessage = (object) => object instanceof http.OutgoingMessage; + +dc.subscribe('http.client.request.created', common.mustCall(({ request }) => { + assert.strictEqual(request.getHeader('foo'), 'bar'); + assert.strictEqual(request.getHeader('baz'), undefined); + assert.strictEqual(isOutgoingMessage(request), true); + assert.strictEqual(isHTTPServer(server), true); +})); + +dc.subscribe('http.client.request.start', common.mustCall(({ request }) => { + assert.strictEqual(request.getHeader('foo'), 'bar'); + assert.strictEqual(request.getHeader('baz'), 'bar'); + assert.strictEqual(isOutgoingMessage(request), true); +})); + +const server = http.createServer(common.mustCall((_, res) => { + res.end('done'); +})); + +server.listen(async () => { + const { port } = server.address(); + const req = http.request({ + port, + headers: { + 'foo': 'bar', + } + }, common.mustCall(() => { + server.close(); + })); + req.setHeader('baz', 'bar'); + req.end(); +}); diff --git a/test/parallel/test-diagnostics-channel-http.js b/test/parallel/test-diagnostics-channel-http.js index e134b9ac05a85d..cc212e3f6207f5 100644 --- a/test/parallel/test-diagnostics-channel-http.js +++ b/test/parallel/test-diagnostics-channel-http.js @@ -53,6 +53,11 @@ dc.subscribe('http.server.response.finish', common.mustCall(({ assert.strictEqual(isHTTPServer(server), true); })); +dc.subscribe('http.client.request.created', common.mustCall(({ request }) => { + assert.strictEqual(isOutgoingMessage(request), true); + assert.strictEqual(isHTTPServer(server), true); +}, 2)); + const server = http.createServer(common.mustCall((req, res) => { res.end('done'); }));