Skip to content

Commit

Permalink
http: add diagnostics channel for http client
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Jun 27, 2022
1 parent 42ad967 commit d0d3d67
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 1 deletion.
102 changes: 102 additions & 0 deletions doc/api/diagnostics_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,108 @@ channel.subscribe(onMessage);
channel.unsubscribe(onMessage);
```

## Example

```mjs
import http from 'node:http';
import dc from 'node:diagnostics_channel';

const onClientRequestStart = dc.channel('http.client.request.start');
const onClientResponseFinish = dc.channel('http.client.response.finish');
const onServerRequestStart = dc.channel('http.server.request.start');
const onServerResponseFinish = dc.channel('http.server.response.finish');

onClientRequestStart.subscribe(({ request }) => {

});

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

});

onServerRequestStart.subscribe(({
request,
response,
socket,
server,
}) => {

});

onServerResponseFinish.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();
});
});
});
```

```cjs
const http = require('http');
const dc = require('diagnostics_channel');

const onClientRequestStart = dc.channel('http.client.request.start');
const onClientResponseFinish = dc.channel('http.client.response.finish');
const onServerRequestStart = dc.channel('http.server.request.start');
const onServerResponseFinish = dc.channel('http.server.response.finish');

onClientRequestStart.subscribe(({ request }) => {

});

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

});

onServerRequestStart.subscribe(({
request,
response,
socket,
server,
}) => {

});

onServerResponseFinish.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();
});
});
});
```

[`'uncaughtException'`]: process.md#event-uncaughtexception
[`channel.subscribe(onMessage)`]: #channelsubscribeonmessage
[`diagnostics_channel.channel(name)`]: #diagnostics_channelchannelname
15 changes: 15 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = {
validateHeaderName,
validateHeaderValue,
get,
request
request,
};

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

const onClientRequestStart = dc.channel('http.client.request.start');
const onClientResponseFinish = dc.channel('http.client.response.finish');
const onServerRequestStart = dc.channel('http.server.request.start');
const onServerResponseFinish = dc.channel('http.server.response.finish');

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

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

onServerRequestStart.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');
}));

onServerResponseFinish.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 d0d3d67

Please sign in to comment.