0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 13:09:21 +01:00

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

PR-URL: https://github.com/nodejs/node/pull/55622
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
This commit is contained in:
Marco Ippolito 2024-11-02 08:46:20 -05:00 committed by GitHub
parent 44afd67831
commit 560b2a1677
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 68 additions and 0 deletions

View File

@ -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}

View File

@ -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');
@ -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);

View File

@ -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();
}));
});

View File

@ -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);