Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: add diagnostic channel http.server.response.created #55622

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/diagnostics_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,14 @@ Emitted when client receives a response.

Emitted when server receives a request.

`http.server.response.created`

* `request` {http.IncomingMessage}
* `response` {http.ServerResponse}

Emitted when server creates a response.
The event is emitted before the response is sent.

`http.server.response.finish`

* `request` {http.IncomingMessage}
Expand Down
7 changes: 7 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ let debug = require('internal/util/debuglog').debuglog('http', (fn) => {

const dc = require('diagnostics_channel');
const onRequestStartChannel = dc.channel('http.server.request.start');
const onResponseCreatedChannel = dc.channel('http.server.response.created');
const onResponseFinishChannel = dc.channel('http.server.response.finish');

const kServerResponse = Symbol('ServerResponse');
Expand Down Expand Up @@ -224,6 +225,12 @@ function ServerResponse(req, options) {
this._traceEventId = getNextTraceEventId();
traceBegin(HTTP_SERVER_TRACE_EVENT_NAME, this._traceEventId);
}
if (onResponseCreatedChannel.hasSubscribers) {
onResponseCreatedChannel.publish({
request: req,
response: this,
});
}
}
ObjectSetPrototypeOf(ServerResponse.prototype, OutgoingMessage.prototype);
ObjectSetPrototypeOf(ServerResponse, OutgoingMessage);
Expand Down
45 changes: 45 additions & 0 deletions test/parallel/test-diagnostic-channel-http-response-created.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const dc = require('diagnostics_channel');

const isOutgoingMessage = (object) => object instanceof http.OutgoingMessage;
const isIncomingMessage = (object) => object instanceof http.IncomingMessage;

dc.subscribe('http.server.response.created', common.mustCall(({
request,
response,
}) => {
assert.strictEqual(request.headers.foo, 'bar');
assert.strictEqual(response.getHeader('baz'), undefined);
assert.strictEqual(isIncomingMessage(request), true);
assert.strictEqual(isOutgoingMessage(response), true);
}));

dc.subscribe('http.server.response.finish', common.mustCall(({
request,
response,
}) => {
assert.strictEqual(request.headers.foo, 'bar');
assert.strictEqual(response.getHeader('baz'), 'bar');
assert.strictEqual(isIncomingMessage(request), true);
assert.strictEqual(isOutgoingMessage(response), true);
}));

const server = http.createServer(common.mustCall((_, res) => {
res.setHeader('baz', 'bar');
res.end('done');
}));

server.listen(() => {
const { port } = server.address();
http.get({
port,
headers: {
'foo': 'bar',
}
}, common.mustCall(() => {
server.close();
}));
});
8 changes: 8 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,14 @@ dc.subscribe('http.server.response.finish', common.mustCall(({
assert.strictEqual(isHTTPServer(server), true);
}));

dc.subscribe('http.server.response.created', common.mustCall(({
request,
response,
}) => {
assert.strictEqual(isIncomingMessage(request), true);
assert.strictEqual(isOutgoingMessage(response), true);
}));

dc.subscribe('http.client.request.created', common.mustCall(({ request }) => {
assert.strictEqual(isOutgoingMessage(request), true);
assert.strictEqual(isHTTPServer(server), true);
Expand Down
Loading