Skip to content

Commit

Permalink
http: add diagnostic channel http.client.request.created
Browse files Browse the repository at this point in the history
PR-URL: nodejs#55586
Fixes: nodejs#55352
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Jake Yuesong Li <[email protected]>
Reviewed-By: theanarkh <[email protected]>
  • Loading branch information
marco-ippolito authored and tpoisseau committed Nov 21, 2024
1 parent 16abab2 commit 04bf62d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/api/diagnostics_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 6 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-diagnostic-channel-http-request-created.js
Original file line number Diff line number Diff line change
@@ -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();
});
5 changes: 5 additions & 0 deletions test/parallel/test-diagnostics-channel-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}));
Expand Down

0 comments on commit 04bf62d

Please sign in to comment.