Skip to content

Commit

Permalink
http: export diagnostics channel for http
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Jun 26, 2022
1 parent 42ad967 commit 008ae8f
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 5 deletions.
58 changes: 58 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -3605,6 +3605,64 @@ try {
}
```

## `http.diagnosticsChannel`

<!-- YAML
added: REPLACEME
-->

* `onClientRequestStart`
* `onClientResponseFinish`
* `onHTTPRequestStart`
* `onHTTPResponseFinish`

Examples:

```js
'use strict';

const http = require('http');

http.diagnosticsChannel.onClientRequestStart.subscribe(({ request }) => {
});

http.diagnosticsChannel.onClientResponseFinish.subscribe(({ request, response }) => {

});

http.diagnosticsChannel.onHTTPRequestStart.subscribe(({
request,
response,
socket,
server,
}) => {

});

http.diagnosticsChannel.onHTTPResponseFinish.subscribe(({
request,
response,
socket,
server,
}) => {

});

const server = http.createServer((req, res) => {
res.end('done');
});

server.listen(() => {
const { port } = server.address();
http.get(`http://localhost:${port}`, (res) => {
res.resume();
res.on('end', () => {
server.close();
});
});
});
```

[RFC 8187]: https://www.rfc-editor.org/rfc/rfc8187.txt
[`'checkContinue'`]: #event-checkcontinue
[`'finish'`]: #event-finish
Expand Down
19 changes: 18 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ const {

const kClientRequestStatistics = Symbol('ClientRequestStatistics');

const dc = require('diagnostics_channel');
const onClientRequestStartChannel = dc.channel('http.client.request.start');
const onClientResponseFinishChannel = dc.channel('http.client.response.finish');

const { addAbortSignal, finished } = require('stream');

let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
Expand Down Expand Up @@ -367,6 +371,11 @@ ClientRequest.prototype._finish = function _finish() {
},
});
}
if (onClientRequestStartChannel.hasSubscribers) {
onClientRequestStartChannel.publish({
request: this,
});
}
};

ClientRequest.prototype._implicitHeader = function _implicitHeader() {
Expand Down Expand Up @@ -645,6 +654,12 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
},
});
}
if (onClientResponseFinishChannel.hasSubscribers) {
onClientResponseFinishChannel.publish({
request: req,
response: res,
});
}
req.res = res;
res.req = req;

Expand Down Expand Up @@ -927,5 +942,7 @@ ClientRequest.prototype.clearTimeout = function clearTimeout(cb) {
};

module.exports = {
ClientRequest
ClientRequest,
onClientRequestStartChannel,
onClientResponseFinishChannel,
};
4 changes: 3 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,5 +1080,7 @@ module.exports = {
setupConnectionsTracking,
storeHTTPOptions,
_connectionListener: connectionListener,
kServerResponse
kServerResponse,
onRequestStartChannel,
onResponseFinishChannel,
};
18 changes: 15 additions & 3 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ const {
} = primordials;

const httpAgent = require('_http_agent');
const { ClientRequest } = require('_http_client');
const {
ClientRequest,
onClientRequestStartChannel,
onClientResponseFinishChannel,
} = require('_http_client');
const { methods } = require('_http_common');
const { IncomingMessage } = require('_http_incoming');
const {
Expand All @@ -40,7 +44,9 @@ const {
_connectionListener,
STATUS_CODES,
Server,
ServerResponse
ServerResponse,
onRequestStartChannel,
onResponseFinishChannel,
} = require('_http_server');
let maxHeaderSize;

Expand Down Expand Up @@ -123,7 +129,13 @@ module.exports = {
validateHeaderName,
validateHeaderValue,
get,
request
request,
diagnosticsChannel: {
onClientRequestStart: onClientRequestStartChannel,
onClientResponseFinish: onClientResponseFinishChannel,
onHTTPRequestStart: onRequestStartChannel,
onHTTPResponseFinish: onResponseFinishChannel,
},
};

ObjectDefineProperty(module.exports, 'maxHeaderSize', {
Expand Down
51 changes: 51 additions & 0 deletions test/parallel/test-diagnostics-channel-http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

http.diagnosticsChannel.onClientRequestStart.subscribe(common.mustCall(({ request }) => {
assert.strictEqual(typeof request, 'object');
}));

http.diagnosticsChannel.onClientResponseFinish.subscribe(common.mustCall(({ request, response }) => {
assert.strictEqual(typeof request, 'object');
assert.strictEqual(typeof response, 'object');
}));

http.diagnosticsChannel.onHTTPRequestStart.subscribe(common.mustCall(({
request,
response,
socket,
server,
}) => {
assert.strictEqual(typeof request, 'object');
assert.strictEqual(typeof response, 'object');
assert.strictEqual(typeof socket, 'object');
assert.strictEqual(typeof server, 'object');
}));

http.diagnosticsChannel.onHTTPResponseFinish.subscribe(common.mustCall(({
request,
response,
socket,
server,
}) => {
assert.strictEqual(typeof request, 'object');
assert.strictEqual(typeof response, 'object');
assert.strictEqual(typeof socket, 'object');
assert.strictEqual(typeof server, 'object');
}));

const server = http.createServer(common.mustCall((req, res) => {
res.end('done');
}));

server.listen(() => {
const { port } = server.address();
http.get(`http://localhost:${port}`, (res) => {
res.resume();
res.on('end', () => {
server.close();
});
});
});

0 comments on commit 008ae8f

Please sign in to comment.